python cv2 feature matching give different results [closed]

asked 2018-08-21 21:24:23 -0600

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

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 intwo_nn
                   if first.distance < ratio * second.distance]
    print(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)
    match(des1, des2)
    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.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-12-05 12:11:11.073425