【问题标题】:Opencv BFMatcher for multiple images用于多个图像的 Opencv BFMatcher
【发布时间】:2018-09-22 21:20:37
【问题描述】:

我想在 Python 中使用 opencv 执行蛮力 SIFT 特征匹配。

我在 opencv 文档中找到了以下代码。

import numpy as np
import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread('box.png',0)          # queryImage
img2 = cv2.imread('box_in_scene.png',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)

# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)

# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])

# cv2.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)

plt.imshow(img3),plt.show()

但是,此代码仅适用于两个图像。我尝试提供描述符列表,但它似乎不起作用。如何一次匹配多个数据库图像?

【问题讨论】:

    标签: python opencv image-processing


    【解决方案1】:

    BFMatcher 一次只能用于两个描述符。如果您在此处查看文档:https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html

    蛮力匹配器很简单。 它采用第一组中一个特征的描述符并匹配 使用一些距离计算与第二组中的所有其他特征。 并返回最接近的那个。

    我很不确定您所说的多个描述符是什么意思。但是,我知道您可以从与 image2 匹配的 image1 中获取描述符。然后,将 image1 与 image3 匹配。最后,image2 和 image3。匹配多个图像的描述符需要手动完成。

    【讨论】:

    • 你知道如何对这些匹配进行排名吗?
    • @Lilo 您需要按升序对它们进行排序(距离越小越好)。只需:matches = sorted(matches, key=lambda match: match.distance)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 2016-11-27
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    相关资源
    最近更新 更多