0

When I match SIFT feature using FLANN,I found same input descriptor give different match pairs in same process.

python code:

 import cv2

 def match(des_q, des_t):
    FLANN_INDEX_KDTREE = 1
    ratio = 0.7  
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)
    flann1 = cv2.FlannBasedMatcher(index_params, search_params)
    two_nn = flann1.knnMatch(des_q, des_t, k=2)
    matches = [(first.queryIdx, first.trainIdx) for first, second in two_nn
                   if first.distance < ratio * second.distance]
    print(matches)
    return matches

def img_sim(img1, img2):
    img1 = cv2.cvtColor(img1, cv2.IMREAD_GRAYSCALE)
    img2 = cv2.cvtColor(img2, cv2.IMREAD_GRAYSCALE)
    sift = cv2.xfeatures2d.SIFT_create()
    eps = 1e-7
    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(img1, None)
    des1 /= (des1.sum(axis=1, keepdims=True) + eps)
    des1 = np.sqrt(des1)
    kp2, des2 = sift.detectAndCompute(img2, None)
    des2 /= (des2.sum(axis=1, keepdims=True) + eps)
    des2 = np.sqrt(des2)
    # test images,same input(des1,des2),different output?
    matches1 = match(des1, des2)
    matches2 = match(des1, des2)

img1 = '' # query image
img2 = '' # index image
img1 = cv2.cvtColor(cv2.imread(img1), cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(cv2.imread(img2), cv2.COLOR_BGR2RGB)
img_sim(img1, img2)

I found the matches in line 11 is different although my input descriptor(des1,des2) is same,I guess the reason is kd-tree cache,but I can't solve it.Any one could help me?

I want the matches result is always same. My cv2 version is 3.4.0, Thanks in advance.

Community
  • 1
  • 1
FancyXun
  • 1,218
  • 1
  • 9
  • 17

1 Answers1

0

I'm not an expert, but I have an assumption:

FLANN is based on a randomized k-d tree algorithm that approximates the nearest neighbor. Its aim is to find a fast approximation with acceptable declines in accuracy. As a consequence, the results may not always be the same.

For detailed informations, have a look at the papers listed at FLANN

Depending on what you are using the matches for (e.g. calculation of a homography), it could be possible to be no issue at all.