3

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'
Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • Does [PyImageSearch](https://www.pyimagesearch.com/2015/07/16/where-did-sift-and-surf-go-in-opencv-3/) help? It’s opencv 3 not 4, though. – Pam Jun 26 '20 at 19:48
  • It should be available in Python 3 OpenCV 3. But you can use ORB in place of SIFT. – fmw42 Jun 26 '20 at 19:54
  • @fmw42 See edits. I tried installing 3 as well, but it doesn't work. Unless I'm missing something on how to install an even older version. I'm also trying ORB from the OpenCV docs, but so far with OpenCV 4.2.0 I can't even replicate their example. https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html – Salvatore Jun 26 '20 at 20:04
  • I do not know much about v4. I am using Python 3.7 and OpenCV 3.4.8 installed using MacPorts. I had SIFT installed in previous versions, but not now. But I have used ORB. Always check https://docs.opencv.org/4.1.1/ for documentation changes for your specific version of OpenCV. Use the version selector. – fmw42 Jun 26 '20 at 20:08
  • Perhaps this will help. See https://stackoverflow.com/questions/18561910/cant-use-surf-sift-in-opencv or https://drthitirat.wordpress.com/2019/01/20/opencv-python-build-opencv-4-0-1-dev-contrib-non-free-siftsurf-from-sources-on-windows-10-64-bit-os/ and https://pysource.com/2018/03/21/feature-detection-sift-surf-obr-opencv-3-4-with-python-3-tutorial-25/ – fmw42 Jun 26 '20 at 20:24

2 Answers2

3

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
Salvatore
  • 10,815
  • 4
  • 31
  • 69
Dr Yuan Shenghai
  • 1,849
  • 1
  • 6
  • 19
  • 1
    Note: as of OpenCV 4.4 SIFT is re-enabled without using the NONFREE flag so if you're trying to avoid the non-free packages you can just install the latest rather than enable all the patented material. – Ford Davis Oct 01 '20 at 18:21
1

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
Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • I was able to get a build following your note and at the last step i run into this error: [100%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o In file included from /app/opencv-python/_skbuild/linux-x86_64-3.7/cmake-build/modules/python_bindings_generator/pyopencv_custom_headers.h:7, from /app/opencv-python/opencv/modules/python/src2/cv2.cpp:1894: /app/opencv-python/opencv_contrib/modules/phase_unwrapping/misc/python/pyopencv_phase_unwrapping.hpp:2:13: error: ‘phase_unwrapping’ in namespace ‘cv’ does not name a type typedef cv::phase. – shivas Jun 29 '20 at 13:46