使用ORB算法在Python OpenCV中实现特征匹配功能

11

嗨,我正在使用Python OpenCV的ORB算法进行特征匹配,但是当我运行这段代码时,我遇到了以下错误:

Traceback (most recent call last):

File "ffl.py", line 27, in

for m,n in matches:

TypeError: 'cv2.DMatch' object is not iterable

我不知道该如何解决它。

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()
3个回答

14

bf.match 只返回单个对象列表,您无法使用 m,n 进行迭代。也许您与 bf.knnMatch 搞混了?

您可以将您的代码更改为:

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

来自OpenCV的Python教程(链接):

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

  • DMatch.distance - 描述符之间的距离。数值越小越好。
  • DMatch.trainIdx - 训练描述符中的索引。
  • DMatch.queryIdx - 查询描述符中的索引。
  • DMatch.imgIdx - 训练图像的索引。

1
在这个上下文中,“train”、“descriptors”和“query”是什么意思? - DragonKnight
1
@DragonKnight queryIdx 给出了你正在尝试匹配的原始图像上关键点的索引,而 trainIdx 给出了参考图像上匹配的关键点的索引。 - Eyshika
这不是Lowe的比率测试。请参考原始的SIFT论文或使用OpenCV示例目录中提供的代码。 - Christoph Rackwitz

3
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(从记忆中得出)。


1
这也没有意义。Lowe's比率测试需要使用k=2的knnMatch。你不能简单地选择下一个描述符的最佳匹配项。它必须是同一描述符的最接近的两个匹配项。 - Christoph Rackwitz

1
代码试图使用Lowe的比率测试(请参见原始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匹配器。它比暴力匹配器更快。
请查看OpenCV教程或OpenCV源代码中的样例目录(samples/python/find_obj.py)以获取可用的代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接