問題描述
我正在嘗試重寫此處中描述的代碼.使用 Opencv 的 python API.
I am trying to rewrite the code described here. using the python API for Opencv.
代碼的第 3 步有這幾行:
The step 3 of the code has this lines:
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );
我在 OpenCV 參考 中反復查看,但沒有發現與 python 中的 FlannBasedMatcher 或其他對象相關的內容哪個可以完成這項工作.
I have looked over and over in the OpenCV reference but found nothing related to a FlannBasedMatcher in python or some other object which can do the work.
有什么想法嗎?
注意:我使用 OpenCV 2.3.1 和 Python 2.6
NOTE: I am usign OpenCV 2.3.1 and Python 2.6
推薦答案
查看python2文件夾下OpenCV 2.3.1提供的示例,我發現了一個基于flann的匹配函數的實現,它不依賴于FlanBasedMatcher對象.
Looking in the examples provided by OpenCV 2.3.1 under the python2 folder, I found an implementation of a flann based match function which doesn't rely on the FlanBasedMatcher object.
代碼如下:
FLANN_INDEX_KDTREE = 1 # bug: flann enums are missing
flann_params = dict(algorithm = FLANN_INDEX_KDTREE,
trees = 4)
def match_flann(desc1, desc2, r_threshold = 0.6):
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]
這篇關于OpenCV python的API:FlannBasedMatcher的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!