AttributeError: 'module' object has no attribute 'xfeatures2d' [Python/OpenCV 2.4]
Asked Answered
C

14

60

This line:

sift = cv2.xfeatures2d.SIFT_create()

return error:

Traceback (most recent call last):
  File "C:/Python27/openCVskrypty/GUI/SOLUTION2.py", line 11, in <module>
    sift = cv2.xfeatures2d.SIFT_create()
AttributeError: 'module' object has no attribute 'xfeatures2d'

I read something about this error and it appears in OpenCV version 3.0. This is quite weird because I have 2.4.11 version.

I check dir(cv2) and I haven't got xfeatures2d module. Does anyone know why? Can I download it separately?

Thanks for help how fix this.

Cioban answered 4/5, 2016 at 22:36 Comment(0)
D
68

I think you should install opencv-contrib-python instead. The module you're using is not support in opencv-python. See opencv-contrib-python.

To install:

pip install opencv-contrib-python
Diarchy answered 9/4, 2018 at 14:35 Comment(3)
This is likely the right solution for those using OpenCV 3+ who see the AttributeError: 'module' object has no attribute 'xfeatures2d'Mansion
have you tried to uninstall the previous OpenCV first?Diarchy
Worked for me but got this warning - [ WARN:[email protected]] global shadow_sift.hpp:13 SIFT_create DEPRECATED: cv.xfeatures2d.SIFT_create() is deprecated due SIFT tranfer to the main repository. github.com/opencv/opencv/issues/16736Bethink
H
34

SIFT is a patented algorithm, hence not available in each open-cv version. What you can do is install opencv and its contrib part simultaneously, i.e,

pip install opencv-python==3.3.0.10 opencv-contrib-python==3.3.0.10

SIFT worked fine for me on above versions of opencv.

Hals answered 16/9, 2018 at 21:3 Comment(5)
You can even go up to 3.4.2.17 and still use SIFT.Zonda
At least for me, cv2.xfeatures2d.SIFT_create() only work on 3.3.0.10Brahui
ERROR: No matching distribution found for opencv-python==3.3.0.10Homeomorphism
Version 3.3.0.10 wasn't available now, next version up was 3.4.2.16 and was able to work with sift=cv2.xfeatures2d.SIFT_create() . I also had to do pip3 install opencv-python sudo apt-get install libatlas-base-dev sudo apt-get install libjasper-dev sudo apt-get install libqtgui4 sudo apt-get install python3-pyqt5 sudo apt install libqt4-testChon
Is available in newer versions just by using cv2.SIFT_create(). I am using opencv 4.5.4 rnNeodarwinism
I
24

For CV2 Version 4.5.1, this works

sift = cv2.SIFT_create()
kp = sift.detect(gimg,None)
img=cv2.drawKeypoints(gimg,kp,img)
plt.imshow(img)
Illyria answered 19/1, 2021 at 11:36 Comment(4)
github.com/HeatherJiaZG/SuperGlue-pytorch/issues/13Gusset
Works for me as well with opencv-python==4.5.3.56 and opencv-contrib-python==4.5.3.56Geyer
@Usama what is gimg?Unavoidable
@SyedHussain It is the gray Image.Illyria
Y
14

After executing the command:

pip install opencv-contrib-python

, I got the following error:

error: OpenCV(4.0.0) /Users/rene/build/skvark/opencv-python/opencv_contrib/modules/xfeatures2d/src/sift.cpp:1207: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'create'

Could solve it with the following command in anaconda:

conda install -c menpo opencv

Or with pip:

pip install opencv-python==3.4.2.17

pip install opencv-contrib-python==3.4.2.17
Yorick answered 3/4, 2019 at 8:34 Comment(3)
No matching distribution foundChon
should be pip install opencv-python==3.4.2.17 and pip install opencv-contrib-python==3.4.2.17Pudency
Do install 3.4.2.16 if you want to use SIFT or SURF. (Tested) #52306078Nettle
X
5

