AttributeError: ‘module‘ object has no attribute ‘xfeatures2d‘

created at 12-06-2021 views: 79

problem

When practicing the sift operator, I found this problem:

AttributeError: 'module' object has no attribute 'xfeatures2d'

reason

OpenCV version problem, because the sift operator is patented, the original OpenCV cannot include this function, and it can only be included in the extension package `opencv-contrib-python`.

Solution

In two versions

1)If the version of opencv-python is <=3.4:

pip install opencv-contrib-python==3.4.2.17
sift = cv2.xfeatures2d.SIFT_create()
kp = sift.detect(gray_img, None)
img = cv2.drawKeypoints(gray_img, kp, img)
plt.imshow(img)

2) If the version of opencv-python is >=4.5:

pip install opencv-contrib-python==4.5.3.56

When using

sift = cv2.SIFT_create()
kp = sift.detect(gray_img, None)
img = cv2.drawKeypoints(gray_img, kp, img)
plt.imshow(img)
created at:12-06-2021
edited at: 12-06-2021: