首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ORB python opencv的匹配特性

ORB python opencv的匹配特性
EN

Stack Overflow用户
提问于 2015-07-29 02:28:36
回答 3查看 17.8K关注 0票数 11

我正在与opencv匹配特性,但是当我运行这段代码时,我得到了这个错误跟踪(最近一次调用):文件"ffl.py",第27行,in for m,n in matches: TypeError:'cv2.DMatch‘对象不可迭代

我不知道怎么修

代码语言:javascript
复制
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()
EN

回答 3

Stack Overflow用户

发布于 2015-10-17 08:56:01

bf.match只返回单个对象的列表,您不能用m,n迭代它。

您只需将代码更改为:

代码语言:javascript
复制
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 -火车图像索引。
票数 14
EN

Stack Overflow用户

发布于 2019-03-20 14:37:59

代码语言:javascript
复制
for m in matches:
    if m.distance < 0.7:
        good.append(m)

这段代码很好,但实际上与原始代码的含义不同。我认为在匹配中使用ORB和涉及n和n+1元素的内容是SIFT算法的原意,它执行比率匹配。

因此,正确的代码应该是(我猜):

代码语言:javascript
复制
for i, m in enumerate(matches):
    if i < len(matches) - 1 and m.distance < 0.7 * matches[i+1].distance:
        good.append(m)

效率较低,可能有一个解决办法或更好的代码。但我要强调的是,已回答的代码并不像OP的代码那样执行

原版筛纸上写着:

这个测试通过计算最佳匹配和次优匹配的比率来拒绝糟糕的匹配。如果该比率低于某一阈值,则由于该匹配质量较低而被丢弃.

还请注意,"0.7“是命名为”比率“,并固定为0.75 (从内存)在原始文件。

票数 3
EN

Stack Overflow用户

发布于 2020-12-19 22:16:39

代码尝试使用Lowe的比率测试(见原始筛选纸)。

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

守则应改为:

代码语言:javascript
复制
    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源代码(samples/python/find_obj.py)中的OpenCV教程或samples目录,可以找到工作的代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31690265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档