Compare: Output image based on colour distance

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?".
Post Reply
sabrehagen
Posts: 5
Joined: 2014-04-27T19:29:24-07:00
Authentication code: 6789

Compare: Output image based on colour distance

Post by sabrehagen »

Hi,

I'm trying to compare two images. One is a screenshot, and the other is the same screenshot but after being processed by h264. I am currently using compare to show me the pixels that have changed between the two images. The output shows (as expected) in red the differing pixels. I would like the intensity of this colour to be proportionate to the colour distance between the original and encoded pixels. How would I go about doing this?

Thanks for your assistance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare: Output image based on colour distance

Post by fmw42 »

I do not think you can do that directly with compare.

If I assume you have two grayscale images, then try in unix syntax

Code: Select all

convert image1 image2 \
\( -clone 0 -clone 1 -compose difference -composite \) \
-delete 1 \
\( -clone 0 -fill red -colorize 100 \) \
-swap 1,2 -compose over -composite result
If you have color images, you need to reduce the contrast before overlaying the color

Code: Select all

convert image1 image2 \
\( -clone 0 -clone 1 -compose difference -composite -colorspace gray \) \
-delete 1 \
\( -clone 0 -fill red -colorize 100 \) \
\( -clone 0 -function polynomial "0.5 0.5" \) \
-delete 0 -reverse -compose over -composite result
If on windows remove the \ on the parens and change the line ending \ to ^.

If this does not do it, then let us know what you want differently.
sabrehagen
Posts: 5
Joined: 2014-04-27T19:29:24-07:00
Authentication code: 6789

Re: Compare: Output image based on colour distance

Post by sabrehagen »

Thanks Fred! That seems very close to what I need, except there seems to be a constant factor by which the comparison is off. I mocked up two images to test with which are linked below. One is a base of blue (RGB 0, 0, 255) and the other has progressively lighter blue blocks across it. As you can see in the attached results image, the regions that are both exactly blue in the same place in both images still show as different, or a lighter purpley blue. My goal is to have the regions of exactly the same colour as white.

By eye it seems that if the purpley blue in the result that is in the common to both areas was subtracted from the result image, this operation would do exactly what I need. How would I modify the command to achieve this?

Blue: http://i.imgur.com/HPc8eVx.png
Blocks: http://i.imgur.com/QOGGBQ9.png
Result after running your second command: http://i.imgur.com/2wdiEcG.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare: Output image based on colour distance

Post by fmw42 »

I am still unclear as to what you really want. I had toned down the first image so that I could overlay red onto it to show where the differences were with stronger red being more difference.

If you just want to see the difference where white is the same and black is totally different with shades of gray between, then try

Code: Select all

convert orig.png mod.png -compose difference -composite -colorspace gray -negate result.png

If this is not what you want, then explain further.
Post Reply