SIFT() in opencv is not working: 'module' object has no attribute 'SURF'
Asked Answered
M

9

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'

Mathes answered 9/2, 2014 at 18:55 Comment(8)
SIFT and SURF are nonfree, patented algos. some package managers ( eg. debian ) give you a cv2 module, that does not include them.Kirchner
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?Mathes
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.Kirchner
Thanks! Where can I get it?Mathes
either sourceforge or githubKirchner
Thanks, Can you write it as a reply so I could approve your answer?Mathes
let us continue this discussion in chatMathes
Possible duplicate of Can't use SURF, SIFT in OpenCVMetheglin
M
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

Mathes answered 11/2, 2014 at 0:4 Comment(1)
"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.Kirchner
S
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)

Seiler answered 29/11, 2017 at 2:1 Comment(1)
This is a beautiful answer. Works as charm on my python3.8 Ubuntu 20.04Trepang
I
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]

Intercession answered 16/5, 2019 at 6:14 Comment(0)
M
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

Mathes answered 11/2, 2014 at 0:4 Comment(1)
"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.Kirchner
H
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.

Horeb answered 9/2, 2014 at 20:10 Comment(0)
O
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

Overprize answered 6/9, 2015 at 5:33 Comment(0)
W
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.

Windswept answered 9/5, 2014 at 15:40 Comment(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.Kirchner
D
0

You can use it easily as follows:

sift = cv2.xfeatures2d_SIFT()

keypoints_detector = sift.detect(image=your_grayscale_image, mask=None)
Don answered 28/12, 2019 at 12:59 Comment(0)
L
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)

Leyte answered 10/6, 2021 at 6:14 Comment(0)
T
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()
Tourcoing answered 3/7, 2021 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.