简体   繁体   English

使用 ImageMagick “区分”图像

[英]“Diff” an image using ImageMagick

How can I get the difference between two images?我怎样才能得到两个图像之间的差异? I have the original image.我有原图。 Someone has written on an exact duplicate of the original image.有人在原始图像的精确副本上写了字。 Now, I need to compare the original to the written on image and extract just the writing in image format.现在,我需要将原始图像与写在图像上的内容进行比较,并仅提取图像格式的文字。

Example: I have a picture of a house.示例:我有一张房子的照片。 Someone took a copy and wrote “Hello.”有人拿了一份,写了“你好”。 on the copy, I want to somehow compare the two pictures, remove the house.在副本上,我想以某种方式比较两张图片,删除房子。 and be left with an image of the words “Hello!”.并留下“你好!”字样的图像。

Is this possible with ImageMagick? ImageMagick 可以做到这一点吗? I know there are ways to get the statistical difference between images, but that is not what I am looking for.我知道有一些方法可以获得图像之间的统计差异,但这不是我想要的。

My own favorites are these two: 我最喜欢的是这两个:

 compare image1 image2 -compose src diff.png
 compare image1 image2 -compose src diff.pdf

The only difference between the 2 commands above: the first one shows the visual difference between the two images as a PNG file, the second one as a PDF. 上述两个命令之间的唯一区别:第一个显示两个图像之间的视觉差异为PNG文件,第二个显示为PDF。

The resulting diff file displays all pixels which are different in red color. 生成的diff文件显示红色不同的所​​有像素。 The ones which are unchanged appear white. 未改变的那些看起来是白色的。

Short and sweet. 简短又甜蜜。

Note, your images need not be the same type. 请注意,您的图片不一定是同一类型。 You can even mix JPEG, TIFF, PNG -- under one condition: the images should be of the same size (image dimension in pixels). 您甚至可以在一种情况下混合JPEG,TIFF,PNG:图像应该具有相同的大小(图像尺寸,以像素为单位)。 The output format is determined by the output filename's extension. 输出格式由输出文件名的扩展名决定。

Should you, for some reason, need a higher resolution than the default one (72 dpi) -- then just add an appropriate -density parameter: 如果由于某种原因,您需要比默认分辨率(72 dpi)更高的分辨率 - 那么只需添加适当的-density参数:

 compare -density 300 image1 image2 -compose src diff.jpeg

Illustrated examples 插图示例

Here are a few illustrations of results for variations of the above command. 以下是对上述命令的变化的结果的一些说明。 Note: the two files compared were even PDF files , so it works with these too (as long as they are 1-pagers)! 注意:比较的两个文件甚至是 PDF文件 ,所以它也适用于这些(只要它们是1页)!


Left: Image with text Center: Original image Right: Differences (=text) in red pixels. 左:带文本的图像中心:原始图像右:红色像素的差异(=文本)。 仅红色差异像素;相同的像素是白色的

compare \
        porsche-with-scratch.pdf  porsche-original.pdf \
       -compose src \
        diff-compose-default.pdf

This is the same command I suggested earlier above. 这与我上面提到的命令相同。


Left: Image with text Center: Original image Right: Differences in 'seagreen' pixels. 左:带文本的图像中心:原始图像右: “seagreen”像素的差异。 只有Seagreen差异像素;相同的像素是白色的

compare \
        porsche-with-scratch.pdf  porsche-original.pdf \
       -compose src \
       -highlight-color seagreen \
        diff-compose-default.pdf

This command adds a parameter to make the difference pixels 'seagreen' instead of the default red. 此命令添加一个参数,使差异像素为“seagreen”而不是默认的红色。


Left: Image with text Center: Original image Right: Blue diffs (but w. some context background) 左:带文本的图像中心:原始图像右:蓝色差异(但是w。某些上下文背景) 仅蓝色差异像素;第一个比较的图像作为减轻背景 l

compare \
        porsche-with-scratch.pdf  porsche-original.pdf \
       -highlight-color blue \
        diff-compose-default.pdf

This command removes the -compose src part -- the result is the default behavior of compare which keeps as a lightened background the first one of the 2 diffed images. 此命令删除-compose src部分 - 结果是compare的默认行为,它将2个差异图像中的第一个保留为减轻背景。 (This time with added parameter to make the diff pixels appear in blue.) (这次添加了参数,使差异像素显示为蓝色。)

While compare does a good job for many applications, I found that sometimes I prefer a different approach, particularly when comparing images which are mostly grayscale: 虽然compare对于许多应用程序来说都很好,但我发现有时候我更喜欢不同的方法,特别是在比较大多数灰度级的图像时:

convert '(' file1.png -flatten -grayscale Rec709Luminance ')' \
        '(' file2.png -flatten -grayscale Rec709Luminance ')' \
        '(' -clone 0-1 -compose darken -composite ')' \
        -channel RGB -combine diff.png

The idea is follows: convert both file1.png and file2.png to grayscale. 其思路如下:将file1.pngfile2.png转换为灰度。 Then trat the first as the red channel of the resulting image, the second as the green channel. 然后将第一个作为结果图像的红色通道,第二个作为绿色通道。 The blue channel is formed from these two using the darken compose operator, which essentially means taking the minimum. 蓝色通道由这两个使用darken组合算子形成,这实际上意味着采用最小值。

So things which are white in both images stay white. 因此,两个图像中都是白色的东西都是白色的。 Things which are black in both images stay black. 两张照片中黑色的东西都保持黑色。 Things which are white in the first image but black in the second turn red, and things which are white in the second but black in the first turn green. 在第一张图像中是白色但在第二张图中是黑色的东西是红色的东西,在第二张中是白色而在第一个中是黑色的东西是绿色的。

The result gives you a nicely color-coded image where you can easily associate green with the first input and red with the second. 结果为您提供了一个漂亮的彩色编码图像,您可以轻松地将绿色与第一个输入相关联,红色与第二个输入相关联。 Here is an example where I'm using this to compare the output from LaTeX against that from KaTeX (before I fixed some bug to make this better): 这是一个例子,我用它来比较LaTeX的输出和KaTeX的输出(在我修复一些bug以使其更好之前):

在此输入图像描述

You can combine the approaches, using compare to see where something changed and then using the above to see in more detail how it changed. 您可以结合的方法,运用compare ,看看那里的东西改变了,然后用上面更详细地看看它是如何改变。

From ImageMagick 6.3.4, you can use -compose ChangeMask (see also "Removing a Known Background" and following sections).从 ImageMagick 6.3.4 开始,您可以使用-compose ChangeMask (另请参阅“删除已知背景”和以下部分)。 For example, using IM7 and these images stone.png , diamond_ore.png , and netherrack.png :例如,使用 IM7 和这些图像stone.pngdiamond_ore.pngnetherrack.png
石头.png 钻石矿石.png 地狱岩.png

magick diamond_ore.png stone.png -fuzz 15% -compose ChangeMask -composite diamond_ore_overlay.png gives: magick diamond_ore.png stone.png -fuzz 15% -compose ChangeMask -composite diamond_ore_overlay.png给出:
diamond_ore_overlay.png

magick netherrack.png \( diamond_ore.png stone.png -fuzz 15% -compose ChangeMask -composite +compose \) -composite nether_diamond_ore.png gives: magick netherrack.png \( diamond_ore.png stone.png -fuzz 15% -compose ChangeMask -composite +compose \) -composite nether_diamond_ore.png给出:
下界钻石矿石.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM