Python 3.x Python cv2 ORB检测和计算返回“;“输入图像中的通道数无效”;

Python 3.x Python cv2 ORB检测和计算返回“;“输入图像中的通道数无效”;,python-3.x,numpy,computer-vision,cv2,orb,Python 3.x,Numpy,Computer Vision,Cv2,Orb,我试图从两个不同的图像中提取和匹配特征,但由于某种原因,“detectAndCompute”方法在我的orb对象上不起作用: orb=cv2.orb_create() kp,角点=orb.detectAndCompute(图像,无 我正在传递一个灰度图像(函数np.float32(cv2.cvtColor(image,cv2.COLOR_BGR2GRAY))的返回值)。出于某种原因,程序返回以下错误: 回溯(最近一次呼叫最后一次): 文件“C:\Users\levxr\Desktop\Visua

我试图从两个不同的图像中提取和匹配特征,但由于某种原因,“detectAndCompute”方法在我的orb对象上不起作用: orb=cv2.orb_create() kp,角点=orb.detectAndCompute(图像,无 我正在传递一个灰度图像(函数np.float32(cv2.cvtColor(image,cv2.COLOR_BGR2GRAY))的返回值)。出于某种原因,程序返回以下错误: 回溯(最近一次呼叫最后一次): 文件“C:\Users\levxr\Desktop\Visual positioning bot main\alloverlay.py”,第37行,在 cv2.imshow(“摄影机”+str(i),corn1.updateanddisplay()) 文件“C:\Users\levxr\Desktop\Visual positioning bot main\features.py”,第33行,在UpdateAddDisplay中 dst=self.update(image=self.image) 文件“C:\Users\levxr\Desktop\Visual positioning bot main\features.py”,第23行,在更新中 kp,角点=orb.detectAndCompute(图像,无)
cv2.error:OpenCV(4.4.0)c:\users\appveyor\appdata\local\temp\1\pip-req-build-95hbg2jt\OpenCV\modules\imgproc\src\color.simd\u helpers.hpp:92:error:(-2:Unspecified error)函数“\uu cdecl cv::impl:
匿名名称空间”::CvtHelper很难理解,但我想我已经发现了问题:
您正在传递
orb.detectAndCompute
类型为
np.float32
的图像

  • orb.detectAndCompute
    不支持类型为
    np.float32
    的映像

重现问题:

下面的“简单测试”再现了这个问题:
代码示例将黑色(零)图像传递给
orb.detectAndCompute

以下代码无异常通过(图像类型为
np.uint8
):

以下代码引发异常,因为图像类型为
np.float32

# image type is float32:
image = np.float32(np.zeros((100, 100), np.uint8))
orb = cv2.ORB_create()
kp, corners = orb.detectAndCompute(image, None)
出现了一个例外:

输入图像中的通道数无效:


解决方案:

尽量避免
np.float32
转换。
您还可以将
image
转换为
uint8
,如下所示:

kp, corners = orb.detectAndCompute(image.astype(np.uint8), None)
# image type is uint8:
image = np.zeros((100, 100), np.uint8)
orb = cv2.ORB_create()
kp, corners = orb.detectAndCompute(image, None)
# image type is float32:
image = np.float32(np.zeros((100, 100), np.uint8))
orb = cv2.ORB_create()
kp, corners = orb.detectAndCompute(image, None)
kp, corners = orb.detectAndCompute(image.astype(np.uint8), None)