问题:在用python3使用orb = cv2.ORB()进行ORB时候,可能会产生错误:TypeError: Incorrect type of self (must be 'Feature2D' or its derivative)
import numpy as np
import cv2
from matplotlib import pyplot as plt
img1 = cv2.imread('data/queryImage.jpg', 0) # queryImage
img2 = cv2.imread('data/trainImage.jpg', 0) # trainImage
# Initiate SIFT detector
orb = cv2.ORB()
# find the keypoints and descriptors with SIFT
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
报错信息:
TypeError Traceback (most recent call last)
<ipython-input-42-dfae86b275b4> in <module>()
3
4 # find the keypoints and descriptors with ORB
----> 5 kp1, des1 = orb.detectAndCompute(img1,None)
6 kp2, des2 = orb.detectAndCompute(img2,None)
7
TypeError: Incorrect type of self (must be 'Feature2D' or its derivative)
解决:将orb = cv2.ORB()替换为:orb = cv2.ORB_create()
分析:
在opencv2中, orb = cv2.ORB ()是对的;
在opencv3中这样会报错,改成orb = cv2.ORB_create()就没问题了。
变更样例:
import numpy as np
import cv2
from matplotlib import pyplot as plt
img1 = cv2.imread('data/queryImage.jpg', 0) # queryImage
img2 = cv2.imread('data/trainImage.jpg', 0) # trainImage
# Initiate SIFT detector
#orb = cv2.ORB() 将orb= cv2.ORB()替换为:orb= cv2.ORB_create()
orb= cv2.ORB_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
说明:问题产生的环境
Python版本:3.6.5
OpenCV版本:3.4.2