在加载图像之前,我们需要先确保img1
和img2
分别是NumPy数组或者灰度图像文件的像素数组。
以下是两种加载图像的方法,分别用于加载NumPy数组和灰度图像文件:
1、加载NumPy数组:
import cv2
import numpy as np
# 假设你已经有了两个NumPy数组,分别是img1和img2
# 比较两幅图像是否相等
are_equal = np.array_equal(img1, img2)
# 输出结果
print(are_equal)
2、加载灰度图像文件:
from PIL import Image
import numpy as np
# 假设图像文件路径分别为img1_path和img2_path
# 加载图像1
img1_path = "path/to/image1.png"
pil_img1 = Image.open(img1_path)
img1 = np.array(pil_img1)
# 加载图像2
img2_path = "path/to/image2.png"
pil_img2 = Image.open(img2_path)
img2 = np.array(pil_img2)
# 比较两幅图像是否相等
are_equal = np.array_equal(img1, img2)
# 输出结果
print(are_equal)
在以上代码示例中,img1
和img2
可以是NumPy数组或者灰度图像文件的像素数组。如果这两个数组完全相等,则are_equal
为True,否则为False。