在进行编写特征提取的代码中,使用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
发表评论