使用ImageMagick进行图像的平等测试 [英] Equality test of images using ImageMagick

查看:297
本文介绍了使用ImageMagick进行图像的平等测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

解决方案

ImageMagick提供比较

检查两个图像的 md5 校验和不是正确的方法,因为一些图像格式(例如,具有EXIF的PNG和JPEG)包含文件被创建的日期和时间(参见示例1),并且一些文件可以在视觉上相同,但在内部完全不同(参见示例2)具有不同的位深度(请参阅示例3)。



示例1

 #创建两个相同的图像(100x100像素,亮红色),但文件内容不同
convert -size 100x100 xc:red r1.png
convert -size 100x100 xc:红r2.png

#MD5校验他们
MD5 R'巴纽
MD5(r1.png)= 9f6d612615efd88c3fd8521d717e9811
MD5(r2.png)= 996911bec0e0da75af46a1e78c052998# Mmmm different

#询问IM告诉我们两者之间的绝对误差(不同像素数)
比较 - 测量AE r1.png r2.png null:
0#否差别 - 更好

为什么这两个不同在MD5?因为日期在...

 识别-verbose r [12] .png | grep -i date 
date:create:2015-03-03T14:57:26 + 00:00
日期:修改:2015-03-03T14:57:26 + 00:00
日期:创建:2015-03-03T14:57:43 + 00:00
日期:修改: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

#与MD5的比较
MD5 r.png r.gif
MD5(r.png)= 692ef06b62a15b799d5dc549b0dd3737
MD5(r.gif)= 549feea78dc438924fbb3e0ef97dc0b3#哎呀

#比较正常
比较-metric AE r.gif r.png null:
0#相同

示例3

 #创建8位PNG和16位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#哎呀

#让ImageMagick的比较它们
比较-metric AE 8.png 48.png空:
0

图像的模糊比较

正如Kurt所暗示的,这也导致了对图像进行模糊比较的可能性。我们可以这样探索:

 #创建一个灰色的图像,100x100,并在其中加入一些噪音
convert - size 100x100 xc:gray + noise gaussian noise.png





现在乘以1.01的所有像素,使他们潜移默化1%亮:

 #使像素1%更亮
convert noise.png -evaluate multiply 1.01 brighternoise.png

#...并比较两个图像的统计信息
identify -verbose * noise * | grep的-E^图片|意味着

图片:brighternoise.png
均值:127.235(0.498959)LT; ---在明亮的图像,没错,是光明的
图像:noise.png
mean:126.175(0.494805)

不同的方式:

 #在被认为不同之前,像素最多可能有2%不同
compare -fuzz 2% metric AE noise.png brighternoise.png null:
0#2个图像之间的所有像素值在2%之间

#像素在被认为不同之前只能相差0.5%
比较 - fuzz 0.5%-metric AE noise.png brighternoise.png null:
594#594的10,000个像素相差超过0.5%

#计算均方根误差(RMSE)看到多少像素往往相差
比较-metric RMSE noise.png brighternoise.png空:
278.96(0.00425666)#平均像素0.4%不等 - 在所有$ b $即几乎没有b


Is there any equality predicate function in ImageMagick library? I want to compare two images and find whether they are the exactly same (all colors of the pixels are the same) or have any differences.

I’ve looked around, but it seems not to have a such function. Should I write the function using pixel iterators by myself?

解决方案

ImageMagick provides the compare function to properly compare images.

Checking the md5 checksum of two images is not the correct approach, since some image formats (e.g. PNG and JPEG with EXIF for example), contain the date and time the file was created (see example 1) below, and some files can be visually identical but represented completely differently internally (see example 2), or have different bit-depths (see example 3).

Example 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

Why do these two differ in MD5? Because the date is in them...

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

Example 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

Example 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

Fuzzy Comparison of Images

As Kurt alludes to, this also leads to the possibility of doing a fuzzy compare of images. We can explore that like this:

# Create a grey image, 100x100 and put some noise in it
convert -size 100x100 xc:gray +noise gaussian noise.png

Now multiply all pixels by 1.01 to make them an imperceptible 1% brighter:

# 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)

And now compare them, a few different ways:

# 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

这篇关于使用ImageMagick进行图像的平等测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