Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using knnMatch with OpenCV+Python

I want to match two pictures using Python+OpenCV. I have used SURF to extract keypoints and descriptors from both of them. Now, I need to match these descriptors and for this reason I decided to use Flann Matcher.

flann_params = dict(algorithm = FLANN_INDEX_KDTREE,trees = 4)    
matcher = cv2.FlannBasedMatcher(flann_params, {})

But when I try to use knnMatch with descriptors (desc1, desc2), openCV throws an exception.

raw_matches=matcher.knnMatch(np.asarray(desc1),np.asarray(desc2), 2)

The exception is the following:

raw_matches=matcher.knnMatch(np.asarray(desc1),np.asarray(desc2), 2) #2
cv2.error: /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.4.2/modules/flann/src/miniflann.cpp:299: error: (-210) type=6
 in function buildIndex_

How I could use knnMatch correctly? Is it a Bug?

like image 244
Sergio Avatar asked Sep 20 '12 08:09

Sergio


1 Answers

I solved this problem using the correct data type with the function np.asarray()

raw_matches=matcher.knnMatch(np.asarray(desc1,np.float32),np.asarray(desc2,np.float32), 2) #2
like image 54
Sergio Avatar answered Sep 20 '22 03:09

Sergio