9

I am trying to run the simplest opencv SIFT code through the shell of Ubuntu, with no luck

I get an error:

AttributeError: 'module' object has no attribute 'SURF'

The code:

import cv2
cv2.SIFT()

My configurations:

  • Ubuntu version is 13.10 64bit
  • cv2.__version__ is 2.4.5
  • the output of dir(cv2) is (for the S letter only)

'scaleAdd', 'segmentMotion', 'sepFilter2D', 'setIdentity', 'setMouseCallback', 'setTrackbarPos', 'setUseOptimized', 'setWindowProperty', 'solve', 'solveCubic', 'solvePnP', 'solvePnPRansac', 'solvePoly', 'sort', 'sortIdx', 'split', 'sqrt', 'startWindowThread', 'stereoCalibrate', 'stereoRectify', 'stereoRectifyUncalibrated', 'subtract', 'sumElems'

Chris Tang
  • 567
  • 7
  • 18
Oz Radiano
  • 779
  • 2
  • 11
  • 30
  • 1
    SIFT and SURF are nonfree, patented algos. some package managers ( eg. debian ) give you a cv2 module, that does not include them. – berak Feb 09 '14 at 20:17
  • I have additional configuration - Windows 7 with python 2.7 and opencv 2.4.6, in which I am able to call SIFT() using both terminal and pyDev... which lib caused it to work on this configuration? – Oz Radiano Feb 09 '14 at 22:43
  • 1
    the lib would be opencv_nonfree, but the prebuilt cv2.pyd on win is probably statically linked (so it's included already) again, if you need it on linux, get the source, compile your own, and you're done. – berak Feb 09 '14 at 22:52
  • Thanks! Where can I get it? – Oz Radiano Feb 09 '14 at 23:23
  • 1
    either [sourceforge](http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.8/opencv-2.4.8.zip/download) or [github](https://github.com/itseez/opencv) – berak Feb 10 '14 at 07:39
  • Thanks, Can you write it as a reply so I could approve your answer? – Oz Radiano Feb 10 '14 at 10:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47158/discussion-between-oz-radiano-and-berak) – Oz Radiano Feb 10 '14 at 10:39
  • Possible duplicate of [Can't use SURF, SIFT in OpenCV](https://stackoverflow.com/questions/18561910/cant-use-surf-sift-in-opencv) – Georgy May 16 '18 at 08:40

9 Answers9

6

This was driving me crazy but scratch all the other suggestions, turns out you can now get SIFT and SURF with just two terminal commands!

  1. Be sure there is no other opencv on you computer...

    pip uninstall opencv-python
    
  2. Then get the contribute version (has SIFT and SURF + others)...

    pip install opencv-contrib-python
    

It should install but note that the names are a little different.

import cv2
sift = cv2.xfeatures2d.SIFT_create()

!!!pip pip hurray!!! (that's just a pun, not part of the code)

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Tripp Cannella
  • 1,094
  • 1
  • 8
  • 9
5
import cv2
sift = cv2.SIFT()

This code will not work if you are using opencv version 3.0 or greater. an alternative of this code is

sift = cv2.xfeatures2d.SIFT_create()
(Only works if you have installed opencv-contrib-python library )

Now again if you have an opencv-contrib-python version > 3.4 than it won't work with a different erro

error: OpenCV(4.1.0) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1207: 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'

best solution for this is:

**step 1: pip uninstall opencv-python**

**step 2: pip install opencv-contrib-python==3.4.2.16**

This worked for me.

[Note : If you have not installed opencv using pip install opencv-python, than just go and delete the downloaded library and follow the above instruction]

veben
  • 19,637
  • 14
  • 60
  • 80
shubham singh
  • 511
  • 1
  • 5
  • 16
2

Not the smoothest way to do it, but it worked for me.

@Berak explained to me, as you can observe in the comments on my question, that the SIFT algorithm, as well as FAST algorithm are patented, which means that they are not part of the regular opencv installation.

Therefore I searched for a python distribution that will have it all - and I found one. So, I didn't actually solved the problem, as @Berak suggested, alternatively I bypassed it using Python(x,y)

Thanks for the help,

Ozrad

Oz Radiano
  • 779
  • 2
  • 11
  • 30
  • "not part of the regular opencv installation." - there is no regular *binary* distribution. it's included in the src, and maintainers of PCAs decide, if they pack it in or not. – berak May 09 '14 at 15:46
1

I also had problem in using SIFt because i had only openCV. But after installing ROS Hydro, i am able to use SIFT/SURF as they come under nonfree part.

skm
  • 5,015
  • 8
  • 43
  • 104
1

A simple code i found for SIFT

import cv2
import numpy as np

img = cv2.imread('home.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

sift = cv2.SIFT()
kp = sift.detect(gray,None)

img=cv2.drawKeypoints(gray,kp)

cv2.imwrite('sift_keypoints.jpg',img)

And i tested the code, it works

iec2011007
  • 1,828
  • 3
  • 24
  • 38
0

I followed the openCV python windows installation guide. I went to try using cv2.SIFT and found it was not available in this installation.

After completely removing python 2.7 and openCV, I then installed python(x,y) and included openCV. I get
cv2.version
'2.4.8'

And: cv2.SIFT -> cv2.SURF

So python(x,y) does include SIFT,SURF modules.

Community
  • 1
  • 1
  • let me clarify: python is not the problem here, the question is, wether the nonfree module was compiled into your *cv2.pyd* or not. – berak May 09 '14 at 15:43
0

You can use it easily as follows:

sift = cv2.xfeatures2d_SIFT()

keypoints_detector = sift.detect(image=your_grayscale_image, mask=None)
Reza Ghorbani
  • 2,396
  • 2
  • 28
  • 33
0

Both the SIFT and SURF are pattented algorithms by their respective authors. Previously they were not available in the main repository of OpenCV but in contrib but now as per OpenCV, their patent has been expired in 2020 hence SIFT and SURF are added in the main repository in latest releases. I have tried 4.5.2 and it works fine.

You can install this opencv version using pip3 install opencv-python=4.5.2.

To instantiate and test SIFT use the below code.

import numpy as np
import cv2 as cv

img = cv.imread('home.jpg')
gray= cv.cvtColor(img,cv.COLOR_BGR2GRAY)
sift = cv.SIFT_create()
kp = sift.detect(gray,None)
img=cv.drawKeypoints(gray,kp,img)
cv.imwrite('sift_keypoints.jpg',img)

0

Use a version above 4.4.0, SIFT was moved into main lib again. https://opencv.org/opencv-4-4-0/

pip install opencv-python==4.4.0.46
import cv2
sift = cv2.SIFT_create()
Raul Medeiros
  • 224
  • 2
  • 5