“差异”使用 ImageMagick 生成图像
如何获得两幅图像之间的差异?我有原始图像。有人在原始图像的精确复制品上写了字。现在,我需要将原始内容与图像上的文字进行比较,并仅提取图像格式的文字。
示例:我有一张房子的照片。有人拿了一份并写道:“你好!”在副本上。我想以某种方式比较这两张照片,去掉房子,留下一张写着“你好!”的图像。
这可以用 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? I know there are ways to get the statistical difference between images, but that is not what I am looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自己最喜欢的是这两个:
上面两个命令之间的唯一区别:第一个以 PNG 文件形式显示两个图像之间的视觉差异,第二个以 PDF 形式显示。
生成的差异文件显示所有红色不同的像素。未改变的显示为白色。
简短而甜蜜。
请注意,您的图像不必是同一类型。您甚至可以混合 JPEG、TIFF、PNG - 在一种条件下:图像应具有相同的大小(图像尺寸以像素为单位)。输出格式由输出文件名的扩展名决定。
如果出于某种原因,您需要比默认分辨率 (72 dpi) 更高的分辨率,那么只需添加适当的
-密度
参数即可:插图示例
以下是不同分辨率的结果的一些插图上面的命令。 注意:比较的两个文件是偶数 PDF 文件,因此它也适用于这些文件(只要它们是单页文件)!
左:带有文字的图像 中心:原始图像 右:红色像素中的差异(=文本)。
这与我之前建议的命令相同。
左:带有文字的图像 中心:原始图像 右:“海绿色”像素的差异。
此命令添加一个参数以使差异像素为“海绿色 ” ' 而不是默认的红色。
左:带有文字的图像 中心:原始图像 右:蓝色差异(但有一些上下文背景)
l
此命令删除
-compose src
部分——结果是compare
的默认行为,它将 2 个差异图像中的第一个图像保留为亮化背景。 (这次添加了参数以使差异像素显示为蓝色。)My own favorites are these two:
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.
The resulting diff file displays all pixels which are different in red color. 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). 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: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)!
Left: Image with text Center: Original image Right: Differences (=text) in red pixels.
This is the same command I suggested earlier above.
Left: Image with text Center: Original image Right: Differences in 'seagreen' pixels.
This command adds a parameter to make the difference pixels 'seagreen' instead of the default red.
Left: Image with text Center: Original image Right: Blue diffs (but w. some context background)
l
This command removes the
-compose src
part -- the result is the default behavior ofcompare
which keeps as a lightened background the first one of the 2 diffed images. (This time with added parameter to make the diff pixels appear in blue.)虽然
compare
对于许多应用程序来说都很好,但我发现有时我更喜欢不同的方法,特别是在比较大部分是灰度的图像时:想法如下:同时转换
file1.png< /code> 和
file2.png
为灰度。然后将第一个作为结果图像的红色通道,第二个作为绿色通道。蓝色通道是使用darken
组合运算符从这两个通道形成的,这本质上意味着取最小值。所以两个图像中白色的东西都保持白色。两张图像中黑色的东西都保持黑色。第一个图像中为白色但第二个图像中为黑色的物体会变成红色,第二个图像中为白色但第一个图像中为黑色的物体会变成绿色。
结果为您提供了一个漂亮的颜色编码图像,您可以轻松地将绿色与第一个输入关联起来,将红色与第二个输入关联起来。这是一个示例,我使用它来将 LaTeX 的输出与 KaTeX 的输出进行比较(之前我修复了一些错误以使其更好):
您可以组合使用这些方法,使用
compare
来查看更改的位置,然后使用上面的方法来更详细地查看如何更改改变了。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:The idea is follows: convert both
file1.png
andfile2.png
to grayscale. 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 thedarken
compose operator, which essentially means taking the minimum.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):
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.从 ImageMagick 6.3.4 开始,您可以使用
-compose ChangeMask
(另请参阅“删除已知背景”和以下部分)。例如,使用 IM7 和这些图像
stone.png
、diamond_ore.png
和netherrack.png
:magick Diamond_ore.png Stone.png -fuzz 15% -compose ChangeMask -composite Diamond_ore_overlay.png
给出:然后
magick netherrack.png Diamond_ore_overlay.png -composite nether_diamond_ore.png
给出:(在一个命令中:
magick netherrack.png \( Diamond_ore.png Stone.png -fuzz 15% -compose ChangeMask -composite +compose \) -composite nether_diamond_ore.png
)From ImageMagick 6.3.4, you can use
-compose ChangeMask
(see also "Removing a Known Background" and following sections).For example, using IM7 and these images
stone.png
,diamond_ore.png
, andnetherrack.png
:magick diamond_ore.png stone.png -fuzz 15% -compose ChangeMask -composite diamond_ore_overlay.png
gives:Then
magick netherrack.png diamond_ore_overlay.png -composite nether_diamond_ore.png
gives:(In one command:
magick netherrack.png \( diamond_ore.png stone.png -fuzz 15% -compose ChangeMask -composite +compose \) -composite nether_diamond_ore.png
)