赞
踩
在进行编写特征提取的代码中,使用SURF和SIFT算法一直报错。
- # 写法1
- sift = cv2.xfeatures2d.SIFT_create()
- surf = cv2.xfeatures2d.SURF_create()
- # 写法2
- sift = cv2.SIFT_create()
- 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
- # 卸载之前的OpenCV
- pip uninstall opencv-python
- # 安装指定版本的OpenCV和pencv-contrib-python
- pip install opencv-python==3.4.2.16
- pip install opencv-contrib-python==3.4.2.16
然后我都就可以正常运行,大家一定要注意python的版本不能太高,不然还是会报错,为了避免经常报错,一般我创建虚拟环境时都会指定python=3.7
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。