Python 2.7中的OpenCV 3在使用Brute-Force Matcher时出错

时间:2015-08-05 12:58:48

标签: python opencv computer-vision opencv3.0

我在Windows上使用OpenCV 3和Python 2.7。

这是我正在使用的代码:

import numpy as np
import cv2
from matplotlib import pyplot as plt
img1 = cv2.imread('feature.jpg',0)  
img2 = cv2.imread('large-pic.jpg',0)
orb = cv2.ORB_create()
# cv2.ORB() doesn't work as mentioned in the documentation

kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
matches = sorted(matches, key = lambda x:x.distance)

img3 = np.zeros((1,1))
img4 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],img3,flags=2)
plt.imshow(img4)

在尝试使用ORB描述符实现Brute Force功能匹配时,在 plt.imshow(img4)之后出现以下错误:

Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    plt.imshow(img4)
   File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2368, in imshow
    ret = ax.imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 6734, in imshow
im.set_data(X)
  File "C:\Python27\lib\site-packages\matplotlib\image.py", line 412, in set_data
    raise TypeError("Image data can not convert to float")
TypeError: Image data can not convert to float

可能是什么原因?

1 个答案:

答案 0 :(得分:1)

根据this,你应该这样做:

img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], flags=2)
plt.imshow(img3),plt.show()