开发者

“Diff” an image using ImageMagick

开发者 https://www.devze.com 2023-02-14 04:51 出处:网络
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 c开发者_JS百科ompare the original to the wri

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 c开发者_JS百科ompare 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.

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.

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)

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. (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:

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. 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.

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

“Diff” an image using ImageMagick

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.


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, and netherrack.png:

“Diff” an image using ImageMagick

“Diff” an image using ImageMagick

“Diff” an image using ImageMagick

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

“Diff” an image using ImageMagick

magick netherrack.png \( diamond_ore.png stone.png -fuzz 15% -compose ChangeMask -composite +compose \) -composite nether_diamond_ore.png gives:

“Diff” an image using ImageMagick

0

精彩评论

暂无评论...
验证码 换一张
取 消