10

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?

4

2 に答える 2

18

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
于 2012-09-23T16:54:29.553 に答える
0

この質問への回答を参照してください。

EstebanAngeeの回答からの関連コードは次のとおりです。

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

パラメータ辞書を作成します。

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

最近傍検索を実行します。

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]) )

一致した記述子を返します。

return pairs[mask]

私は現在ワークステーションにいないので、コードの何が問題になっているのかわかりませんが、同じ問題が発生したときに上記の質問ですべての問題が解決しました。あなたは使う必要はありませんFlannBasedMatcher、私もそれに問題があったことを覚えています。

それでも解決しない場合は、明日かそこらで解決策を見つけることができるかどうかを確認します。

于 2012-09-20T16:45:37.150 に答える