Compare two images and get the difference

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare two images and get the difference

Post by fmw42 »

lusky wrote:How many graylevels are? How much must the pixel change that the program count it. Like I sayd the number is 1,2%? If it grayscale it just for 1 level, how much must it be to count it?
I think I really need to see your images to figure out what is going on.


I think it gets changed for 1 graylevel in any rgb channel. If you are in Q16 IM that is 1 out of 65535 for any of red, green or blue channels. If you are on Q8 IM then 1 out of 256.

Please read and see the examples at http://www.imagemagick.org/Usage/compare/#compare. Even the difference between a png and jpg version of the image will see the difference.

See the comment there also:

Note however that this mask is of ANY difference, even the smallest difference. For example you can see all the minor differences that saving an image to the Lossy JPEG Format produces...

You can also make the difference image show just red and white as per the example there

compare bag_frame1.gif bag_frame2.gif \
-compose Src compare_src.gif


However, you can add -fuzz XX% to the compare. It will not change the statistics, but the diffimage will only show differences larger than that fuzz factor. -fuzz measures the rmse color distance between two colors and is therefore consistent with the -metric rmse.

Try this:

convert rose: rose.jpg
compare -metric rmse rose: rose.jpg -compose src rose_diff.png
1769.16 (0.0269957)

compare -fuzz 5% -metric rmse rose: rose.jpg -compose src rose_diff5.png
1769.16 (0.0269957)

In the first case, almost every pixel is red as there is some difference between png and jpg due to compression, even if only 1 graylevel

In the second case, much of the image is white, because it only considers coloring red where the two images have pixels that are more than 5% different in color.

If you need the statistics to show the true difference after the fuzz processing, then you need to process the output

compare -fuzz 5% -metric rmse rose: rose.jpg -compose Src -highlight-color White -lowlight-color Black rose_diff5bw.png
convert rose_diff5bw.png -format "%[fx:mean]" info:
0.0540372

This produces a b/w diffimage. Thus 5.4% are white and the rest black. So 5.4% of the pixels have changed. Those are the ones that are more than 5% fuzz factor different in color. Note that 5.4% is count and 5% is rmse color difference, i.e. not count.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare two images and get the difference

Post by fmw42 »

Anthony pointed out to me that a better way to get pixel counts is to use -metric AE. That way the resulting values from the compare are actual pixel counts. see http://www.imagemagick.org/Usage/compare/#statistics

For example:


compare -metric AE rose: rose.jpg -compose src rose_diff.png
3217

Which says that 3217 pixels are different by at least one graylevel. This out of a total number of pixels=w*h=3220. So all but 3 pixels are different. Only 3 pixels are exactly the same.

Whereas

compare -fuzz 5% -metric AE rose: rose.jpg -compose src rose_diff5.png
174

says that only 174 (out of the 3220) pixels are different.

Images rose_diff.png and rose_diff5.png show that is also the case.
lusky
Posts: 10
Joined: 2012-04-14T05:43:05-07:00
Authentication code: 8675308
Location: Lenart, Slovenia

Re: Compare two images and get the difference

Post by lusky »

Thak you for everythink. What you wrote is just perfect working. Great explanation.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Compare two images and get the difference

Post by anthony »

Now if I can get someone to write up my raw 'Compare Statistics' notes, with proper examples.
http://www.imagemagick.org/Usage/compare/#statistics
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
lusky
Posts: 10
Joined: 2012-04-14T05:43:05-07:00
Authentication code: 8675308
Location: Lenart, Slovenia

Re: Compare two images and get the difference

Post by lusky »

The examples I use are:
1.) compare -fuzz 3% -metric ae image1.jpg image2.jpg difference.jpg
2.) compare -fuzz 3% -metric ae image1.jpg image2.jpg -compose src difference.jpg
3.) compare -fuzz 3% -metric ae image1.jpg image2.jpg -compose src -highlight-color White -lowlight-color Black difference.jpg

This are the commands I use for that what I need.
Post Reply