【问题标题】:How can I compare 2 sets of images with OpenCV如何将 2 组图像与 OpenCV 进行比较
【发布时间】:2021-04-15 11:37:04
【问题描述】:

我正在使用 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 与 Images2 中的 te3.jpg 进行比较。

我可以添加这两个文件夹并使其循环遍历它们,以便为 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")
    

【问题讨论】:

    标签: python-3.x opencv image-compression


    【解决方案1】:

    我认为你只需要一个嵌套的 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 
    

    根据你的记忆或者文件夹中的图像数量,我可以像上面那样将所有 imgs 直接加载到一个列表中,或者你在 for 循环中加载 imgs,这样你就可以遍历 ls_imgs1_path 和 ls_imgs2_path

    【讨论】:

    • 第 6 行,在 中 ls_imgs1_path = os.listdir(Images1) # 将所有图像路径加载到列表中 NameError: name 'Images1' is not defined
    • 我更改了 img 变量的名称以使其更清晰。上半部分只是另一种(我认为更好)从两个文件夹加载你的 imgs 的方法——从 python 预装的 os 模块中查看函数“listdir”
    • 对不起 - 带有列表推导的行应该如下所示: ls_imgs1 = [cv2.imread(os.path.join(Images1, img)) for img in ls_imgs1_path] ls_imgs2 = [cv2. imread(os.path.join(Images2, img)) for img in ls_imgs2_path] 原因是,列表 "ls_imgs1_path" 看起来像这样: ["img1.png", "img2.png", ... ] 但您需要完整路径才能正确加载它们。这是通过“os.path.join()”方法获得 ["Images1/img1.png", "Images1/img2.png", ...] - 它解决了问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 2014-05-15
    相关资源
    最近更新 更多