Reputation: 1243
hi im working in Matching Features with ORB python opencv but when i run this code i get this error Traceback (most recent call last): File "ffl.py", line 27, in for m,n in matches: TypeError: 'cv2.DMatch' object is not iterable
i don't know how to fix it
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()
Upvotes: 11
Views: 20128
Reputation: 15364
The code attempts to use Lowe's ratio test (see original SIFT paper).
this requires, for every descriptor, the two closest matches.
the code should read:
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)
further, I would highly recommend the flann matcher. it's faster than the brute force matcher.
look at the OpenCV tutorials or the samples directory in OpenCV's source (samples/python/find_obj.py
) for code that works.
Upvotes: 1
Reputation: 875
for m in matches:
if m.distance < 0.7:
good.append(m)
This block of code is good, but does not really have the same meaning as the original. I think that using ORB and something involving n and n+1 elements in the matches refers to the original intent of SIFT algorithm, which performs a ratio match.
So, a correct code would be (I guess) :
for i, m in enumerate(matches):
if i < len(matches) - 1 and m.distance < 0.7 * matches[i+1].distance:
good.append(m)
Less efficient, and there may be a workaround or better code. But my main point is to underline that the answered code is not doing the same as OP's code.
Original SIFT paper saying :
This test rejects poor matches by computing the ratio between the best and second-best match. If the ratio is below some threshold, the match is discarded as being low-quality.
Please also note that "0.7" is named "ratio" and is fixed to 0.75 (from memory) in the original paper.
Upvotes: 4
Reputation: 3068
bf.match
return only a list of single objects, you cannot iterate over it with m,n. Maybe you are confused with bf.knnMatch
?
You can just change your code to:
for m in matches:
if m.distance < 0.7:
good.append(m)
From the Python tutorials of OpenCV (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.
Upvotes: 15