将多个图像与一个图像进行比较

Compare mutiple images with one images

我正在尝试将一张图片与装满图片的文件夹进行比较,并试图找到相同的图片,但我不知道如何将一张图片与装满图片的文件夹进行比较

我尝试使用 fnmatch 和 os 制作一个 listOfFiles,但它只需要少数几张图片中的一张。

#import 
import cv2
import numpy as np
import os, fnmatch

#Collects all images 
listOfFiles = os.listdir('./images')
pattern = "*.jpg"
for entry in listOfFiles:
    if fnmatch.fnmatch(entry, pattern):
            Allimages = ("images/" + entry)

#Define variables
upload = cv2.imread("images/img1.jpg")
duplicate = cv2.imread(Allimages)
#Checks if duplicate is duplicate 
if upload.shape == duplicate.shape:
    print("The images have same size and channels")
    difference = cv2.subtract(upload, duplicate)
    b, g, r = cv2.split(difference) 

    if cv2.countNonZero(b) == 0 and cv2.countNonZero (g) == 0 and cv2.countNonZero(r) == 0:
        print("images are the same")

else:
    print("images are different")

您必须在 for 循环中完成所有操作。

可能你可以只使用

比较图像
if (upload == duplicate).all():

代码:

import cv2
import os

directory = './images'
upload = cv2.imread("images/img1.jpg")

for entry in os.listdir(directory):

    if entry.lower().endswith( ('.jpg', '.jpeg', '.png', '.gif') ):

        fullname = os.path.join(directory, entry)
        print('fullname:', fullname)
        duplicate = cv2.imread(fullname)

        if upload.shape == duplicate.shape:
            print("The images have same size and channels")

            #difference = cv2.subtract(upload, duplicate)
            #b, g, r = cv2.split(difference) 
            #if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
            if (upload == duplicate).all():
                print("images are the same")

        else:
            print("images are different")