Python 3.x Python cv2未将图像转换为灰度
因此,我编写了一个模块来检测特定图像的特征。在没有更改图像格式、源或modeule中的任何内容的情况下,它突然向我返回以下异常:Python 3.x Python cv2未将图像转换为灰度,python-3.x,opencv,feature-detection,3d-reconstruction,Python 3.x,Opencv,Feature Detection,3d Reconstruction,因此,我编写了一个模块来检测特定图像的特征。在没有更改图像格式、源或modeule中的任何内容的情况下,它突然向我返回以下异常: gray = np.float32(cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)) cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-pz4stnv8\opencv\modules\imgproc\src\color.c
gray = np.float32(cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY))
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-pz4stnv8\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
模块代码如下所示:
import sys
import cv2
import numpy as np
class Unknown_algorythm_error(Exception):
def __init__(self):
pass
class Corner_detector():
def __init__(self, image, detectortype="Harris", corners=[]):
self.corners = corners
self.image = image
self.detectortype = detectortype
def update(self, image=None):
try:
if image.type() != None:
self.image = image
except:
self.image = image
gray = np.float32(cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY))
if self.detectortype == "Harris":
self.corners = cv2.cornerHarris(gray, 3, 3, 0, 1)
elif self.detectortype == "Shi-Tomasi":
self.corners = cv2.goodFeaturesToTrack(gray, 3, 3, 0, 1)
else:
raise Unknown_algoryth_error
return self.corners
def updateanddisplay(self):
dst = self.update(image=self.image)
self.image[dst>0.01*dst.max()] = [0, 0, 255]
return self.image
这是因为没有图像可转换。即使当
image.type()==None
时,您仍然调用cvtColor()
。