Ask Your Question
0

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

asked 2017-12-25 06:38:39 -0600

Sakthivel S gravatar image

updated 2017-12-26 02:32:46 -0600

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 ...
(more)
edit retag flag offensive close merge delete

Comments

how did you install your cv2 ?

berak gravatar imageberak ( 2017-12-25 06:42:47 -0600 )edit

I didn't install separately, just followed following link to install openCV for both c++ and Python http://www.codebind.com/linux-tutoria...

Sakthivel S gravatar imageSakthivel S ( 2017-12-25 06:58:01 -0600 )edit

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 ?

berak gravatar imageberak ( 2017-12-25 07:12:29 -0600 )edit

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

supra56 gravatar imagesupra56 ( 2017-12-25 07:54:19 -0600 )edit
1

Added cmake console output in my original question

Sakthivel S gravatar imageSakthivel S ( 2017-12-26 02:33:11 -0600 )edit

I have rebuilt latest opencv with latest opencv-contrib. But, still facing same issue. Is there any other to solve this issue ?

Sakthivel S gravatar imageSakthivel S ( 2017-12-26 05:15:59 -0600 )edit

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)

berak gravatar imageberak ( 2017-12-26 05:19:42 -0600 )edit

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

cv2.__version__ '3.4.0-dev'

3Dreconstruction(master)$ python3 Python 3.5.2 (default) >>> import cv2

cv2.__version__ '3.3.0'

Sakthivel S gravatar imageSakthivel S ( 2017-12-27 07:27:56 -0600 )edit

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)

berak gravatar imageberak ( 2017-12-27 07:33:54 -0600 )edit

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

Sakthivel S gravatar imageSakthivel S ( 2017-12-27 08:05:07 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-12-27 08:20:11 -0600

Sakthivel S gravatar image

After removing cv2 from site-packages, solved xfeatures2d issue. Thanks for your support.

edit flag offensive delete link more

Comments

Can you tell me from which site-packages?

Well, in this folder /usr/local/lib/python3.5/site-packages, I have only one cv2.so. Should I remove it?

Riya208 gravatar imageRiya208 ( 2018-05-10 09:51:57 -0600 )edit

i do not have any cv2 folder or file in site-pakages still facing same issue.. what should i do?

jaisan gravatar imagejaisan ( 2019-06-20 06:46:45 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-25 06:38:39 -0600

Seen: 42,315 times

Last updated: Dec 26 '17