[英]OpenCV feature matching for multiple images
How can I optimise the SIFT feature matching for many pictures using FLANN? 如何使用FLANN优化许多图片的SIFT功能匹配?
I have a working example taken from the Python OpenCV docs. 我有一个从Python OpenCV文档中获取的工作示例。 However this is comparing one image with another and it's slow. 然而,这是将一个图像与另一个图像进行比较而且速度很慢。 I need it to search for features matching in a series of images (a few thousands) and I need it to be faster. 我需要它来搜索一系列图像(几千个)中匹配的特征,我需要它更快。
My current idea: 我目前的想法:
http://docs.opencv.org/trunk/doc/py_tutorials/py_feature2d/py_feature_homography/py_feature_homography.html http://docs.opencv.org/trunk/doc/py_tutorials/py_feature2d/py_feature_homography/py_feature_homography.html
import sys # For debugging only import numpy as np import cv2 from matplotlib import pyplot as plt MIN_MATCH_COUNT = 10 img1 = cv2.imread('image.jpg',0) # queryImage img2 = cv2.imread('target.jpg',0) # trainImage # Initiate SIFT detector sift = cv2.SIFT() # find the keypoints and descriptors with SIFT kp1, des1 = sift.detectAndCompute(img1,None) kp2, des2 = sift.detectAndCompute(img2,None) FLANN_INDEX_KDTREE = 0 index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) search_params = dict(checks = 50) flann = cv2.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(des1,des2,k=2) # store all the good matches as per Lowe's ratio test. good = [] for m,n in matches: if m.distance MIN_MATCH_COUNT: src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2) dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0) matchesMask = mask.ravel().tolist() h,w = img1.shape pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,M) img2 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.LINE_AA) else: print "Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT) matchesMask = None draw_params = dict(matchColor = (0,255,0), # draw matches in green color singlePointColor = None, matchesMask = matchesMask, # draw only inliers flags = 2) img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params) plt.imshow(img3, 'gray'),plt.show()
UPDATE UPDATE
After trying out many things I might have come closer to the solution now. 在尝试了很多东西后,我现在可能已经接近解决方案了。 I hope it's possible to build the index and then search in it like this: 我希望有可能构建索引,然后在其中搜索如下:
flann_params = dict(algorithm=1, trees=4) flann = cv2.flann_Index(npArray, flann_params) idx, dist = flann.knnSearch(queryDes, 1, params={})
However I still haven't managed to build an accepted npArray to the flann_Index parameter. 但是我仍然没有设法为flann_Index参数构建一个接受的npArray。
loop through all images as image: npArray.append(sift.detectAndCompute(image, None)) npArray = np.array(npArray)
I never solved this in Python, however I switched environment to C++ where you get more OpenCV examples and don't have to use a wrapper with less documentation. 我从来没有在Python中解决这个问题,但是我将环境转换为C ++,你可以获得更多的OpenCV示例,而不必使用包含较少文档的包装器。
An example on the issue I had with matching in multiple files can be found here: https://github.com/Itseez/opencv/blob/2.4/samples/cpp/matching_to_many_images.cpp 关于我在多个文件中匹配的问题的示例可以在这里找到: https : //github.com/Itseez/opencv/blob/2.4/samples/cpp/matching_to_many_images.cpp
Here are several pieces of my advice: 以下是我的一些建议:
This is a very interesting topic. 这是一个非常有趣的话题。 My ears are opening too. 我的耳朵也开了。
Along with the reply of @stanleyxu2005 I'd like to add some tips as to how to do the whole matching itself since I'm currently working of such a thing. 随着@ stanleyxu2005的回复,我想添加一些关于如何进行整个匹配的提示,因为我目前正在处理这样的事情。
A general recommendation is to look at the stitching process in OpenCV and read the source code. 一般建议是在OpenCV中查看拼接过程并阅读源代码。 The stitching pipeline is a straight forward set of processes and you just have to see how exactly you can implement the single steps. 拼接管道是一组直接的过程,您只需要了解如何实现单个步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.