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
相关文章:
- Base64 图像比较
- 比较BGR图像是否完全相同
- 如何使用mingw64 gcc/g 比较两个图像
- 如何比较QT中的两个RGB图像
- TBB免费图像lambda阵列比较错误
- 访问图像像素;MatIterator_<> 和 Mat::at 运算符的比较
- MPI BMP 图像比较更高效
- C++图像比较每个块中的平均位数
- Opencv比较图像
- OpenCV检测手部和比较图像
- OpenCV代码将存储在列表中的描述符与新的图像描述符进行比较
- 如何在 opencv 中将图像与数据库进行比较C++
- 使用直方图比较 c++ 匹配图像
- OpenCV图像调整大小与matlab的比较
- 使用C++和OpenCV进行图像比较的方法
- C# 中的 pHash 图像比较实现
- 如何使用 OpenCV 比较存储在文件中的两个图像
- 如何比较比较两个图像的轮廓
- c++ /SIFT/SQL -如果有一种方法可以有效地比较图像的SIFT描述符与SQL数据库中的SIFT描述符
- 比较图像的相似性