Python 2.7 中的 OpenCV 3 在使用蛮力匹配器时出错
OpenCV 3 in Python 2.7 gives error while using Brute-Force Matcher
我在 Windows 上使用带有 Python 2.7 的 OpenCV 3。
这是我正在使用的代码:
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 描述符实现蛮力特征匹配时,在 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
可能是什么原因?
根据this,你应该这样做:
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], flags=2)
plt.imshow(img3),plt.show()
我在 Windows 上使用带有 Python 2.7 的 OpenCV 3。
这是我正在使用的代码:
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 描述符实现蛮力特征匹配时,在 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
可能是什么原因?
根据this,你应该这样做:
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], flags=2)
plt.imshow(img3),plt.show()