7

I have opencv 3.4.3 installed (using pip3 install opencv-python and pip3 install opencv-python-contrib)

When I run a code containing this line:
sift = cv2.xfeatures2d.SIFT_create()
I got this error:

AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d'

Is xfeatures2d function not anymore supported by opencv 3.4.3?

singrium
  • 2,746
  • 5
  • 32
  • 45
  • Do you put `from cv2 import *` ? – goodahn Oct 05 '18 at 16:20
  • I put `import cv2` in the beginning of my code – singrium Oct 05 '18 at 16:21
  • Try dropping the `cv2` from `cv2.xfeatures2d.SIFT_create()`. Looking at the documentation, everything references `cv.xfeature2d` rather than 'cv2' – artomason Oct 05 '18 at 16:23
  • I think `xfeatures2d` function is currently supported because we can check at [opencv3.4.3 documentation](https://docs.opencv.org/3.4.3/index.html). – goodahn Oct 05 '18 at 16:25
  • 3
    The OpenCV pip installer via `pip install opencv-python-contrib` do *not* have the non-free modules such as SIFT anymore. You will have to manually compile OpenCV to get access to those. Alternatively you can find an installer elsewhere that includes them. – alkasm Oct 06 '18 at 00:09
  • @artomason I dropped `cv2` from `cv2.xfeatures2d.SIFT_create()` to be `xfeatures2d.SIFT_create()` But I got the same error. – singrium Oct 06 '18 at 13:14
  • @goodahn Yeah, that's what I thought when I read the documentation, but When I used it in my code, it didn't work. And as I read about this in the github issues, I found that opencv does not have the non-free mdules anymore as said Alexander Reynolds – singrium Oct 06 '18 at 13:17
  • @AlexanderReynolds I'll try to downgrade Opencv and install a lower version. I think this will solve the problem, isn't it? – singrium Oct 06 '18 at 13:18
  • I believe the older versions will not have this "fix" in it, so you should be OK. Don't go too far back though! :) – alkasm Oct 06 '18 at 21:43
  • 1
    After discussions with @John_Sharp1318 who has answered below, my answer is incorrect! His answer below is indeed the actual issue, and you should probably accept it instead for others who have your same issue. However, you will additionally run into the problem stated in my question with 3.4.3 anyways, which is stated in [this question](https://stackoverflow.com/questions/52574336/opencv-xfeatures2d-surf-213the-function-feature-is-not-implemented/52686193#52686193). – alkasm Oct 07 '18 at 07:05
  • I have the same problem, sadly on kaggle. I cannot do anything. – Max Apr 21 '21 at 14:56

4 Answers4

11

The error message you have is related to the fact that the module xfeatures2d does not exist. It is not directly related to SIFT algorithm nor any algorithm in xfeatures2d (all will send that error). I suggest you to either reinstall opencv-contrib-python(pip install opencv-contrib-python) or if you are using anaconda or equivalent to re-install the two opencv package from another source repository. A last option consist to compile the full OpenCV ("regular" + contrib) by yourself if you are comfortable with it.

Hope it helps.

John_Sharp1318
  • 939
  • 8
  • 19
10

After hours of hair pulling and installing / compiling everything from the scratch, I needed to post this for other people who might be doing the same little stupid mistake I was doing.

If you have both opencv-python and opencv-contrib-python installed in your system and still getting this error, instead of

 sift = cv2.xfeatures2d.SIFT_create()

try

 sift = cv2.SIFT_create()
Ege Akat
  • 101
  • 1
  • 3
6

EDIT: My answer was incorrect. Please see the other answer below.

The issue stated in my answer was the solution to a different problem (answered here). However, since you're using OpenCV 3.4.3, I believe you will also have that different problem as well. The original text that was in this answer is now on that other answer, since it is the correct answer to that question.

alkasm
  • 22,094
  • 5
  • 78
  • 94
3

Try this in your conda prompt.

pip3 uninstall opencv-python
pip3 install -U opencv-contrib-python==3.4.2.16
Lakpa Tamang
  • 408
  • 1
  • 4
  • 12
  • This worked for me: pip3 uninstall opencv-python and then pip3 install -U opencv-contrib-python – spatak Jul 10 '21 at 18:40