停止Python 3.7 glob和cv2循环多次比较图像文件

问题描述 投票:0回答:1

[寻求完善脚本的帮助,该脚本接受一个图像jpg文件并将其与目录中的多个jpg文件进行比较,并在第一个循环中提供100%匹配或在第二个循环中减少%匹配的百分比。

该代码效果很好,并最终给出了答案,但问题是该脚本以相同的%相似度反复遍历相同文件,这似乎浪费了处理能力和时间。

我知道中断在这里很有用,但我不知道在不停止运行的情况下将其插入到代码中的任何方法,使脚本仅检查每个图像一次的任何帮助将不胜感激。



import glob
import cv2

original = cv2.imread("testsmall.jpeg") # image to check against all others

sift = cv2.xfeatures2d.SIFT_create()
kp_1, desc_1 = sift.detectAndCompute(original, None)
index_params = dict(algorithm=0, trees=5)
search_params = dict()
flann = cv2.FlannBasedMatcher(index_params, search_params)

Load all the images
all_images_to_compare = []
titles = []
for f in glob.iglob("live_test\*"): #directory where the files to check against are
    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:
            difference = cv2.subtract(original, image_to_compare)
            b, g, r = cv2.split(difference)
            if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
                print(title + ": 100%")  # PRINTS THE EXACT MATCHES
        # 2) Check for similarities between the 2 images
        kp_2, desc_2 = sift.detectAndCompute(image_to_compare, None)
        matches = flann.knnMatch(desc_1, desc_2, k=2)
        good_points = []
        for m, n in matches:
            if m.distance < 0.6 * n.distance:  
                good_points.append(m)
        number_keypoints = 0
        if len(kp_1) >= len(kp_2):
            number_keypoints = len(kp_1)
        else:
            number_keypoints = len(kp_2)
        percentage_similarity = len(good_points) / number_keypoints * 100
        print(title + " : " + str(int(percentage_similarity)) + "%")

即使目录中只有6x jpg文件,您看到的当前输出也会出现多次出现相同的文件名,并且重复的数量随着目录中图像数量的增加而增加]


live_test\07322540785999_1.jpg : 8%
live_test\07322540785999_1.jpg : 8%
live_test\07322540785999_2.jpg : 3%
live_test\07322540785999_1.jpg : 8%
live_test\07322540785999_2.jpg : 3%
live_test\07322540808667_1.jpg : 12%
live_test\07322540785999_1.jpg : 8%
live_test\07322540785999_2.jpg : 3%
live_test\07322540808667_1.jpg : 12%
live_test\07322540808667_2.jpg : 3%
live_test\07322540785999_1.jpg : 8%
live_test\07322540785999_2.jpg : 3%
live_test\07322540808667_1.jpg : 12%
live_test\07322540808667_2.jpg : 3%
live_test\7322540557466_T1.jpg : 51%
live_test\07322540785999_1.jpg : 8%
live_test\07322540785999_2.jpg : 3%
live_test\07322540808667_1.jpg : 12%
live_test\07322540808667_2.jpg : 3%
live_test\7322540557466_T1.jpg : 51%
live_test\test.jpg : 1%

Process finished with exit code 0

寻求完善脚本的帮助,该脚本接受一个图像jpg文件并将其与目录中的多个jpg文件进行比较,并在第一个循环中提供100%匹配或在第二个中减少%匹配的百分比...

python python-3.x image opencv glob
1个回答
0
投票

欢迎使用堆栈溢出。由于我没有您的输入文件,因此我将提供一种无法测试的解决方案,以确保您确实遇到问题。

我认为您的问题是您的第二个循环不应该在您的第一个循环内。因为现在,每次您从glob.iglob("live_test\*")中检索文件名时,您都将开始比较之前累积的所有文件的比较阶段。我认为您希望在收集完所有输入文件之后进入比较阶段。

© www.soinside.com 2019 - 2024. All rights reserved.