当前位置:   article > 正文

解决调用surf = cv2.xfeatures2d.SURF_create()报错:cv2.error: OpenCV(4.10.0) D:\a\opencv-python\opencv-pytho

解决调用surf = cv2.xfeatures2d.SURF_create()报错:cv2.error: OpenCV(4.10.0) D:\a\opencv-python\opencv-pytho

在进行编写特征提取的代码中,使用SURFSIFT算法一直报错。

  1. # 写法1
  2. sift = cv2.xfeatures2d.SIFT_create()
  3. surf = cv2.xfeatures2d.SURF_create()
  4. # 写法2
  5. sift = cv2.SIFT_create()
  6. surf = cv2.SURF_create()

第一种报错AttributeError: module ‘cv2’ has no attribute ‘xfeatures2d’

第二种报错AttributeError: module ‘cv2’ has no attribute ‘SURF_create’

这两种写法全部会报错,opencv-contrib-python,pip install opencv-contrib-python也去全部安装了,后面发现是在OpenCV 4.0及之后的版本中,SURF和SIFT等专利算法被移至cv2.xfeatures2d模块,并且需要额外的配置才能使用。而我的opencv为4.10的。

cv2.error: OpenCV(4.10.0) D:\a\opencv-python\opencv-python\opencv_contrib\modules\xfeatures2d\src\surf.cpp:1028: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'cv::xfeatures2d::SURF::create'

解决方法

我的虚拟环境的python是3.7的,直接卸载之前安装的opencv的版本,安装对应版本的opancv

  1. # 卸载之前的OpenCV
  2. pip uninstall opencv-python
  3. # 安装指定版本的OpenCV和pencv-contrib-python
  4. pip install opencv-python==3.4.2.16
  5. pip install opencv-contrib-python==3.4.2.16

然后我都就可以正常运行,大家一定要注意python的版本不能太高,不然还是会报错,为了避免经常报错,一般我创建虚拟环境时都会指定python=3.7

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/926740
推荐阅读
相关标签
  

闽ICP备14008679号