Base64 图像比较
Base64 Image Comparison
假设我有两个图像 A 和 B,大小相同,通道数相同,格式相同(例如 PNG 中大小为 25x25 的 RGB 图像)。
我想比较这两张图片,并根据 每个像素的差异总和 对这两张图片的不同程度打分。但是,这些图像以 Base64 格式编码(如 HTML 页中的图像)。
我的问题是,总结Base64格式的每个字符的差异是否一定可以估计A和B的不同或相似之处?
我在 Python 中编写了一个测试来生成三个随机图像 img1, img2, img3
。然后先逐个像素比较它们,再比较它们的Base64版本。我试图查看 img1
是否与 img2
或 img3
更相似,然后测试这两个比较是否相关。
答案是否。它们不相关。在我的测试中,1000 次测试中有 488 次它们不相关。
import numpy as np
import base64
# Generates an image with random values
def generate_image():
img = np.random.random((10,10,3)) * 255
return np.uint8(img)
# First comparison method based on pixel to pixel comparison
def compare_numpy(img1, img2):
return np.sum( np.abs( img1 - img2 ) )
# Second comparison method based on comparing Base64 versions
def compare_base64(img1, img2):
b1 = list(base64.b64encode(img1))
b2 = list(base64.b64encode(img2))
return sum( abs(b1[i] - b2[i]) for i in range(len(b1)))
# Test if both methods says if img1 is closer to img2 or img3
def correlation_test():
img1 = generate_image()
img2 = generate_image()
img3 = generate_image()
# img1 is closer to img2 or img3
# Testing pixel to pixel comparison
cmp12 = compare_numpy(img1, img2)
cmp13 = compare_numpy(img1, img3)
if cmp12 < cmp13:
result1 = 2
else:
result1 = 3
# Testing Base64 comparison
cmp12 = compare_base64(img1, img2)
cmp13 = compare_base64(img1, img3)
if cmp12 < cmp13:
result2 = 2
else:
result2 = 3
return result1 == result2
true_cnt = 0
false_cnt = 0
# Running the test 1000 times
for i in range(1000):
if correlation_test():
true_cnt += 1
else:
false_cnt += 1
print(f"They are correlated {true_cnt} times")
print(f"They are not correlated {false_cnt} times")
# They are correlated 512 times
# They are not correlated 488 times
假设我有两个图像 A 和 B,大小相同,通道数相同,格式相同(例如 PNG 中大小为 25x25 的 RGB 图像)。
我想比较这两张图片,并根据 每个像素的差异总和 对这两张图片的不同程度打分。但是,这些图像以 Base64 格式编码(如 HTML 页中的图像)。
我的问题是,总结Base64格式的每个字符的差异是否一定可以估计A和B的不同或相似之处?
我在 Python 中编写了一个测试来生成三个随机图像 img1, img2, img3
。然后先逐个像素比较它们,再比较它们的Base64版本。我试图查看 img1
是否与 img2
或 img3
更相似,然后测试这两个比较是否相关。
答案是否。它们不相关。在我的测试中,1000 次测试中有 488 次它们不相关。
import numpy as np
import base64
# Generates an image with random values
def generate_image():
img = np.random.random((10,10,3)) * 255
return np.uint8(img)
# First comparison method based on pixel to pixel comparison
def compare_numpy(img1, img2):
return np.sum( np.abs( img1 - img2 ) )
# Second comparison method based on comparing Base64 versions
def compare_base64(img1, img2):
b1 = list(base64.b64encode(img1))
b2 = list(base64.b64encode(img2))
return sum( abs(b1[i] - b2[i]) for i in range(len(b1)))
# Test if both methods says if img1 is closer to img2 or img3
def correlation_test():
img1 = generate_image()
img2 = generate_image()
img3 = generate_image()
# img1 is closer to img2 or img3
# Testing pixel to pixel comparison
cmp12 = compare_numpy(img1, img2)
cmp13 = compare_numpy(img1, img3)
if cmp12 < cmp13:
result1 = 2
else:
result1 = 3
# Testing Base64 comparison
cmp12 = compare_base64(img1, img2)
cmp13 = compare_base64(img1, img3)
if cmp12 < cmp13:
result2 = 2
else:
result2 = 3
return result1 == result2
true_cnt = 0
false_cnt = 0
# Running the test 1000 times
for i in range(1000):
if correlation_test():
true_cnt += 1
else:
false_cnt += 1
print(f"They are correlated {true_cnt} times")
print(f"They are not correlated {false_cnt} times")
# They are correlated 512 times
# They are not correlated 488 times