Error when using knnMatch with OpenCV + Python

I want to map two pictures using Python + OpenCV. I used SURF to extract key points and descriptors from both. Now I need to map 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) 

An 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 can I use knnMatch correctly? This is mistake?

+10
source share
2 answers

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

 raw_matches=matcher.knnMatch(np.asarray(desc1,np.float32),np.asarray(desc2,np.float32), 2) #2 
+10
source

See the answer to this question .

Here is the relevant code from Esteban Angee :

 r_threshold = 0.6 FLANN_INDEX_KDTREE = 1 # bug: flann enums are missing 

Build a parameter dictionary:

 flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 4) flann = cv2.flann_Index(desc2, flann_params) 

Search for nearby neighbors:

 idx2, dist = flann.knnSearch(desc1, 2, params = {}) # bug: need to provide empty dict mask = dist[:,0] / dist[:,1] < r_threshold idx1 = np.arange(len(desc1)) pairs = np.int32( zip(idx1, idx2[:,0]) ) 

Returns descriptors that match:

 return pairs[mask] 

I'm not on my workstation right now, so I'm afraid I can't look at what is wrong with your code, but this question solved all my problems when I had the same problem. You do not need to use FlannBasedMatcher , I remember that I also had problems with it.

If this does not help, I will see if I can find my solution tomorrow or so.

0
source

Source: https://habr.com/ru/post/1436604/


All Articles