问题
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'
回答1:
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
回答2:
From the issue: 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
来源:https://stackoverflow.com/questions/62601766/using-sift-or-an-alternative-in-python-opencv-4-2-0-in-2020