Reputation: 11
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:
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???
Upvotes: 1
Views: 1146
Reputation: 129
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)
Upvotes: 0
Reputation: 11
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.
Upvotes: 0