I am using OpenCV to compare 2 images.
After a couple of days, I was able to modify it to compare a image to a list of images.
How can I compare a list of images with another list?
Ex: we have 2 folders Images1 and Images2. Images1 = te1.jpg, te2.jpg, te3.jpg; Images2 = te1.jpg, te2.jpg, te3.jpg.
I want to compare te1.jpg from Images1 with te1.jpg from Images2, te2.jpg from Images1 with te2.jpg from Images2 and te3.jpg from Images1 with te3.jpg from Images2.
Can I add both folders and make it loop thru them in order to get the correspondent image in Images2 for every image in Images1?
He is my code until now:
import cv2
import numpy as np
import glob
original = cv2.imread("te.jpg")
#Load all the images
all_images_to_compare = []
titles = []
for f in glob.iglob("images2/*"):
image = cv2.imread(f)
titles.append(f)
all_images_to_compare.append(image)
for image_to_compare, title in zip(all_images_to_compare, titles):
# 1) Check if 2 images are equals
if original.shape == image_to_compare.shape:
print("The images have the same size and channels")
difference = cv2.subtract(original, image_to_compare)
b, g, r = cv2.split(difference)
#image1 = original.shape
#image2 = duplicate.shape
cv2.imshow("difference", difference)
#cv2.imshow("b", b)
#cv2.imshow("g", g)
#cv2.imshow("r", r)
#print(image1)
#print(image2)
print(cv2.countNonZero(b))
if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) ==0:
print("Similarity: 100% (equal size and channels)")
# 2) Check for similarities between the 2 images
sift = cv2.xfeatures2d.SIFT_create()
kp_1, desc_1 = sift.detectAndCompute(original, None)
kp_2, desc_2 = sift.detectAndCompute(image_to_compare, None)
#print("Keypoints 1ST Image: " + str(len(kp_1)))
#print("Keypoints 2ND Image: " + str(len(kp_2)))
index_params = dict(algorithm=0, trees=5)
search_params = dict()
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(desc_1, desc_2, k=2)
good_points = []
ratio = 0.9 # mai putin de 1
for m, n in matches:
if m.distance < ratio*n.distance:
good_points.append(m)
# Define how similar they are
number_keypoints = 0
if len(kp_1) <= len(kp_2):
number_keypoints = len(kp_1)
else:
number_keypoints = len(kp_2)
print("Keypoints 1ST Image: " + str(len(kp_1)))
print("Keypoints 2ND Image: " + str(len(kp_2)))
print("Title:" +title)
percentage_similarity = len(good_points) / number_keypoints * 100
print("Similarity: " + str(int(percentage_similarity)) + "%
")
question from:
https://stackoverflow.com/questions/65651415/how-can-i-compare-2-sets-of-images-with-opencv 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…