Laurel Laurel - 1 year ago 225
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)

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>
File "C:\Python27\lib\site-packages\matplotlib\", 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\", line 6734, in imshow
File "C:\Python27\lib\site-packages\matplotlib\", 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 Source

According to this, you should do:

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