How do I show difference in two images in 2 colors?

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
mgupta
Posts: 7
Joined: 2014-08-15T14:49:55-07:00
Authentication code: 6789

How do I show difference in two images in 2 colors?

Post by mgupta »

I have used compare functionality where it shows difference between 2 images in dark red and light red color.

Is there any way I can get the difference in 2 contrasting colors like red and green?


Thank you,
Megha
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I show difference in two images in 2 colors?

Post by fmw42 »

Compare only uses one color. The rest that is a perfect match is just the image with less contrast or white mixed in.

What would the two colors represent -- perfect match and anything different? You can do that with -compose difference. Then threshold and fill the white with one color and the black with another color. Or after the difference use +level-colors to apply two different colors.

see
http://www.imagemagick.org/Usage/compare/#compare
http://www.imagemagick.org/Usage/compare/#difference

Try these commands with the two images from the compare reference above.

Code: Select all

compare -metric rmse -highlight-color green1 bag_frame1.gif bag_frame2.gif bag_cmp.gif

Code: Select all

convert bag_frame1.gif bag_frame2.gif -compose difference -composite +level-colors pink,blue bag_diff.gif

Code: Select all

convert bag_frame1.gif bag_frame2.gif -compose difference -composite -threshold 0 \
-fill pink -opaque black -fill blue -opaque white  bag_diff2.gif
Or these with 3 colors:

Code: Select all

convert bag_frame1.gif bag_frame2.gif -compose difference -composite \
\( -clone 0 -threshold 0 \) \
\( -clone 0 +level-colors skyblue,blue \) \
\( -clone 1 -fill pink -opaque black \) \
-delete 0 -reverse -compose over -composite bad_diff3.gif

Code: Select all

convert bag_frame1.gif bag_frame2.gif -compose difference -composite \
\( -clone 0 -threshold 0 \) \
\( -clone 0 +level-colors blue,white \) \
\( -clone 1 -fill pink -opaque black \) \
-delete 0 -reverse -compose over -composite bad_diff4.gif
mgupta
Posts: 7
Joined: 2014-08-15T14:49:55-07:00
Authentication code: 6789

Re: How do I show difference in two images in 2 colors?

Post by mgupta »

Replied above.
Last edited by mgupta on 2014-08-15T16:05:30-07:00, edited 1 time in total.
mgupta
Posts: 7
Joined: 2014-08-15T14:49:55-07:00
Authentication code: 6789

Re: How do I show difference in two images in 2 colors?

Post by mgupta »

2 colors would reflect the content that is different in 2 images.

Say there are 2 images: imageA and imageB. And say I choose 2 colors to represent different content, red and green.

The resultant image will consist of content shown in green that is in imageA and not in imageB

and

The resultant image will also consist of content shown in red that is in imageB and not in imageA

The content which is same in both imageA and imageB will be just shown in gray color.

Basically I want to highlight the content that is different in both the images. Current functionality marks the difference with dark red and light red color which is not very distinguishable. If the light red can be replaced by some other color, it would be great.

fmw42 wrote:Compare only uses one color. The rest that is a perfect match is just the image with less contrast or white mixed in.

What would the two colors represent -- perfect match and anything different? You can do that with -compose difference. Then threshold and fill the white with one color and the black with another color. Or after the difference use +level-colors to apply two different colors.

see
http://www.imagemagick.org/Usage/compare/#compare
http://www.imagemagick.org/Usage/compare/#difference

Try these commands with the two images from the compare reference above.

Code: Select all

compare -metric rmse -highlight-color green1 bag_frame1.gif bag_frame2.gif bag_cmp.gif

Code: Select all

convert bag_frame1.gif bag_frame2.gif -compose difference -composite +level-colors pink,blue bag_diff.gif

Code: Select all

convert bag_frame1.gif bag_frame2.gif -compose difference -composite -threshold 0 \
-fill pink -opaque black -fill blue -opaque white  bag_diff2.gif
Or these with 3 colors:

Code: Select all

convert bag_frame1.gif bag_frame2.gif -compose difference -composite \
\( -clone 0 -threshold 0 \) \
\( -clone 0 +level-colors skyblue,blue \) \
\( -clone 1 -fill pink -opaque black \) \
-delete 0 -reverse -compose over -composite bad_diff3.gif

Code: Select all

convert bag_frame1.gif bag_frame2.gif -compose difference -composite \
\( -clone 0 -threshold 0 \) \
\( -clone 0 +level-colors blue,white \) \
\( -clone 1 -fill pink -opaque black \) \
-delete 0 -reverse -compose over -composite bad_diff4.gif
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I show difference in two images in 2 colors?

Post by fmw42 »

