gpt4 book ai didi

comparison - 使用ImageMagick对图像进行均等测试

转载 作者:行者123 更新时间:2023-12-01 22:29:53 27 4
gpt4 key购买 nike

ImageMagick库中是否有任何相等谓词函数?我想比较两个图像,并找出它们是否完全相同(像素的所有颜色都相同)或有什么不同。

我环顾四周,但似乎没有这样的功能。我应该自己使用像素迭代器编写函数吗?

最佳答案

ImageMagick提供compare函数以正确比较图像。

检查两个图像的md5校验和不是正确的方法,因为某些图像格式(例如,带有EXIF的PNG和JPEG)包含下面创建文件的日期和时间(请参见示例1),并且可以直观地看到一些文件相同,但内部表示完全不同(请参见示例2),或者具有不同的位深度(请参见示例3)。

示例1

# Create two identical images (100x100 pixels, bright red) but with different file contents
convert -size 100x100 xc:red r1.png
convert -size 100x100 xc:red r2.png

# MD5 checksum them
md5 r?.png
MD5 (r1.png) = 9f6d612615efd88c3fd8521d717e9811
MD5 (r2.png) = 996911bec0e0da75af46a1e78c052998 # Mmmm different

# Ask IM to tell us absolute error between the two (number of differing pixels)
compare -metric AE r1.png r2.png null:
0 # No difference - that's better

为什么这两个在MD5中有所不同?因为日期在他们里面...
identify -verbose r[12].png | grep -i date
date:create: 2015-03-03T14:57:26+00:00
date:modify: 2015-03-03T14:57:26+00:00
date:create: 2015-03-03T14:57:43+00:00
date:modify: 2015-03-03T14:57:43+00:00

示例2
# Create PNG and identical GIF
convert -size 100x100 xc:red r.png
convert -size 100x100 xc:red r.gif

# Compare with MD5 sums
md5 r.png r.gif
MD5 (r.png) = 692ef06b62a15b799d5dc549b0dd3737
MD5 (r.gif) = 549feea78dc438924fbb3e0ef97dc0b3 # Ooops

# Compare properly
compare -metric AE r.gif r.png null:
0 # Identical

示例3
# Create 8-bit PNG and 16-bit PNG
convert -size 100x100 xc:red PNG8:8.png
convert -size 100x100 xc:red PNG48:48.png

# MD5 sum them
md5 8.png 48.png
MD5 (8.png) = eb3fc9a06e1632c3b41ebb986b81a816
MD5 (48.png) = 32fdf1c30793a4fed941c91d27084e0a # Ooops

# Let ImageMagick compare them
compare -metric AE 8.png 48.png null:
0

图像的模糊比较

正如Kurt所暗示的,这也导致对图像进行模糊比较的可能性。我们可以像这样探索:
# Create a grey image, 100x100 and put some noise in it
convert -size 100x100 xc:gray +noise gaussian noise.png



现在,将所有像素乘以1.01,使它们的亮度提高不到1%:
# Make pixels 1% brighter
convert noise.png -evaluate multiply 1.01 brighternoise.png

# ... and compare the statistics of the two images
identify -verbose *noise* | grep -E "^Image|mean"

Image: brighternoise.png
mean: 127.235 (0.498959) <--- The brighter image is, well, brighter
Image: noise.png
mean: 126.175 (0.494805)

现在,通过几种不同的方式对它们进行比较:
# Pixels may differ by up to 2% before being considered different
compare -fuzz 2% -metric AE noise.png brighternoise.png null:
0 # All pixel values within 2% between the 2 images

# Pixels may only differ by 0.5% before being considered different
compare -fuzz 0.5% -metric AE noise.png brighternoise.png null:
594 # 594 of the 10,000 pixels differ by more than 0.5%

# Calculate Root Mean Square Error (RMSE) to see how much pixels tend to differ
compare -metric RMSE noise.png brighternoise.png null:
278.96 (0.00425666) # On average, the pixels differ by 0.4% - i.e. hardly at all

关于comparison - 使用ImageMagick对图像进行均等测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7940935/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com