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:
- identify image descriptors for two different images
- compare them to determine how well they match, producing a match score for how similar these images are
- then save the descriptors for the second image to a file
- then load the descriptors for the second image in from this file
- 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???