【问题标题】:Matching Features with ORB python opencv使用 ORB python opencv 匹配特征
【发布时间】:2015-07-29 02:28:36
【问题描述】:

嗨,我正在使用 ORB python opencv 匹配功能,但是当我运行此代码时出现此错误 回溯(最近一次通话最后): 文件“ffl.py”,第 27 行,在 对于 m,n 在比赛中: TypeError: 'cv2.DMatch' 对象不可迭代

不知道怎么解决

import numpy as np
import cv2
import time

ESC=27   
camera = cv2.VideoCapture(0)
orb = cv2.ORB_create()
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

imgTrainColor = cv2.imread('/home/shar/home.jpg')
imgTrainGray = cv2.cvtColor(imgTrainColor, cv2.COLOR_BGR2GRAY)

kpTrain = orb.detect(imgTrainGray,None)
kpTrain, desTrain = orb.compute(imgTrainGray, kpTrain)

firsttime = True

while True:

    ret, imgCamColor = camera.read()
    imgCamGray = cv2.cvtColor(imgCamColor, cv2.COLOR_BGR2GRAY)
    kpCam = orb.detect(imgCamGray,None)
    kpCam, desCam = orb.compute(imgCamGray, kpCam)
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    matches = bf.match(desCam,desTrain)
    good = []
    for m,n in matches:
        if m.distance < 0.7*n.distance:
            good.append(m)

    if firsttime==True:
        h1, w1 = imgCamColor.shape[:2]
        h2, w2 = imgTrainColor.shape[:2]
        nWidth = w1+w2
        nHeight = max(h1, h2)
        hdif = (h1-h2)/2
        firsttime=False

    result = np.zeros((nHeight, nWidth, 3), np.uint8)
    result[hdif:hdif+h2, :w2] = imgTrainColor
    result[:h1, w2:w1+w2] = imgCamColor

    for i in range(len(matches)):
        pt_a=(int(kpTrain[matches[i].trainIdx].pt[0]), int(kpTrain[matches[i].trainIdx].pt[1]+hdif))
        pt_b=(int(kpCam[matches[i].queryIdx].pt[0]+w2), int(kpCam[matches[i].queryIdx].pt[1]))
        cv2.line(result, pt_a, pt_b, (255, 0, 0))

    cv2.imshow('Camara', result)

    key = cv2.waitKey(20)                                 
    if key == ESC:
        break

cv2.destroyAllWindows()
camera.release()

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    bf.match 仅返回单个对象的列表,您不能使用 m,n 对其进行迭代。也许您对bf.knnMatch 感到困惑?

    您只需将代码更改为:

    for m in matches:
        if m.distance < 0.7:
            good.append(m)
    

    来自OpenCV的Python教程(link):

    matches = bf.match(des1,des2) 行的结果是一个 DMatch 列表 对象。此 DMatch 对象具有以下属性:

    • DMatch.distance - 描述符之间的距离。越低越好 它是。
    • DMatch.trainIdx - 列车描述符中的描述符索引
    • DMatch.queryIdx - 查询描述符中的描述符索引
    • DMatch.imgIdx - 火车图像的索引。

    【讨论】:

    • 在这种情况下,“train”、“descriptors”和“query”是什么意思?
    • @DragonKnight queryIdx 给出您尝试匹配的原始图像上的关键点索引,trainIdx 给出在参考图像上匹配的关键点索引。
    • 不是劳氏比率测试。请查阅原始 SIFT 论文或使用 opencv 示例目录中给出的代码。
    【解决方案2】:
    for m in matches:
        if m.distance < 0.7:
            good.append(m)
    

    这段代码不错,但和原来的意思不太一样。我认为在匹配中使用 ORB 和涉及 n 和 n+1 元素的东西是指 SIFT 算法的初衷,它执行比率匹配。

    所以,正确的代码应该是(我猜):

    for i, m in enumerate(matches):
        if i < len(matches) - 1 and m.distance < 0.7 * matches[i+1].distance:
            good.append(m)
    

    效率较低,可能有解决方法或更好的代码。但我的主要观点是强调回答的代码 与 OP 的代码不同

    原始 SIFT 论文说:

    此测试通过计算最佳匹配之间的比率来拒绝不良匹配 和第二好的匹配。如果比率低于某个阈值,则匹配 被丢弃为低质量。

    另请注意,“0.7”被命名为“ratio”,在原论文中固定为 0.75(记忆中)。

    【讨论】:

    • 这也没有意义。劳氏比率剩余需要 knnMatch,k=2。您不能简单地为 next 描述符取最佳匹配。它必须是 same 描述符的最接近的两个匹配项。
    【解决方案3】:

    代码尝试使用劳氏比率测试(参见原始 SIFT 论文)。

    对于每个描述符,这需要 两个 最接近的匹配项。

    代码应为:

        matches = bf.knnMatch(desCam, desTrain, k=2) # knnMatch is crucial
        good = []
        for (m1, m2) in matches: # for every descriptor, take closest two matches
            if m1.distance < 0.7 * m2.distance: # best match has to be this much closer than second best
                good.append(m1)
    

    此外,我强烈推荐 flann matcher。它比蛮力匹配器更快。

    查看 OpenCV tutorials 或 OpenCV 源代码 (samples/python/find_obj.py) 中的示例目录以获取有效的代码。

    【讨论】:

      猜你喜欢
      • 2017-10-05
      • 2014-05-16
      • 2018-04-21
      • 2018-02-22
      • 1970-01-01
      • 2014-04-04
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多