使用 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!".
这可以用 ImageMagick 实现吗?我知道有很多方法可以获得图像之间的统计差异,但这不是我想要的.
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.
推荐答案
我个人最喜欢的是这两个:
My own favorites are these two:
compare image1 image2 -compose src diff.png
compare image1 image2 -compose src diff.pdf
上述 2 个命令之间的唯一区别:第一个将两个图像之间的视觉差异显示为 PNG 文件,第二个显示为 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.
生成的差异文件显示了所有不同的红色像素.未改变的显示为白色.
The resulting diff file displays all pixels which are different in red color. The ones which are unchanged appear white.
简短而甜蜜.
请注意,您的图片不能是同一类型.您甚至可以混合使用 JPEG、TIFF、PNG——在一种情况下:图像大小应相同(图像尺寸以像素为单位).输出格式由输出文件名的扩展名决定.
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.
如果您出于某种原因需要比默认分辨率 (72 dpi) 更高的分辨率——那么只需添加适当的 -密度
参数:
Should you, for some reason, need a higher resolution than the default one (72 dpi) -- then just add an appropriate -density
parameter:
compare -density 300 image1 image2 -compose src diff.jpeg
<小时>
图示示例
以下是上述命令变体的一些结果说明.注意:比较的两个文件是偶数 PDF 文件,因此它也适用于这些文件(只要它们是 1 页)!
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.
compare
porsche-with-scratch.pdf porsche-original.pdf
-compose src
diff-compose-default.pdf
这与我之前建议的命令相同.
This is the same command I suggested earlier above.
左: 带有文字的图像 中心:原始图片 右:seagreen"像素的差异.
Left: Image with text Center: Original image Right: Differences in 'seagreen' pixels.
compare
porsche-with-scratch.pdf porsche-original.pdf
-compose src
-highlight-color seagreen
diff-compose-default.pdf
此命令添加了一个参数,使差异像素为seagreen"而不是默认的红色.
This command adds a parameter to make the difference pixels 'seagreen' instead of the default red.
左: 带有文字的图像 中心:原始图像 右:蓝色差异(但有一些上下文背景)l>
Left: Image with text Center: Original image Right: Blue diffs (but w. some context background) l
compare
porsche-with-scratch.pdf porsche-original.pdf
-highlight-color blue
diff-compose-default.pdf
此命令删除了 -compose src
部分——结果是 compare
的默认行为,它将 2 个差异图像中的第一个保留为亮背景.(这次添加了参数,使差异像素显示为蓝色.)
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. (This time with added parameter to make the diff pixels appear in blue.)
这篇关于使用 ImageMagick “区分"图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!