14

I'm trying to add the xfeatures2d module from opencv_contrib to an existing OpenCV/Python project.

I've downloaded the latest version of the module from the repo, and built OpenCV again with the following additional params:

OPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib-master/modules
BUILD_opencv_xfeatures2d=ON

Excerpt from build log:

-- Installing: /usr/local/lib/python2.7/site-packages/cv2.so
-- Installing: /usr/local/lib/python3.4/site-packages/cv2.so
-- Installing: /usr/local/lib/libopencv_xfeatures2d.3.0.0.dylib

It appears the new module is installed correctly. I'm able to import cv2 in both Python versions. However neither recognise the new features the module is supposed to add.

>>> cv2.SURF()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SURF'
>>> cv2.xfeatures2d.SURF()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'xfeatures2d'
Robbert
  • 5,063
  • 6
  • 36
  • 44

4 Answers4

17

I encountered this same issue. I'm using python 2.7.6 and OpenCv 3.0 with the additional non-free modules. I do have xfeatures2d present in available modules and can import it, however it was as though xfeatures2d didn't contain SIFT or SURF. No matter how I called them it was the same Error:

"AttributeError: 'module' object has no attribute 'SIFT'

I tried the different name spaces suggested, and only recently noticed this detail and GOT IT WORKING!

$ python

>>>import cv2

>>>help(cv2.xfeatures2d)

You'll notice that it replies that it is now referred to as...

FUNCTIONS

SIFT_create(...)

and

SURF_create(...)

So very simply - the namespace is NOT "cv2.SIFT()" or "cv2.xfeatures2d.SIFT" but rather

cv2.xfeatures2d.SIFT_create()

Please give it a shot!

Community
  • 1
  • 1
Sovtek
  • 186
  • 1
  • 5
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](http://stackoverflow.com/questions/ask). You can also [add a bounty](http://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question once you have enough [reputation](http://stackoverflow.com/help/whats-reputation). – RiggsFolly Jan 26 '15 at 18:38
  • 4
    Thanks @RiggsFolly, but this could actually, potentially, answer the question. – Robbert Jan 27 '15 at 08:03
  • 1
    @Sovtek - thanks for submitting your findings! I'll have a look at this solution later this week, when I continue work on the OpenCV project. If it does solve the issue for me, I'll mark your answer as Accepted. – Robbert Jan 27 '15 at 08:05
  • 1
    gave you a vote for the help(cv2.xfeatures2d). Worked for me. – Yonatan Simson May 08 '17 at 11:53
7

Install it from pip

Python 2.x

pip install opencv-contrib-python

Python 3.x

pip3 install opencv-contrib-python

Use sudo if a permsision error occurred.

gtzinos
  • 1,205
  • 15
  • 27
  • I cannot find this package. Also 'pip3 list | grep opencv' does not contain it. Any ideas what am I missing? – mapto Feb 20 '18 at 17:54
  • 1
    Actually, [found it](https://pypi.python.org/pypi/opencv-contrib-python), not sure why it failed the first time. – mapto Feb 20 '18 at 18:00
4

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
  • 1
    Thanks for chiming in Jprog. I did end up using 2.4 for now (2.4.10 actually). I won't mark this as an accepted answer though, as it does not solve the issue in OpenCV 3. – Robbert Jan 13 '15 at 09:16
2

you are missing the new, additional namespace:


>>> help(cv2.xfeatures2d)
Help on module cv2.xfeatures2d in cv2:

NAME
    cv2.xfeatures2d

FILE
    (built-in)

FUNCTIONS
    SIFT(...)
        SIFT([, nfeatures[, nOctaveLayers[, contrastThreshold[, edgeThreshold[,
sigma]]]]]) -> <xfeatures2d_SIFT object>

    SURF(...)
        SURF([hessianThreshold[, nOctaves[, nOctaveLayers[, extended[, upright]]
]]]) -> <xfeatures2d_SURF object>

    StarDetector(...)
        StarDetector([, _maxSize[, _responseThreshold[, _lineThresholdProjected[
, _lineThresholdBinarized[, _suppressNonmaxSize]]]]]) -> <xfeatures2d_StarDetect
or object>

DATA
    FREAK_NB_ORIENPAIRS = 45
    FREAK_NB_PAIRS = 512
    FREAK_NB_SCALES = 64


>>> surf = cv2.xfeatures2d.SURF(300)
berak
  • 39,159
  • 9
  • 91
  • 89
  • Thanks @berak (you seem to answer all OpenCV questions I come across). I have tried `xfeatures2d` but it gives the same error - no such attribute - on both Python 2 and 3. It also doesn't explain why PyCharm is autocompleting correctly. – Robbert Sep 26 '14 at 12:23
  • can it be, you got *2* pythons in your box ? – berak Sep 26 '14 at 12:25
  • Correct, I have both Python 2.7 and 3.4, just to test which version works, since OpenCV 3 is still in alpha. Both are able to load OpenCV correctly (though I have to change `EXPORTPATH` accordingly) and call regular `cv2` properties. – Robbert Sep 26 '14 at 12:28
  • PyCharm was probably picking up autocomplete from an older OpenCV installation, considering the new namespace in OpenCV 3. When I reload the modules in PyCharm, autocomplete no longer works. – Robbert Sep 26 '14 at 12:45
  • do you actually *have* an xfeatures2d module inside cv2 [help(cv2)] ? – berak Sep 27 '14 at 08:33
  • No, it's not listed there. I'm wondering whether I've built the files correctly. Is there any additional step I need to take apart from the two make parameters I supplied in my answer? – Robbert Sep 27 '14 at 12:20
  • please check, if the c++ xfeatures2d module was built at all. it could be disabled (by dependancy), if you did not have cuda. – berak Sep 27 '14 at 12:22
  • 1
    I've been dealing with this issue myself. It looks like `xfeatures2d` is flat-out broken in the alpha/beta releases. It's building properly (at least according to the build log), but the python API is not built at all. – Fake Name Nov 29 '14 at 00:26