https://stackoverflow.com/questions/26059134/adding-modules-from-opencv-contrib-to-opencv

https://stackoverflow.com/questions/26059134/adding-modules-from-opencv-contrib-to-opencv


Adding modules from opencv_contrib to OpenCV

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

4 Answers

up vote 15 down vote accepted

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!

  • This does not really answer the question. If you have a different question, you can ask it by clickingAsk Question. You can also add a bounty to draw more attention to this question once you have enough reputation. –  RiggsFolly  Jan 26 '15 at 18:38
  • 2
    Thanks @RiggsFolly, but this could actually, potentially, answer the question. –  Robbert  Jan 27 '15 at 8: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 8:05
  • 1
    gave you a vote for the help(cv2.xfeatures2d). Worked for me. –  Yonatan Simson  May 8 '17 at 11:53

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
    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 9:16

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.

  • I cannot find this package. Also 'pip3 list | grep opencv' does not contain it. Any ideas what am I missing? –  mapto  Feb 20 at 17:54
  • Actually, found it, not sure why it failed the first time. –  mapto  Feb 20 at 18:00

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)
  • Thanks @berak (you seem to answer all OpenCV questions I come across). I have triedxfeatures2d 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
  • 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 0:26


http://blog.csdn.net/firemicrocosm/article/details/48413763

http://blog.csdn.net/firemicrocosm/article/details/49307869

python3.0 以上版本配合opencv3.0以上的版本

SIFT及SURF等都被集成到contrib中,所以与之前的版本要区分开

[python]  view plain  copy
  1. import cv2  
  2. import numpy as np  
  3.   
  4. img = cv2.imread('lena.jpg')  
  5.   
  6. gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)  
  7.   
  8. siftDetector = cv2.xfeatures2d.SIFT_create(100)  
  9. kp,res = siftDetector.detectAndCompute(gray,None)  
  10. print(len(kp))  
  11. print(res)  
  12.   
  13. img = cv2.drawKeypoints(img,outImage=img,keypoints=kp, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)  
  14.   
  15. cv2.imshow("sift",img)  
  16. cv2.waitKey(0)  
  17. cv2.destroyAllWindows()  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值