The resultant image will consist of content shown in green that is in imageA and not in imageB

and

The resultant image will also consist of content shown in red that is in imageB and not in imageA
This does not make sense to me. If two images are different, the same content (pixels) is different in both images. You would need 3 images to do what you want.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I show difference in two images in 2 colors?

Post by snibgo »

@mgupta: please show examples of images that have content in one image but not in the other. You can put them somewhere like dropbox.com and paste URLs here.

Perhaps this means there is some constant background, and you are really interested in differences from the background.
snibgo's IM pages: im.snibgo.com
mgupta
Posts: 7
Joined: 2014-08-15T14:49:55-07:00
Authentication code: 6789

Re: How do I show difference in two images in 2 colors?

Post by mgupta »

mgupta
Posts: 7
Joined: 2014-08-15T14:49:55-07:00
Authentication code: 6789

Re: How do I show difference in two images in 2 colors?

Post by mgupta »

snibgo wrote:@mgupta: please show examples of images that have content in one image but not in the other. You can put them
somewhere like dropbox.com and paste URLs here.
Please see attached images in the post. Thanks.
Last edited by mgupta on 2014-08-18T07:56:29-07:00, edited 1 time in total.
mgupta
Posts: 7
Joined: 2014-08-15T14:49:55-07:00
Authentication code: 6789

Re: How do I show difference in two images in 2 colors?

Post by mgupta »

fmw42 wrote:
The resultant image will consist of content shown in green that is in imageA and not in imageB

and

The resultant image will also consist of content shown in red that is in imageB and not in imageA
This does not make sense to me. If two images are different, the same content (pixels) is different in both images. You would need 3 images to do what you want.

See attached images to see what I am trying to accomplish. Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I show difference in two images in 2 colors?

Post by fmw42 »

You are comparing two images with the white background. That is not the same as comparing the two images. You are also getting 4 colors. A different from background, B different from background, where they are the same, and the background. You cannot do that with IM compare. You will need to do several ways to get difference from each other and the background.

This is one way to do it:

Code: Select all

convert A.png B.png \
\( -clone 0,1 -compose difference -composite \) \
\( -clone 0 -clone 2 -compose minus -composite -background red -alpha shape \) \
\( -clone 1 -clone 2 -compose minus -composite -background green1 -alpha shape \) \
\( -clone 0,1 -compose plus -composite -fill gray -opaque black \) \
-delete 0-2 -reverse -background white -compose over -flatten result.png
mgupta
Posts: 7
Joined: 2014-08-15T14:49:55-07:00
Authentication code: 6789

Re: How do I show difference in two images in 2 colors?

Post by mgupta »

fmw42 wrote:You are comparing two images with the white background. That is not the same as comparing the two images. You are also getting 4 colors. A different from background, B different from background, where they are the same, and the background. You cannot do that with IM compare. You will need to do several ways to get difference from each other and the background.

This is one way to do it:

Code: Select all

convert A.png B.png \
\( -clone 0,1 -compose difference -composite \) \
\( -clone 0 -clone 2 -compose minus -composite -background red -alpha shape \) \
\( -clone 1 -clone 2 -compose minus -composite -background green1 -alpha shape \) \
\( -clone 0,1 -compose plus -composite -fill gray -opaque black \) \
-delete 0-2 -reverse -background white -compose over -flatten result.png

The images I used were just an example. The images will not necessarily have white background. Basically I want the output of compare. But instead of dark and light red colors, I want to show changes in contrasting colors (like red and green as shown in my sample image).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I show difference in two images in 2 colors?

Post by fmw42 »

As I said before, compare only looks at two image and not the background. Compare cannot be used to get what you want. You need to do something like I did to get multiple differences and combine them.

Did you try my code on other examples, yet, to see if it works? If it does not work, then please provide URLs to a more complex background and I will take a look at it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I show difference in two images in 2 colors?

Post by fmw42 »

I have been playing around with another example that is your two images with a gradient behind them. Unless you have the actual background image, you cannot find the two horizontal bars, since they and the background are treated the same as background. That is both the bars and the background do not differ in the two images. I can get the red and green difference, but that is all.

C.png
Image

D.png
Image

Code: Select all

convert C.png D.png \
\( -clone 0,1 -compose difference -composite -threshold 0 \) \
\( -clone 0 -clone 2 -compose minus -composite -threshold 0 -fill green1 -opaque white -channel rgba -fill none -opaque black \) \
\( -clone 1 -clone 2 -compose minus -composite -threshold 0 -fill red -opaque white -channel rgba -fill none -opaque black \) \
\( -clone 0,1 -evaluate-sequence mean \) \
-delete 0-2 -reverse -background none -compose over -flatten CD_diff.png
Image
Post Reply