OpenCV功能匹配多个图像 [英] OpenCV feature matching for multiple images
问题描述
如何使用FLANN为许多图片优化SIFT功能匹配?
How can I optimise the SIFT feature matching for many pictures using FLANN?
我有一个来自Python OpenCV文档的工作示例.但是,这是将一个图像与另一个图像进行比较,而且速度很慢.我需要它来搜索一系列图像(几千张)中匹配的特征,并且我需要它更快.
I have a working example taken from the Python OpenCV docs. 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.
我当前的想法:
- 浏览所有图像并保存功能.怎么样?
- 将相机的图像与上述基础进行比较,然后找到正确的图像.怎么样?
- 将结果,匹配的图像或其他内容提供给我.
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()
更新
尝试了许多事情之后,我现在可能更接近解决方案了.我希望可以建立索引,然后像这样搜索它:
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={})
但是我仍然没有设法为flann_Index参数建立一个可接受的npArray.
However I still haven't managed to build an accepted npArray to the flann_Index parameter.
loop through all images as image:
npArray.append(sift.detectAndCompute(image, None))
npArray = np.array(npArray)
推荐答案
我从来没有在Python中解决过这个问题,但是我将环境切换到了C ++,在那里您可以获得更多的OpenCV示例,而不必使用包装较少的文档.
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.
可以在这里找到有关多个文件匹配问题的示例: https://github.com/Itseez/opencv/blob/2.4/samples/cpp/matching_to_many_images.cpp
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
这篇关于OpenCV功能匹配多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!