在 Python OpenCV 4.2.0(2020 年)中使用 SIFT(或替代方案) [英] Using SIFT (or an alternative) in Python OpenCV 4.2.0 (In 2020)

查看:703
本文介绍了在 Python OpenCV 4.2.0(2020 年)中使用 SIFT(或替代方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SIFT 通过 Python 进行特征检测,但它不再是 OpenCV OpenCV contrib 的一部分.

使用 OpenCV opencv-contrib-python(两个版本 4.2.0.34,这个问题的最新版本),我得到:

<预><代码>>>>导入 cv2>>>cv2.SIFT_create()回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.AttributeError: 模块 'cv2.cv2' 没有属性 'SIFT_create'>>>cv2.xfeatures2d.SIFT_create()回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1210:错误:(-213:未实现该功能/特性)此算法已获得专利并被排除在这个配置;设置 OPENCV_ENABLE_NONFREE CMake 选项并在函数中重建库'cv::xfeatures2d::SIFT::create'

我发现的每个相关答案都建议使用 contrib 或旧版本,但这些都不再有效.

是否更容易从源代码构建它以获取错误指示的 SIFT,或者使用替代方法?我将如何做这些?我只需要某种方法来进行特征检测,最好是尺度不变的.

这个问题 提到了 SIFT 替代方案,但已经过时了(最佳答案是 8 岁左右).2020 年我们现在可以做什么?

编辑显示 OpenCV 3 不起作用:

尝试安装 OpenCV 3:

<预><代码>>>>pip 安装 opencv-python==3错误:找不到满足 opencv-python==3 要求的版本(来自版本:3.4.2.16、3.4.2.17、3.4.3.18、3.4.4.19、3.4.5.20、3.4.6.27、3.4.7.28, 3.4.8.29, 3.4.9.31, 3.4.9.33, 4.0.0.21, 4.0.1.23, 4.0.1.24, 4.1.0.25,4.1.1.26、4.1.2.30、4.2.0.32、4.2.0.34)错误:找不到与 opencv-python==3 匹配的发行版>>>pip 安装 opencv-python==3.4.2.16

然后在 Python 中:

<预><代码>>>>导入 cv2>>>打印(cv2.__version__)3.4.2>>>cv2.SIFT_create()回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.AttributeError: 模块 'cv2.cv2' 没有属性 'SIFT_create'

解决方案

SIFT 的专利于 2020 年 3 月到期.但通过将 SIFT 移至免费开源集合,可能无法更新 opencv.

看到这个问题:https://github.com/skvark/opencv-python/issues/126

使用非自由组件重建:

git clone --recursive https://github.com/skvark/opencv-python.gitcd opencv-pythonexport CMAKE_ARGS=-DOPENCV_ENABLE_NONFREE=ON"python setup.py bdist_wheel

I am trying to use SIFT for feature detection with Python, but it is no longer part of OpenCV or OpenCV contrib.

With OpenCV opencv-contrib-python (both versions 4.2.0.34, the latest as of this question), I get:

>>> import cv2
>>> cv2.SIFT_create()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'cv2.cv2' has no attribute 'SIFT_create'
>>> cv2.xfeatures2d.SIFT_create()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1210: 
 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::SIFT::create'

Every related answer I have found has suggested using contrib or an older version, but neither of these work anymore.

Is it easier to build it from source to get SIFT back as the error indicates, or to use an alternative? How would I do either of these? All I need is some way to do feature detection, preferably scale-invariant.

This question mentions SIFT alternatives but is very outdated (best answers are around 8 years old). What can we do now in 2020?

EDIT Showing OpenCV 3 not working:

Trying to install OpenCV 3:

>>> pip install opencv-python==3 
ERROR: Could not find a version that satisfies the requirement opencv-python==3
(from versions: 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.5.20, 3.4.6.27,
3.4.7.28, 3.4.8.29, 3.4.9.31, 3.4.9.33, 4.0.0.21, 4.0.1.23, 4.0.1.24, 4.1.0.25,
4.1.1.26, 4.1.2.30, 4.2.0.32, 4.2.0.34)
ERROR: No matching distribution found for opencv-python==3
>>> pip install opencv-python==3.4.2.16  

Then in Python:

>>> import cv2
>>> print(cv2.__version__)
3.4.2
>>> cv2.SIFT_create()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'cv2.cv2' has no attribute 'SIFT_create'

解决方案

The patent for SIFT expired this Mar 2020. But the opencv might not be updated by moving the SIFT to free open source collection.

See this issue: https://github.com/skvark/opencv-python/issues/126

To rebuild with the non-free components:

git clone --recursive https://github.com/skvark/opencv-python.git
cd opencv-python
export CMAKE_ARGS="-DOPENCV_ENABLE_NONFREE=ON"
python setup.py bdist_wheel

这篇关于在 Python OpenCV 4.2.0(2020 年)中使用 SIFT(或替代方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