Page 1 of 1

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

Posted: 2014-08-15T14:53:52-07:00
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

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

Posted: 2014-08-15T15:01:50-07:00
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

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

Posted: 2014-08-15T16:03:43-07:00
by mgupta
Replied above.

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

Posted: 2014-08-15T16:04:30-07:00
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

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

Posted: 2014-08-15T18:34:34-07:00
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.

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

Posted: 2014-08-15T19:55:23-07:00
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.

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

Posted: 2014-08-18T07:51:57-07:00
by mgupta

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

Posted: 2014-08-18T07:55:03-07:00
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.

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

Posted: 2014-08-18T07:55:52-07:00
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.

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

Posted: 2014-08-18T10:02:36-07:00
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

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

Posted: 2014-08-18T15:30:50-07:00
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).

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

Posted: 2014-08-18T15:35:50-07:00
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.

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

Posted: 2014-08-18T17:30:00-07:00
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