如何用 OpenCV 比较 2 组图像

How can I compare 2 sets of images with OpenCV

我正在使用 OpenCV 比较 2 个图像。

几天后,我能够修改它以将图像与图像列表进行比较。

如何将一个图像列表与另一个列表进行比较?

例如:我们有 2 个文件夹 Images1 和 Images2。图片 1 = te1.jpg、te2.jpg、te3.jpg;图片2 = te1.jpg, te2.jpg, te3.jpg.

我想比较 Images1 的 te1.jpg 和 Images2 的 te1.jpg,Images1 的 te2.jpg 和 Images2 的 te2.jpg 以及 Images1 的 te3.jpg 和 te3.jpg 来自 Images2.

我可以添加这两个文件夹并使其遍历它们以便为 Images1 中的每个图像获取 Images2 中的相应图像吗?

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")
    

我想你只需要一个嵌套的 for 循环?

所以对于文件夹“Images1”和“Images2”- 我会这样处理:

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 

根据您的内存或文件夹中的图像数量,我会像上面那样将所有 img 直接加载到列表中,或者您在 for 循环中加载 img,以便循环遍历 ls_imgs1_path 和 ls_imgs2_path