Module 'cv2' has no attribute 'xfeatures2d' (Example 4.3)

I am working on chapter 4 and am getting the following alarm, i actually dont understand why i am getting it. as i can see the the predictive text does find this function?

here is the code:

#!/usr/bin/env python

import cv2
import numpy as np

image = cv2.imread('/home/user/catkin_ws/src/opencv_for_robotics_images/Unit_4/Course_images/test_e.jpg')

gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

fast = cv2.FastFeatureDetector_create() 

brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()

keypoints = fast.detect(gray, None)    
brief_keypoints, descriptor = brief.compute(gray, keypoints)

brief = np.copy(image)
non_brief = np.copy(image)

# Draw keypoints on top of the input image
cv2.drawKeypoints(image, brief_keypoints, brief, color=(0,250,250))
cv2.drawKeypoints(image, keypoints, non_brief, color=(0,35,250))

cv2.imshow('Fast corner detection',non_brief)
cv2.imshow('BRIEF descriptors',brief)


and the full error:

user:~/catkin_ws/src/unit4/scripts$ python3
Traceback (most recent call last):
  File "", line 13, in <module>
    brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()
AttributeError: module 'cv2' has no attribute 'xfeatures2d'

a little more info, if i try and import this moduel it will not do it:

from cv2 import xfeatures2d

#i get this error, so maybe there is an isue with cv2 libraries or something

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'xfeatures2d' from 'cv2' (/usr/lib/python3/dist-packages/

Hello @ziga.rupret ,

Please try running the following command before executing the example:

pip install opencv-contrib-python

this solved the problem, thanks!

1 Like