Reputation: 6118
I wrote a little script which let to find an object in a global picture by the SIFT descriptors method
. But I have a question about multiple detections in the same picture.
I have this global picture :
I have this template :
My script looks like :
import numpy as np
import cv2
#########################
# SIFT descriptors part #
#########################
img1 = cv2.imread('/Users/valentinjungbluth/Desktop/SIFT:SURF Algo/lampe.jpg',0)
img2 = cv2.imread('/Users/valentinjungbluth/Desktop/SIFT:SURF Algo/ville.jpg',0)
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
print (img1.dtype)
print (img2.dtype)
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.2*n.distance :
good.append([m])
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
cv2.imwrite('matches.jpg',img3)
And the result is :
My question is :
How I can detect this others lamps ? Because all lamps are very similar and I want to match with all lamps which are present in the picture.
Thank you so much !
EDIT With Micka's answer :
Nothing appears at 0.2 scale distance, but if I put 0.75 :
Upvotes: 6
Views: 2535
Reputation: 3115
This is a good question. There are couple of ways I can think of doing this:
1.Sliding Windowing technique - You can search for the "template" in the global image by making a window, the size of the template, and sliding it in the entire image. You can do this for a pyramid so the scale and translational changes are taken care of.
Hope it helps!
Upvotes: 1
Reputation: 433
Try to allow more good matches by being more permissive in the condition.
good = []
for m,n in matches :
if m.distance < 0.2*n.distance :
good.append([m])
A more robust approach would be to describe the lamp using the sift features extracted from the template image(s), and then try to find those features using a sliding window over the image. For each window, compute the sift features, and compute a "distance" to your template's features. If the distance is smaller than a given threshold, then the window contains a lamp!
Upvotes: 1