Pergunta

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'

Foi útil?

Solução 3

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

Outras dicas

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)

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]

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.

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

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.

You can use it easily as follows:

sift = cv2.xfeatures2d_SIFT()

keypoints_detector = sift.detect(image=your_grayscale_image, mask=None)

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)

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()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top