I got this error and all I did was to uninstall opencv packages and install them in the following order.

STEPS

open Anaconda Prompt by running as administrator and type the following commands.

pip uninstall opencv-python

pip uninstall opencv-contrib-python

Then type the following commands

pip install opencv-contrib-python==3.4.2.16

pip install opencv-python==3.4.2.16

This solved my problem. Hope this solves yours!!😁👍

Xavier answered 19/6, 2019 at 12:38 Comment(0)
P
4

In latest CV2 Version 4.5.3.56, this works:

img1 = cv2.imread(r'C:\Users\CW\Desktop\new_img.png') 
sift = cv2.SIFT_create() 
kp1, des1 = sift.detectAndCompute(img1, None)
Posting answered 11/10, 2021 at 4:20 Comment(1)
Code only answers are not considered good practice. Please consider Explaining how this answers the questionSwingeing
T
4

Use sift = cv2.SIFT_create() instead of cv2.xfeatures2d.SIFT_create() as sift patent has expired now, so OpenCV has moved SIFT_create() directly to main repository instead of contrib module in versions > 4.4 , see this

Thereinafter answered 11/8, 2022 at 19:20 Comment(0)
L
3

I used to have similar problem as @srihegde said you can try to uninstall opencv-contrib-python package and reinstall again. You can also try to uninstall opencv-python package if you have one, since it might mess with the packages too.

This helped for me.

Uninstall:

pip3 uninstall opencv-contrib-python
pip3 uninstall opencv-python

And then install:

pip3 install opencv-contrib-python
pip3 install opencv-python
Luckin answered 25/2, 2019 at 18:35 Comment(1)
I only need the install part:Chon
T
3

I got the same error... I have used cv2.__version__ and cv2.__path__ to check the opencv version and path. Then I removed cv2 from site-packages. and install the following

pip install opencv-python==3.4.2.17

pip install opencv-contrib-python==3.4.2.17
Terpsichore answered 20/5, 2019 at 9:59 Comment(3)
This is the order of installation which works for the latest version.Ethaethan
No matching distribution foundChon
You have to modify the distribution version of open cv with the latest one. Presently its 4.1.2.30.Terpsichore
S
2

The problem is with your version of OpenCV. You say you're on version 2.4.11 but this version of OpenCV doesn't have this method available to it.

You can check the documentation. It has features2d

Whereas OpenCV 3.0 does.

Standstill answered 4/5, 2016 at 22:42 Comment(1)
Thanks for answer. Can I download this module separately whether I have to download v.3.0 OpenCV?Cioban
P
1

This error may also occur in OpenCV 3+ as it is caused by mismatched versions of OpenCV and OpenCV-Contrib package.

I had OpenCV version 3.4.1 and OpenCV-Contrib version 3.4.0. I tried the following with OpenCV-Contrib:

Uninstall OpenCV-Contrib package:

$ pip uninstall opencv-contrib-python

Then install the same again:

$ pip install opencv-contrib-python

The pip automatically fetches and installs the latest compatible version.

Perretta answered 15/7, 2018 at 13:17 Comment(0)
C
0

It doesn't work for OpenCV 4.0 due to US patent matter. Perhaps we shall give it a thumb up for this OpenSift effort:

https://github.com/robwhess/opensift

Cubiculum answered 8/3, 2020 at 20:18 Comment(0)
R
0

Open a Powershell Prompt and type the following command:

pip install --user opencv-contrib-python

Fixed it for me.

Open an Ananconda Powershell Prompt if you are using Jupyter Notebook.

Retardation answered 15/1, 2021 at 7:28 Comment(0)
H
0
pip install opencv-contrib-python==4.5.1.48

working on win10

Huntley answered 7/5 at 8:4 Comment(1)
You can improve your answer if you edit it to make it stand out from the 13 existing answers that all suggest to use the same command with a different version string.Lamont

© 2022 - 2024 — McMap. All rights reserved.