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
Clive
  • 1,137
  • 7
  • 19
  • 28

6 Answers6

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)

berak
  • 39,159
  • 9
  • 91
  • 89
  • could you please tell me the exact steps I should follow? I tried what was mentioned in https://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. refers to the build folder where opencv was installed right? and what about ? Is it the folder where all the opencv folders get extracted to? – Clive Sep 09 '14 at 05:31
  • 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. – Clive Sep 09 '14 at 12:11
  • 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 dependancy – berak Sep 09 '14 at 12:24
  • 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? – Clive Sep 09 '14 at 12:46
  • 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.. – berak Sep 09 '14 at 13:00
  • so, getting same problem now. it seems, like the python wrappers for xfeatures2d are deactivated for now. again, 3.0 is alpha. – berak Sep 09 '14 at 13:26
  • 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. – Clive Sep 09 '14 at 13:36
  • 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. – berak Sep 09 '14 at 13:42
  • so do I have to reinstall opencv3.0 or just the opencv_contrib to see if it works later? – Clive Sep 09 '14 at 15:07
  • ^^ please see the edit. you might be able to just recompile it. – berak Sep 09 '14 at 15:42
  • you change the cmakelists.tst file there. then rerun cmake – berak Sep 09 '14 at 16:27
  • 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? – Clive Sep 10 '14 at 02:40
  • 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* to – berak Sep 10 '14 at 06:22
  • 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). – Clive Sep 10 '14 at 06:37
  • if i only knew that ;) – berak Sep 10 '14 at 06:42
  • 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' ` – Calvin Jan 30 '15 at 07:45
  • Also, the function was renamed to `SIFT_create()`. – Delgan Jun 24 '16 at 16:10
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()
(...)
Jprog
  • 79
  • 1
  • 8
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/

angubenko
  • 264
  • 1
  • 3
  • 11
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

majid lesani
  • 189
  • 9
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.

I.R.
  • 442
  • 1
  • 7
  • 16
0

Try using

sift = cv2.xfeatures2d.SIFT_create()
Salma Elshahawy
  • 1,112
  • 2
  • 11
  • 21