首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将多个图像与一个图像进行比较

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

Stack Overflow用户
提问于 2019-07-21 23:59:37
回答 1查看 45关注 0票数 -2

我试图将一张图片与一个充满图片的文件夹进行比较,并试图找到一个相等的图片,但我想不出如何将一张图片与一个充满图片的文件夹进行比较

我试着用fnmatch和os做了一个listOfFiles,但它只需要几张图片中的一张。

代码语言:javascript
复制
#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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-22 00:18:20

你必须在for循环中做所有的事情。

也许您可以仅使用以下命令比较图像

代码语言:javascript
复制
if (upload == duplicate).all():

代码:

代码语言:javascript
复制
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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57134814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档