fastest way to create a `diff` image from two source images?

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
rdil

fastest way to create a `diff` image from two source images?

Post by rdil »

hey, i have two source images that are the same dimensions and mostly the same content and i want to produce an output image that contains only the pixels that are different between the two images, the rest whited out or changed to a constant color. what is the fastest (in terms of time to process the images and create the output image) way to do this? I think I could write a pixel by pixel comparison script but there must be a faster way...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: fastest way to create a `diff` image from two source ima

Post by snibgo »

I don't know the fastest, but a highly convenient method is:

convert A.gif B.gif -compose Change-mask -composite d.png
snibgo's IM pages: im.snibgo.com
rdil

Re: fastest way to create a `diff` image from two source ima

Post by rdil »

i'm not sure exactly what that does but it doesn't do what i'm talking about. the output image contains some sort of merging of the two input files. the pixels that are the same in both are still there but at a different gradient, as are the pixels that differ.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: fastest way to create a `diff` image from two source ima

Post by snibgo »

Perhaps you can post a sample pair of input files, and the command you use, and tell us which IM version you are using.

My command creates output pixels where:
- if the input pixels are the same, creates a transparent pixel;
- if the input pixels are different, uses the pixel from A.

If different pixels should use the pixel from B, just swap the input file names.

If identical pixels should produce a particular colour, use "-background {colour} -flatten".

Your requirement "an output image that contains only the pixels that are different between the two images" is a bit vague. Can you be more precise?
snibgo's IM pages: im.snibgo.com
rdil

Re: fastest way to create a `diff` image from two source ima

Post by rdil »

ah, i had the input files reversed :). thanks.
rdil

Re: fastest way to create a `diff` image from two source ima

Post by rdil »

oh, one other quick question: is there a way to make the output image a tiff? the input images are gifs.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: fastest way to create a `diff` image from two source ima

Post by fmw42 »

perhaps this will work?

convert image1 image2 -compose difference -composite result

The pixels that are exactly the same will be black and the others the difference in colors. You could recolor the exact values to some color if you want using
-fill somecolor -opaque black.

cyclops:
Image

cyclops with red dot in middle:
Image

diff image
Image

If you want to binarize the result so that black=same and white=difference, then

convert image1 image2 -compose difference -composite -threshold 0 result

binarized diff image
Image

http://www.imagemagick.org/Usage/compose/#difference
and
http://www.imagemagick.org/Usage/compare/#difference


Perhaps you can provide a link to a pair of images and your expected result.
Last edited by fmw42 on 2010-05-27T22:21:51-07:00, edited 3 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: fastest way to create a `diff` image from two source ima

Post by snibgo »

is there a way to make the output image a tiff?
Just give the output filename an extension of ".tiff" or ".tif".
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: fastest way to create a `diff` image from two source ima

Post by anthony »

fmw42 wrote:If you want to binarize the result so that black=same and white=difference, then

Code: Select all

convert image1 image2 -compose difference -composite -threshold 0 result
That can still loose information due to the greyscalign operation!

The way to get a binary image of changed pixels is to add the channels togather before thresholding.

Code: Select all

  convert   image1.png image2.png -compose difference -composite
          -channel RGBA  -separate +channel \
          -compose plus -background black -flatten \
          -threshold 0   bgnd_mask.png
See Using difference to Compare images...
http://www.imagemagick.org/Usage/compare/#difference
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply