OpenCV SIFT+ flann多次匹配单键点
我正在尝试使用Open CV匹配关键点。 具体来说,我正在使用“ SIFT”检测器和“ Flann”匹配器。我的代码基于 cv2的文档:
detector = cv2.SIFT_create()
matcher = cv2.FlannBasedMatcher(indexParams=dict(algorithm=0, trees=5), searchParams=dict(checks=50))
kps1, desc1 = detector.detectAndCompute(img1, None)
kps2, desc2 = detector.detectAndCompute(img2, None)
all_matches = matcher.knnMatch(desc1, desc2, 2)
ratio = 0.7
good_matches = []
for m, n in all_matches:
if m.distance <= ratio * n.distance:
good_matches.append(m)
我在good_matches
列表,我有一些超过一个匹配的关键点:
extra_matches = dict()
for match in good_matches:
t_idx = match.trainIdx
reps = [mch for mch in good_matches if mch.trainIdx == t_idx]
if len(reps) > 1 and t_idx not in extra_matches.dict():
extra_matches[t_idx] = reps
print(len(extra_matches)) # not 0
我觉得这很奇怪,因为我认为knnmatch
已经产生了2个最佳匹配。在比率宣传比赛之后,为什么我要有超过单个匹配点?
I'm trying to match keypoints using open cv.
Specifically, I'm using the "sift" detector and "flann" matcher. My code is based on cv2's documentation:
detector = cv2.SIFT_create()
matcher = cv2.FlannBasedMatcher(indexParams=dict(algorithm=0, trees=5), searchParams=dict(checks=50))
kps1, desc1 = detector.detectAndCompute(img1, None)
kps2, desc2 = detector.detectAndCompute(img2, None)
all_matches = matcher.knnMatch(desc1, desc2, 2)
ratio = 0.7
good_matches = []
for m, n in all_matches:
if m.distance <= ratio * n.distance:
good_matches.append(m)
I noticed than even within the good_matches
list, I have some keypoints that have more than a single match:
extra_matches = dict()
for match in good_matches:
t_idx = match.trainIdx
reps = [mch for mch in good_matches if mch.trainIdx == t_idx]
if len(reps) > 1 and t_idx not in extra_matches.dict():
extra_matches[t_idx] = reps
print(len(extra_matches)) # not 0
I find this weird because I thought that knnMatch
already yields the 2 best matches. Why would I have more than a single match per keypoint after ratio-pruning the matches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
果然,发帖五分钟后,我找到了答案:
flann
不进行交叉检查,这意味着我将重复第二个关键点,但对于第1个关键点,我没有重复(在我的代码中验证为出色地)。如果您需要与
Flann
进行交叉检查,则最好的做法是实现自己的交叉检查或使用Flann
以获取描述符的子集,然后使用bfmatcher < /code>的交叉检查选项。
以下是其他一些信息来源: [1] , [2]
Sure enough, five minutes after posting I found the answer:
FLANN
does not do cross-checks, which means I will have repeats of the 2nd keypoints but no repeats for the 1st keypoints (verified in my code as well).The best practice if you need cross-check with
FLANN
is to implement your own cross-check or useFLANN
to get a subset of descriptors and then useBFMatcher
's cross-check option.Here are some other sources of information: [1], [2]