OpenCV3.0 - module has no attribute SIFT
Asked Answered
I

6

9

I am using Ubuntu 12.04. I recently installed OpenCV 3.0 from https://github.com/Itseez/opencv/archive/3.0.0-alpha.zip. I want to do feature matching for which I used the following code:

import numpy as np
import cv2
from matplotlib import pyplot as plt

MIN_MATCH_COUNT = 10

img1 = cv2.imread('box.png',0)          # queryImage
img2 = cv2.imread('box_in_scene.png',0) # trainImage

# Initiate SIFT detector
sift = cv2.SIFT()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)

flann = cv2.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(des1,des2,k=2)

# store all the good matches as per Lowe's ratio test.
good = []
for m,n in matches:
    if m.distance < 0.7*n.distance:
        good.append(m)

if len(good)>MIN_MATCH_COUNT:
    src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
    dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)

    M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
    matchesMask = mask.ravel().tolist()

    h,w = img1.shape
    pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
    dst = cv2.perspectiveTransform(pts,M)

    img2 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.CV_AA)

else:
    print "Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT)
    matchesMask = None

draw_params = dict(matchColor = (0,255,0), # draw matches in green color
                   singlePointColor = None,
                   matchesMask = matchesMask, # draw only inliers
                   flags = 2)

img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params)

plt.imshow(img3, 'gray'),plt.show()

I get the following error:

Traceback (most recent call last):
  File "feature_matching.py", line 11, in <module>
    sift = cv2.SIFT()
AttributeError: 'module' object has no attribute 'SIFT'

Why is SIFT not available in OpenCV 3.0? How do I add SIFT to OpenCV 3.0? Any help would be appreciated. Thankyou.

PS. I tried including modules from https://github.com/Itseez/opencv_contrib

$ cd <opencv_build_directory>
$ cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source_directory>
$ make -j5
$ make install
Invoice answered 9/9, 2014 at 2:16 Comment(1)
finally, I use opencv3.2.0 + opencv_contrib, it works. ^)^Thorlie
S
6
  • yes, you need to build the opencv_extra modules (esp. xfeatures2d).

  • don't forget to run make install after the cmake/make step (your new python module has to get copied over to python/lib/sitelibs)

  • in 3.0 it's : cv2.xfeatures2d.SIFT (note the additional namespace)

Steib answered 9/9, 2014 at 5:22 Comment(17)
could you please tell me the exact steps I should follow? I tried what was mentioned in github.com/Itseez/opencv_contrib (code mentioned in my question above). When should I do make install? I am a bit new to Ubuntu, still getting the hang of it. <opencv_build_directory> refers to the build folder where opencv was installed right? and what about <opencv_source_directory>? Is it the folder where all the opencv folders get extracted to?Invoice
I forgot to run make install last time. I did so this time but still see the same error. Also tried changing cv2.SIFT() to cv2.xfeatures2d.SIFT() and same error shows again. Module object has no attribute xfeatures2d.Invoice
you can do help(cv2) to see, what made it into cv2.pyd this time. if xfeatures2d is missing, there must have been build errors ? there were issues with the cudaarithm dependancy. if you build without cuda, you should remove it from the dependancySteib
I did help(cv2) and didnt see xfeatures2d under Functions. I did see Feature2d_create(...) . I didnt see any build errors while building opencv_contrib. What am I doing wrong? By the way, what is cv2.pyd?Invoice
ok. Feature2d_create(...) is from the 'normal' opencv features2d module(which does no more include sift,surf,brief and star). let me see, if it builds for me today..Steib
so, getting same problem now. it seems, like the python wrappers for xfeatures2d are deactivated for now. again, 3.0 is alpha.Steib
thanks for trying it out. So there's no other way to get it working other than to wait for the stable release? Is SIFT availble in opencv2.4.9? I had tried this code for feature detection in opencv2.4.9 but the function drawMatches() did not exist in 2.4.9 so I had to upgrade to 3.0.Invoice
if you can easily switch back to 2.4.9, do that. at least for some days, until the dust settles. again, it was working 1 day ago, only the java wrappers were broken, now they probably switched it off completely, until it works.Steib
so do I have to reinstall opencv3.0 or just the opencv_contrib to see if it works later?Invoice
^^ please see the edit. you might be able to just recompile it.Steib
you change the cmakelists.tst file there. then rerun cmakeSteib
you mean I should add that code to the end of the cmakelists.txt file in the folder called cudaarithm folder inside the folder modules? Strangely enough I have the same set of folders inside the build folder also (except that the cudaarithm folder is empty there). Is that normal?Invoice
1. either delete cudaarithm from cmakelists.txt or move it to the end and put 'OPTIONAL' before it. 2. yes, confusing, but normal. (you can even throw away the build folder later, the only important one is the one you install toSteib
I uninstalled opencv3.0 and installed opencv2.4.9. I guess it would be better to wait for the stable release of opencv3.0 (December?). If I shift my focus from python to C or C++, would SIFT work? I think the issue is with opencv-python (not much support either).Invoice
if i only knew that ;)Steib
opencv_cudaarithm is now optional in the OpenCV 3 beta. Unfortunately, I still can't get access to xfeatures2d in pyopencv. >>> help(cv2.xfeatures2d) ... AttributeError: 'module' object has no attribute 'xfeatures2d' Bypath
Also, the function was renamed to SIFT_create().Ship
P
0

Another possibility (and the easiest one I found!) is to install the 2.4.9 release which already include SIFT and SURF algorithm. You just have to do then

import cv2
sift = cv2.SIFT()
(...)
Ptyalin answered 12/1, 2015 at 22:38 Comment(0)
D
0

Because of SIFT and SIRF are patended by theirs creators, these descriptors were moved to opencv_contrib package. In order to use it you need download and install both packages: original and contrib.

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

more information http://www.pyimagesearch.com/2015/07/16/where-did-sift-and-surf-go-in-opencv-3/

Dyna answered 19/10, 2015 at 9:35 Comment(0)
S
0

There is a pip source that makes this very easy.

If you have another version of opencv-python installed use this command to remove it to avoid conflicts:

pip uninstall opencv-python Then install the contrib version with this:

pip install opencv-contrib-python SIFT usage:

import cv2 sift = cv2.xfeatures2d.SIFT_create()

source:Can't use SURF, SIFT in OpenCV

Savitt answered 27/5, 2018 at 10:18 Comment(0)
U
0

As mentioned above you can use the precompiled version of OpenCV which is available via pip:
pip install opencv-python

If you want to access extra modules (such as experimental) you can use:
pip install opencv-contrib-python

If you need non-free modules (SURF, SIFT and other) you won't find it after execution of previous command. You need to use this one:
pip install opencv-contrib-python-nonfree

Full list of modules (including contrib and non-free you can find in OpenCV docs.

Uncritical answered 8/5, 2020 at 5:30 Comment(0)
C
0

Try using

sift = cv2.xfeatures2d.SIFT_create()
Contaminate answered 19/2, 2021 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.