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'
1
  • Could you consider editing the original Question since the answer does not reflect the question in the title. Commented Mar 2, 2017 at 6:08

4 Answers 4

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!

Sign up to request clarification or add additional context in comments.

4 Comments

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation.
Thanks @RiggsFolly, but this could actually, potentially, answer the question.
@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.
gave you a vote for the help(cv2.xfeatures2d). Worked for me.
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.

2 Comments

I cannot find this package. Also 'pip3 list | grep opencv' does not contain it. Any ideas what am I missing?
Actually, found it, not sure why it failed the first time.
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()
(...)

1 Comment

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.
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)

8 Comments

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.
can it be, you got 2 pythons in your box ?
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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.