1

I've using ORB/FLANN to compare images and want to save / load image descriptors that come out of FLANN to a file so I can read them in later to compare with another image.

import numpy as np
import os
import cv2

# read in two images and identify their keypoints and descriptors using ORB
first_image = cv2.imread('firstimage.jpg', 0)
second_image = cv2.imread('secondimage.jpg', 0)

detector = cv2.ORB_create(500)
kp1, des1 = detector.detectAndCompute(first_image, None)
kp2, des2 = detector.detectAndCompute(second_image, None)

# Match images using FLANN
FLANN_INDEX_LSH = 6
index_params= dict(algorithm = FLANN_INDEX_LSH, table_number = 6, 
                   key_size = 12, multi_probe_level = 1)
search_params = dict()

flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1, des2, k=2)

# determine how many good matches there are between these images

goodmatches = 0
for m, n in matches:
    if m.distance < 0.7*n.distance:
        goodmatches += 1

print("Number of good matches before save of descriptors =", goodmatches)

np.save('descfile', des2)

desc2 = np.load('descfile.npy')

matches = flann.knnMatch(des1, des2, k=2)

# determine how many good matches there are between these images after save/load

goodmatches = 0
for m, n in matches:
    if m.distance < 0.7*n.distance:
        goodmatches += 1

print("Number of good matches after reading in descriptors from file=", goodmatches)

There's clearly something I don't understand about either descriptors or the save/load process for descriptors as I would expect the two print statements to produce the same number of good matches between the images and they do not.

Any thoughts would be appreciated.

[ADDED]

Maybe this smaller description of what I'm doing in the code above will help:

  1. identify image descriptors for two different images
  2. compare them to determine how well they match, producing a match score for how similar these images are
  3. then save the descriptors for the second image to a file
  4. then load the descriptors for the second image in from this file
  5. compare the descriptors for the first image to those loaded from file for the second image, producing a match score for how similar these images are

I do not get the same match score after loading/saving the descriptors for the second image to file as I got comparing the first and second image before saving/loading.

Why???

2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – ImSo3K
    Commented Nov 16, 2021 at 13:26
  • I'm trying to save and load image descriptors properly. To test this, I'm match the descriptors for 'firstimage.jpg' and 'secondimage.jpg' and printing out how many matches made are good ones. I then save to a file the descriptors for 'secondimage'jpg' and then immediately load them back in. I would expect when I match the descriptors from 'firstimage.jpg' to those from 'secondimage.jpg' that I just loaded from file, that the number of good matches would be the same as calculated before saving/loading descriptors to file. They do not
    – CardaHolic
    Commented Nov 17, 2021 at 14:04

2 Answers 2

0

Ok, after much searching I found the answer to my own question. The load and save of descriptors is working fine above. The reason the results of matching the exact same descriptors yields different results is because the knnMatch() function has a random characteristic to its matching algorithm meaning that even if you call it with the exact same parameters twice in a row you will get at least slightly different results each time.

0

import pickle

For saving

with open(self.modelDir + "\\SiftDescriptors.pkl", 'wb') as f:
    pickle.dump(self.sampleImgDescriptors, f)

For Loading

with open(self.modelDir + "\\SiftDescriptors.pkl", 'rb') as f:
    descriptors = pickle.load(f)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.