opencv使用surf和sift算法报错解决记录
1.报错代码,使用以下两种写法都会报错
# 创建sift和surf特征提取器
# 写法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’
2.查找网络上的解决办法,大部分分为两种
-
sift和surf算法申请了专利,所以opencv新版本删除了这两个算法,所以需要回退到opencv3开头的版本或者opencv4开头的版本
-
缺少opencv-contrib-python,pip install opencv-contrib-python即可解决(亲测没用)
所以主要使用回退opencv版本的各种方法:
(1)直接卸载之前环境中的opencv,然后安装对应版本的opencv
# 卸载之前的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
执行完以上代码之后,使用第一种写法调用sift和surf算法,即
sift = cv2.xfeatures2d.sift_create()
surf = cv2.xfeatures2d.surf_create()
(2)创建新的虚拟环境,设置python版本为3.6,然后新安装需要的包
因为我使用的虚拟环境中的python是3.9版本的,所以在安装3.4.2.16或者其他的不是最新版本的opencv时,会报以下错误:
traceback (most recent call last):
file “< string>”, line 1, in < module >file “/tmp/pip-build-ilgdiy_6/opencv-contrib-python/setup.py”, line 10, in < module >import skbuildmodulenotfounderror: no module named ‘skbuild’
或者报错
note: this error originates from a subprocess, and is likely not a problem with pip.error: legacy-install-failure
× encountered error while trying to install package.
╰─> opencv-python
note: this is an issue with the package mentioned above, not pip.
hint: see above for output from the failure.
或者
looking in indexes: https://pypi.anaconda.org/sts_dileeppj/simple
error: could not find a version that satisfies the requirement opencv-python4.4.0 (from versions: none)
error: no matching distribution found for opencv-python4.4.0
warning: ignoring invalid distribution -pencv-python (c:\users\d
找了各种解决办法,
清除缓存,然后重新安装opencv
# 清除缓存
pip cache purge
安装scikit-build,参见https://zhuanlan.zhihu.com/p/491491510
scikit-build
甚至说是因为有多个python环境导致的错误。。。。。。
尝试了很多方法都没有用,打算放弃的时候,发现了救星
关于python-opencv的sift,surf算法调用错误
在这篇文章中发现可能安装不成功是因为python版本太高,所以安装不了opencv3的版本,然后创建了新的虚拟环境,设置python版本为3.6 ,然后在新的虚拟环境中安装opencv
pip install opencv-python==3.4.2.16
pip install opencv-contrib-python==3.4.2.16
因为是使用jupyter notebook,所以把新的虚拟环境应用到jupyter,参考博文是https://blog.csdn.net/fenglying/article/details/114929571
然后,在jupyter中新建文件时选择新建的opencv环境,如下图
然后执行代码,顺利完成,成功显示了一个demo图片!
至此,终于解决了sift和surf在opencv中的调用问题!!!!
文中引用链接皆为学习转载,侵删。
发表评论