9

I am using OpenCV v4.20 and PyCharm IDE. I want to use SIFT algorithm. But I get this error. I looked for solutions of this error on the internet but none of them did help me. Do you know the solution of this error? (With pip I can install at least 3.4.2.16 version of OpenCV)

Here is my error:

Traceback (most recent call last): File "C:/Users/HP/PycharmProjects/features/featuredetect.py", line 7, in sift = cv.xfeatures2d_SIFT.create()

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'

Here is my code:

import cv2 as cv

image = cv.imread("the_book_thief.jpg")
gray_image = cv.cvtColor(image,cv.COLOR_BGR2GRAY)

sift = cv.xfeatures2d_SIFT.create()
keyPoints = sift.detect(image,None)

output = cv.drawKeypoints(image,keyPoints,None)

cv.imshow("FEATURES DETECTED",output)
cv.imshow("NORMAL",image)

cv.waitKey(0)
cv.destroyAllWindows()
coffeewin
  • 182
  • 3
  • 6
  • 26
Yunus Emre Üzmez
  • 113
  • 1
  • 1
  • 8
  • 3
    the error message is quite self-explaining, isn't it? – Micka Feb 04 '20 at 22:02
  • 1
    Does this answer your question? [sift = cv2.xfeatures2d.SIFT\_create() not working even though have contrib installed](https://stackoverflow.com/questions/52305578/sift-cv2-xfeatures2d-sift-create-not-working-even-though-have-contrib-instal) – HansHirse Feb 05 '20 at 07:59

5 Answers5

15

SIFT's patent has expired in last July. in versions > 4.4, the detector init command has changed to cv2.SIFT_create(). If you're not using opencv's GUI, It's recommended to install the headless version: pip install opencv-python-headless

Assaf-ge
  • 464
  • 4
  • 6
  • 4
    This is the correct answer in 2021. I had issues with `pip install opencv-python-headless`, but I was able to install `pip install opencv-contrib-python` and run sift as `cv2.SIFT_create()` without a problem. – blueether Mar 18 '21 at 21:24
  • 1
    As of January 2022, this is not correct anymore: it must have been moved once more. – rikyeah Jan 12 '22 at 12:58
  • @rikyeah please elaborate. – Assaf-ge Jan 14 '22 at 09:02
  • https://stackoverflow.com/users/11344054/nisan-chhetri 's answer below works for me, on a blank colab notebook – rikyeah Jan 14 '22 at 09:34
  • it's not a matter of date, it's a matter of opencv version. and for ver >4.4 this is correct one. – Assaf-ge Jan 31 '22 at 16:17
7

Unfortunately, according to this Github issue, SIFT no longer available in opencv > 3.4.2. Since you're using OpenCV v4.2.0, it's not included even if you have installed pip install opencv-contrib-python as shown in this post. A work around is to downgrade to any previous OpenCV version that includes SIFT (I believe any version below 3.4.3). I've been successful when downgrading to v3.4.2.16.

pip install opencv-python==3.4.2.16
pip install opencv-contrib-python==3.4.2.16

Using your code with v3.4.2.16, SIFT seems to work

nathancy
  • 42,661
  • 14
  • 115
  • 137
2

I had the same issue previously. I had tried all methods but finally a very simple method work for me which has already answered by many. However, there is a little change in my approach.

  1. Step 1:

    Uninstall the previously install opencv library

    pip uninstall opencv-python

  2. Step 2:

    Install opencv contrib library due to copyright issue. Here, we are using version 3.4.2.17

    pip install opencv-contrib-python==3.4.2.17

    opencv contrib library installation error

    The above figure shows version 3.4.2.16 not found error. Hence, I tried with version 3.4.2.17. If this version doesn't work, try other versions of 3.4.x.

  3. Step 3:

    Then run the following

    import cv2 sift = cv2.xfeatures2d.SIFT_create()

That's all. It works for me. I hope it works for you as well.

Nisan Chhetri
  • 339
  • 3
  • 3
1

I had the same issue, after a lot of attempts, I tried installing opencv-contrib-python several times, but it worked just today. Just to be sure I installed both opencv-python and opencv-contrib-python.

pip install opencv-python

And

pip install opencv-contrib-python 

The version that installed was 4.4.0.46 for both opencv-python and opencv-contrib-python. In case the later versions don't support it (A few of the previous versions didn't support SIFT, the one from a month ago, the latest opencv-contrib-python patch was released on Nov 2nd, 2020).

-3

The solution to your problem should be installing opencv-contrib-python-nonfree (available via pip).

$ pip install opencv-contrib-python-nonfree

As the error states SIFT is patented and therefore not included into OpenCV for license reasons. It's included in the nonfree part.

hazefully
  • 3
  • 3
Demosthenes
  • 1,515
  • 10
  • 22
  • 1
    When ı run this command pip install opencv-python-nonfree it says that " ERROR: Could not find a version that satisfies the requirement opencv-python-nonfree (from versions: none) ERROR: No matching distribution found for opencv-python-nonfree" – Yunus Emre Üzmez Feb 05 '20 at 23:42