newtostack
newtostack

Reputation: 45

AttributeError: module 'cv2.cv2' has no attribute 'SURF_create' , 2. module 'cv2.cv2' has no attribute 'xfeatures2d'

I'm trying to use SIFT and SURF.
On my laptop I have OpenCV version= 4.5.1.48 and also added OpenCV-contrib-python of version 4.5.1.48
I'm getting an error following the documentation SIFT works perfectly after following documentation but SURF isn't working and giving me error for following codes

code 1

surf = cv.xfeatures2d.SURF_create()   
AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d'

code 2

surf = cv2.SURF_create()   
AttributeError: module 'cv2.cv2' has no attribute 'SURF_create'

After reading many answers on Stack Overflow I changed version of OpenCV and did many things but nothing is working for me

I read about the patent expiring too but nothing is working in my case pls tell me if im wrong somewhere

Upvotes: 4

Views: 15780

Answers (2)

Yuhuang Zhang
Yuhuang Zhang

Reputation: 41

For patent reasons, opencv 4.5.1.48 does not include the whole algorithm

You can use Python3.6 (or Python3.7 maybe OK) and install opencv-pyhton==3.4.2.16 and opencv-contrib-python==3.4.2.16, then you can use the function that:

surf = cv2.xfeatures2d.SURF_create() or sift = cv2.xfeatures2d.SIFT_create()

Upvotes: 3

berak
berak

Reputation: 1374

OpenCV version= 4.5.1.48

this is probably from pypi, and does not contain any "nonfree" code

(SURF is still patented, if you absolutely need that, you have to build from src (with contrib modules), using the OPENCV_ENABLE_NONFREE=ON cmake flag)

however, since the SIFT patent expired last year, use that instead

sift = cv2.SIFT_create() # it's in main, no more xfeatures2d

btw, dont install both opencv-python, and opencv-contrib-python, only the latter (else the former will "shadow" it, and you cannot use contrib modules. this is the reason for AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d')

Upvotes: 13

Related Questions