Laurel Laurel - 6 months ago 126
Python Question

OpenCV 3 in Python 2.7 gives error while using Brute-Force Matcher

I'm using OpenCV 3 with Python 2.7 on Windows.

Here's the code I'm working with:

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)


While trying to implement Brute Force feature matching using ORB descriptors, I get the following error after 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


What could be the reason ?

Answer

According to this, you should do:

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