本文介绍了与ORB python opencv匹配的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在使用ORB python opencv进行匹配功能,但是当我运行此代码时,出现此错误追溯(最近一次通话): 在第27行的文件"ffl.py"中 对于在比赛中的m,n:TypeError:"cv2.DMatch"对象不可迭代
hi im working in Matching Features with ORB python opencv but when i run this code i get this errorTraceback (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()
推荐答案
bf.match
仅返回单个对象的列表,您无法使用m,n对其进行迭代.也许您对bf.knnMatch
感到困惑?
bf.match
return only a list of single objects, you cannot iterate over it with m,n. Maybe you are confused with bf.knnMatch
?
您可以将代码更改为:
for m in matches:
if m.distance < 0.7:
good.append(m)
摘自OpenCV的Python教程(链接):
From the Python tutorials of OpenCV (link):
- DMatch.distance-描述符之间的距离.越低越好 它是.
- DMatch.trainIdx-火车描述符中描述符的索引
- DMatch.queryIdx-查询描述符中描述符的索引
- DMatch.imgIdx-火车图像的索引.
- 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.
这篇关于与ORB python opencv匹配的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!