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

问题

在练习sift算子的时候,发现出现这个问题:

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

原因

OpenCV版本问题,由于sift算子是有专利的,固原始OpenCV是无法包含该函数的,只能在扩展包中opencv-contrib-python

解决办法

分两个版本

  • 如果opencv-python的版本是<=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)
  • 如果opencv-python的版本是>=4.5
    • pip install opencv-contrib-python==4.5.3.56
    • 使用的时候
sift = cv2.SIFT_create()
kp = sift.detect(gray_img, None)
img = cv2.drawKeypoints(gray_img, kp, img)
plt.imshow(img)

参考

猜你喜欢

转载自blog.csdn.net/uncle_ll/article/details/121637640