AttributeError:模块'cv2.cv2'没有属性'xfeatures2d'?

时间:2019-12-15 06:50:09

标签: python image-processing computer-vision feature-selection cv2

我试图从几天内使用CV2提取特征表图像以检查相似性,并且陷入了这个问题。 尝试了很多解决方案,但没有解决错误。 我的CV2版本:

<module 'cv2.cv2' from '/home/mobin/anaconda3/lib/python3.7/site-packages/cv2/cv2.cpython-37m-x86_64-linux-gnu.so'>

4.1.2

我的代码:

import cv2
import matplotlib.pyplot as plt
print(cv2)
print(cv2.__version__)
original = cv2.imread("/home/mobin/Merging and Visualization of 7-JSON/group_by_rule_test/black_cotton_sweatshirt/0/1-black_cotton_sweatshirt.jpg")
cv2.imshow("original", original)
image_to_compare = cv2.imread("/home/mobin/Merging and Visualization of 7-JSON/group_by_rule_test/black_cotton_sweatshirt/0/2-black_cotton_sweatshirt.jpg")
print('ok here')
# 2) Check for similarities between the 2 images
sift = cv2.xfeatures2d.SIFT_create()

print('ok here1')
kp_1, desc_1 = sift.detectAndCompute(original, None)
kp_2, desc_2 = sift.detectAndCompute(image_to_compare, None)
print('ok here2')

index_params = dict(algorithm=0, trees=5)
search_params = dict()
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(desc_1, desc_2, k=2)
print('ok here3')

good_points = []
ratio = 0.6
for m, n in matches:
    if m.distance < ratio*n.distance:
        good_points.append(m)
        print(len(good_points))



cv2.imshow("result", result)
cv2.imshow("Original", original)
cv2.imshow("Duplicate", image_to_compare)
cv2.waitKey(0)
cv2.destroyAllWindows()

每次我运行此脚本时,都会显示错误:

Traceback (most recent call last):
  File "run_test.py", line 9, in <module>
    sift = cv2.xfeatures2d.SIFT_create()
AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d'

有时候:

Traceback (most recent call last):
  File "run_test.py", line 6, in <module>
    cv2.imshow("original", original)
cv2.error: OpenCV(4.1.2) /io/opencv/modules/highgui/src/window.cpp:651: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

0 个答案:

没有答案