4

I'm trying to detect an image using SURF following the tutorial (https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html)

My goal is now to add multiple images to FlannBasedMatcher and then save it to be able to load it afterward. When changing the code from the example and trying to add() and train() the descriptors before calling knnMatch(queryDescriptors=des1, k=2) (instead of matches = flann.knnMatch(des1,des2,k=2) I get other results as in the tutorial example.

surf = cv2.xfeatures2d.SURF_create(800)
...
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)   # or pass empty dictionary
...
flann = cv2.FlannBasedMatcher(index_params,search_params)
flann.add(des1)
flann.train()
flann.knnMatch(queryDescriptors=des2, k=2)

Question 1: Why am I getting different results than in the tutorial?

When changing the value of k in knnMatch() for ex. 6, will return the nearest 6 matches. With knn=2, to find the good matches I check that the distance of the returned matches is not larger than m1.distance < 0.8 * m2.distance.

Question 2: With knn=6, what match of the 6 should I use as an anchor to compare that the distance is not larger than 0.8*distance away?

suizo
  • 531
  • 8
  • 24
  • You are using SURF algorithm (which has a particular license) while the tutorial is using SIFT. It is not surprising to have different result with two different selection algorithms. – T.Lucas Dec 19 '18 at 13:39
  • I'm comparing the results of flann.knnMatch(des1, des2) and flann.add(des1), train, flann.knnMatch(des2). Therefor the used algorithm isn't of any importance. (I also tested it with SIFT) – suizo Dec 19 '18 at 15:31

1 Answers1

3

Question 1:

I think the result will differ from the tutorial because the search range is different. The tutorial finds match of item1 of des1 in des2. However, you are looking for item1 of des1 in the union of des1 and des2.

Question 2:

The ratio test was designed by Low(SIFT author) to measure the uniqueness of a matched point. If the distance(in terms of score/similarity) between the best match and second best match is large, it means that the best match is unique and no other feature in the image is similar to it. However, if the second best match is close to the best match, it means that the feature is not unique and the feature is probably a repeated pattern which we should discard from the matching process.

So by using k=6, you are looking for the best 6 matches which doesn't help in determining the uniqueness of the feature since only the best and second best point matters.

Maybe you are trying to cluster similar points from the union of the descriptors. Then the ratio test is useless in this case as there would not be a unique match anymore.

yapws87
  • 1,809
  • 7
  • 16
  • For question 1: I had a typo. I add des1 - train it and the compare it to des2 and get a different result which is strange and I can't explain – suizo Dec 19 '18 at 13:31