1. AttributeError: module ‘cv2’ has no attribute ‘SIFT’
Traceback (most recent call last):
File "F:/PyCharm/pydir/t3.py", line 4, in <module>
sift = cv2.SIFT()
AttributeError: module 'cv2' has no attribute 'SIFT'
原因:opencv将SIFT等算法整合到xfeatures2d集合里面了。变更后写法如下:
import cv2
img = cv2.imread('test3.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
kp = sift.detect(gray,None)
img=cv2.drawKeypoints(gray,kp,img)
cv2.imwrite('sift_keypoints.jpg',img)
2. TypeError: Incorrect type of self (must be ‘Feature2D’ or its derivative)
Opencv-3.1.0文档未更新。提供的代码不起作用。FAST algorithm in OpenCV 的例子应该修改为:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('simple.jpg',0)
# Initiate FAST object with default values
fast = cv2.FastFeatureDetector_create(threshold=25)
# find and draw the keypoints
kp = fast.detect(img,None)
img2 = cv2.drawKeypoints(img, kp, None,color=(255,0,0))
print("Threshold: ", fast.getThreshold())
print("nonmaxSuppression: ", fast.getNonmaxSuppression())
print("neighborhood: ", fast.getType())
print("Total Keypoints with nonmaxSuppression: ", len(kp))
cv2.imwrite('fast_true.png',img2)
# Disable nonmaxSuppression
fast.setNonmaxSuppression(0)
kp = fast.detect(img,None)
print ("Total Keypoints without nonmaxSuppression: ", len(kp))
img3 = cv2.drawKeypoints(img, kp, None, color=(255,0,0))
cv2.imwrite('fast_false.png',img3)
3. AttributeError: ‘module’ object has no attribute ‘FeatureDetector_create’
orb = cv2.ORB()
Regarding the 3.1 version, these functions have been removed in favor of functions like
detector = cv2.TYPE_create()
where TYPE can be ORB or other detector of your choosing but not SURF and SIFT which have been moved to a nonfree package.
4. TypeError: Required argument ‘outImage’ (pos 3) not found
img2 = cv2.drawKeypoints(img,kp,color=(0,255,0), flags=0)
img2 = cv2.drawKeypoints(img,kp,img,color=(0,255,0), flags=0)