Search code examples
python-3.xopencvimage-compression

How can I compare 2 sets of images with OpenCV


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)) + "%\n")
    

Solution

  • I think you just need a nested for loop?

    So for the folders "Images1" and "Images2" - I would to it this way:

    import os
    import cv2
    
    # load all image names into a list
    ls_imgs1_names = os.listdir(Images1) 
    ls_imgs2_names = os.listdir(Images2) 
    
    # construct image paths and save in list
    ls_imgs1_path = [os.path.join(Images1, img) for img in ls_imgs1_names]
    ls_imgs2_path = [os.path.join(Images2, img) for img in ls_imgs2_names]
    
    # list comprehensin to load imgs in lists
    ls_imgs1 = [cv2.imread(img) for img in ls_imgs1_path] 
    ls_imgs2 = [cv2.imread(img) for img in ls_imgs2_path]
    
    for original in ls_imgs1:
        for image_to_compare in ls_imgs2:
    
            # compare orignal to image_to_compare
            # here just insert your code where you compare two images 
    

    Depending on your memory or rather the amount of images in your folder I would either load all imgs directly into a list as I did above or you load the imgs in the for loops, so that you loop over the ls_imgs1_path and ls_imgs2_path