gpt4 book ai didi

python - 与 ORB python opencv 匹配特征

转载 作者:太空狗 更新时间:2023-10-29 22:21:58 24 4
gpt4 key购买 nike

嗨,我正在使用 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()

最佳答案

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

您只需将代码更改为:

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

来自 OpenCV 的 Python 教程(link):

The result of matches = bf.match(des1,des2) line is a list of DMatch objects. This DMatch object has following attributes:

  • DMatch.distance - Distance between descriptors. The lower, the better it is.
  • DMatch.trainIdx - Index of the descriptor in train descriptors
  • DMatch.queryIdx - Index of the descriptor in query descriptors
  • DMatch.imgIdx - Index of the train image.

关于python - 与 ORB python opencv 匹配特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31690265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com