Python3 : AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d'
I'm trying to execute a example open source program. But, getting error in library. Using OpevCV 3.4.0.
Issue :
3Dreconstruction(master)$ python3 example.py Traceback (most recent call last): File "example.py", line 90, in <module> points1, points2, intrinsic = dino() File "example.py", line 68, in dino pts1, pts2 = features.find_correspondence_points(img1, img2) File "/home/aximsoft/Computer Vision/Existing Examples/3Dreconstruction/features.py", line 6, in find_correspondence_points sift = cv2.xfeatures2d.SIFT_create() AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d'
Source Code :Open Source Link
example.py
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import cv2
from camera import Camera
import structure
import processor
import features
# Download images from http://www.robots.ox.ac.uk/~vgg/data/data-mview.html
def house():
input_path = 'imgs/house/'
camera_filepath = 'imgs/house/3D/house.00{0}.P'
cameras = [Camera(processor.read_matrix(camera_filepath.format(i)))
for i in range(9)]
[c.factor() for c in cameras]
points3d = processor.read_matrix(input_path + '3D/house.p3d').T # 3 x n
points4d = np.vstack((points3d, np.ones(points3d.shape[1]))) # 4 x n
points2d = [processor.read_matrix(
input_path + '2D/house.00' + str(i) + '.corners') for i in range(9)]
index1 = 2
index2 = 4
img1 = cv2.imread(input_path + 'house.00' + str(index1) + '.pgm') # left image
img2 = cv2.imread(input_path + 'house.00' + str(index2) + '.pgm')
# fig = plt.figure()
# ax = fig.gca(projection='3d')
# ax.plot(points3d[0], points3d[1], points3d[2], 'b.')
# ax.set_xlabel('x axis')
# ax.set_ylabel('y axis')
# ax.set_zlabel('z axis')
# ax.view_init(elev=135, azim=90)
# x = cameras[index2].project(points4d)
# plt.figure()
# plt.plot(x[0], x[1], 'b.')
# plt.show()
corner_indexes = processor.read_matrix(
input_path + '2D/house.nview-corners', np.int)
corner_indexes1 = corner_indexes[:, index1]
corner_indexes2 = corner_indexes[:, index2]
intersect_indexes = np.intersect1d(np.nonzero(
[corner_indexes1 != -1]), np.nonzero([corner_indexes2 != -1]))
corner_indexes1 = corner_indexes1[intersect_indexes]
corner_indexes2 = corner_indexes2[intersect_indexes]
points1 = processor.cart2hom(points2d[index1][corner_indexes1].T)
points2 = processor.cart2hom(points2d[index2][corner_indexes2].T)
height, width, ch = img1.shape
intrinsic = np.array([ # for imgs/house
[2362.12, 0, width / 2],
[0, 2366.12, height / 2],
[0, 0, 1]])
return points1, points2, intrinsic
def dino():
# Dino
img1 = cv2.imread('imgs/dinos/viff.003.ppm')
img2 = cv2.imread('imgs/dinos/viff.001.ppm')
pts1, pts2 = features.find_correspondence_points(img1, img2)
points1 = processor.cart2hom(pts1)
points2 = processor.cart2hom(pts2)
fig, ax = plt.subplots(1, 2)
ax[0].autoscale_view('tight')
ax[0].imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
ax[0].plot(points1[0], points1[1], 'r.')
ax[1].autoscale_view('tight')
ax[1].imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
ax[1].plot(points2[0], points2[1], 'r.')
fig.show()
height, width, ch = img1.shape
intrinsic = np.array([ # for dino
[2360, 0, width / 2],
[0, 2360, height / 2],
[0, 0, 1]])
return points1, points2, intrinsic
points1, points2, intrinsic = dino()
# Calculate essential matrix with 2d points.
# Result will be up to a scale
# First, normalize points
points1n = np.dot(np.linalg.inv(intrinsic), points1)
points2n = np.dot(np.linalg.inv(intrinsic), points2)
E = structure.compute_essential_normalized(points1n, points2n)
print('Computed essential matrix:', (-E / E[0][1 ...
how did you install your cv2 ?
I didn't install separately, just followed following link to install openCV for both c++ and Python http://www.codebind.com/linux-tutoria...
do you see any other modules from opencv_contrib, when you do a
help(cv2)
? like face, bgsegm,ximgproc ?since you already build with the opencv_contrib modules, we must assume, something went wrong with your build (or you have multiple cv2 /python installs on your box)
the code you're trying to use, is entirely irrelevant now, but canwe see your cmake - console output ?
I have no issues problem. Using OpenCV 3.4.0 and python 3.5 for raspberry pi 3. There is something you can take a look if this could help you....cv2.xfeatures2d
Added cmake console output in my original question
I have rebuilt latest opencv with latest opencv-contrib. But, still facing same issue. Is there any other to solve this issue ?
did you run a proper
make install
? did you look into your python/site-packes, and is it there ?cv2.__version__ will show, what you're actually using. (and it's probably not, what you expect)
Yes. I have installed properly. Here update python2 and python3 cv2 versions. python3 has old version. How can I install cv2 latest to python3 ?
3Dreconstruction(master)$ python Python 2.7.12 (default) >>> import cv2
3Dreconstruction(master)$ python3 Python 3.5.2 (default) >>> import cv2
look into your site-packages, there are probably 2 cv2.so's remove the older (or, if there's a cv2 folder, remove that (it'spropably from pip)
default site packages has nothing
(root)$ ls /usr/lib/python3.5/site-packages/ boost
There is site-package inside home path, should I have to remoe that cv2 ?
(root)$ ls /home/usersoft/.local/lib/python3.5/site-packages/ cv2 matplotlib numpy OpenGL