Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with matches1to2 with Opencv SIFT

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)
like image 272
Undead Avatar asked Jan 02 '18 15:01

Undead


1 Answers

(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)
    
like image 68
Ali Eren Çelik Avatar answered Oct 17 '22 10:10

Ali Eren Çelik