使用ImageMagick“Diff”图像 [英] “Diff” an image using ImageMagick

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

问题描述

如何区分两张图片?我有原始图像。有人写过原始图像的精确副本。现在,我需要比较原始图像和书面图像,并提取图像格式的文字。



示例:我有一张房子的照片。有人拿了一份副本,在副本上写了你好!。我想以某种方式比较两张图片,删除房子,并留下你好!字样的图像。



ImageMagick有可能吗?我知道有办法获得图像之间的统计差异,但这不是我想要的。

解决方案

我自己的收藏夹是这两个:

 比较image1 image2 -compose src diff.png 
比较image1 image2 -compose src diff。 pdf

上述两个命令之间的唯一区别:第一个显示两个图像之间的视觉差异作为PNG文件,第二个作为PDF。



生成的diff文件显示红色不同的所​​有像素。未改变的那些看起来是白色的。



短而甜蜜。



请注意,您的图片需要 属于同一类型。您甚至可以在一种情况下混合JPEG,TIFF,PNG:图像应该具有相同的大小(图像尺寸,以像素为单位)。输出格式由输出文件名的扩展名决定。



如果由于某种原因,您需要比默认分辨率(72 dpi)更高的分辨率 - 那么只需添加适当的 -density 参数:

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






插图示例



以下是对上述命令的变化的结果的一些说明。 注意:比较的两个文件甚至是 PDF文件 ,所以它也适用于这些(只要它们是1页)! / p>




左:带文字的图片      中心:原始图片      右:红色像素的差异(=文字)。

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

这与我之前建议的命令相同。






左:带文字的图片      中心:原始图片      右:'seagreen'像素的差异。

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

this命令添加一个参数,使差异像素'seagreen'而不是默认的红色。






左:带文字的图片      中心:原始图片      右:蓝色差异(但是w。某些背景背景)
l

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

此命令删除 -compose src 部分 - 结果是<$ c的默认行为$ c>比较,它将2个差异图像中的第一个保持为减轻背景。 (这次添加参数使差异像素显示为蓝色。)


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.

解决方案

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.

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:

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


Left: Image with text       Center: Original image      Right: Differences (=text) in red pixels. Red difference pixels only; identical pixels are white

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 difference pixels only; identical pixels are white

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.


Left: Image with text       Center: Original image       Right: Blue diffs (but w. some context background) Blue difference pixels only; first of the compared images as a lightened-up backgroundl

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. (This time with added parameter to make the diff pixels appear in blue.)

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

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