Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问能否实现:比较两张图片是否相同、并输出布尔值 #575

Open
zyd232 opened this issue Dec 13, 2024 · 1 comment
Open

Comments

@zyd232
Copy link

zyd232 commented Dec 13, 2024

需求描述:
比较两张图片的像素信息——若完全相同,输出true;若不同,输出false。

目前的 compare 节点,若输入 image 到 a 和 b,并选择a==b,输出的东西很奇怪:

tensor([[[[True, True, True],
          [True, True, True],
          [True, True, True],
          ...,
          [True, True, True],
          [True, True, True],
          [True, True, True]],

         [[True, True, True],
          [True, True, True],
          [True, True, True],
          ...,
          [True, True, True],
          [True, True, True],
          [True, True, True]],

         [[True, True, True],
          [True, True, True],
          [True, True, True],
          ...,
          [True, True, True],
          [True, True, True],
          [True, True, True]],

         ...,

         [[True, True, True],
          [True, True, True],
          [True, True, True],
          ...,
          [True, True, True],
          [True, True, True],
          [True, True, True]],

         [[True, True, True],
          [True, True, True],
          [True, True, True],
          ...,
          [True, True, True],
          [True, True, True],
          [True, True, True]],

         [[True, True, True],
          [True, True, True],
          [True, True, True],
          ...,
          [True, True, True],
          [True, True, True],
          [True, True, True]]]])
@zyd232 zyd232 changed the title 请问能否实现——比较两张图片是否相同、并输出布尔值 请问能否实现:比较两张图片是否相同、并输出布尔值 Dec 13, 2024
@Chengym2023
Copy link

Python + OpenCV

OpenCV 是一个强大的计算机视觉库,可以用来比较两张图片。

import cv2

def compare_images(img1_path, img2_path):
    img1 = cv2.imread(img1_path)
    img2 = cv2.imread(img2_path)

    if img1.shape != img2.shape:
        return False

    difference = cv2.subtract(img1, img2)
    b, g, r = cv2.split(difference)

    if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
        return True
    else:
        return False

# 使用示例
if compare_images('image1.jpg', 'image2.jpg'):
    print("图片相同")
else:
    print("图片不同")

Python + PIL (Pillow)

PIL 是 Python 的图像处理库,也可以用来比较两张图片。

from PIL import Image

def compare_images(img1_path, img2_path):
    img1 = Image.open(img1_path)
    img2 = Image.open(img2_path)

    if img1.size != img2.size:
        return False

    diff = ImageChops.difference(img1, img2)
    if diff.getbbox() is None:
        return True
    else:
        return False

# 使用示例
if compare_images('image1.jpg', 'image2.jpg'):
    print("图片相同")
else:
    print("图片不同")

4. 哈希算法

使用图像哈希算法(如感知哈希、平均哈希等)可以更快速地比较两张图片。这些算法通过计算图像的特征向量,然后比较这些向量
来判断两张图片是否相同。

Python + ImageHash

ImageHash 是一个用于计算图像哈希值的库。

import imagehash
from PIL import Image

def compare_images(img1_path, img2_path):
    img1 = Image.open(img1_path)
    img2 = Image.open(img2_path)

    hash1 = imagehash.average_hash(img1)
    hash2 = imagehash.average_hash(img2)

    return hash1 == hash2

# 使用示例
if compare_images('image1.jpg', 'image2.jpg'):
    print("图片相同")
else:
    print("图片不同")
遇事不决问AI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants