Error with matches1to2 with Opencv SIFT
Asked Answered
W

1

8

I am getting an error:

line 33, in <module>
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches,None,flags=2)
TypeError: Expected cv::DMatch for argument 'matches1to2'

When I try to execute this code. It is supposed to be a simple image comparison in SIFT and plot the matches. I previously had the code working for ORB, but when I converted it to work for SIFT this error occurred. Can someone please help me out with this? The code is below:

#import os
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
import cv2
import time
import glob, os
import numpy as np
import matplotlib.pyplot as plt


img1 = cv2.imread("/home/undead/Documents/TestSpectro/test.png",0)
img2 = cv2.imread("/home/undead/Documents/LibrarySpectro/ThankYouAud.png",0)

sift = cv2.xfeatures2d.SIFT_create(3000)

kp1,des1 = sift.detectAndCompute(img1,None)
kp2,des2 = sift.detectAndCompute(img2,None)

bf = cv2.BFMatcher()

matches = bf.knnMatch(des1,des2,k=2)


good = []
for m,n in matches:
    if m.distance < 0.75 * n.distance:
        good.append([m])

#img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,flags=2)
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches,None,flags=2)

plt.imshow(img3),plt.show()

UPDATE Found the problem, the line in question should be:

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,flags=2)
Waive answered 2/1, 2018 at 15:24 Comment(0)
A
11

(Apart from your question) In your code, you apply ratio test but then you dont use the result. You should pass "good" instead of "matches" as parameter.

For your question, it is possible to maintain two lists, such as

good = []
good_without_list = []

for m, n in matches:
    if m.distance < 0.75 * n.distance:
        good.append([m])
        good_without_list.append(m)

and then you can use

  • "good" list for "cv2.drawMatchesKnn" (in your case)

    knn_image = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags=2)
    
  • "good_without_list" list for "cv2.drawMatches"

    img3 = cv2.drawMatches(img1, kp1, img2, kp2, good_without_list, None, **draw_params)
    
Actomyosin answered 25/6, 2018 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.