difference between two files

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
geoland
Posts: 74
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

difference between two files

Post by geoland »

Hi.

I am attempting to show the difference between two files, image1 and image2, where image1 contains image2.

Image1 is "the image" and image2 is fixed pattern noise. I want to remove the fixed pattern noise with a template, which is image2

I'm new to imagemagick and this has me stumped.

If I apply the code below, I finish up with what looks very much like image2, when in fact what I want is what is left of image1 - image1 contains image2 - sounds like subtraction, but this too has me stumped.

Code: Select all

composite  image1 image2 -compose difference diff
Subtraction should work, but is not ideal because I have a series of images that are almost identical but not exactly. If I apply the following code the images combine with blurring because the images are not exactly aligned - and I don't want them to be. I want to return separate images for further processing, to be combined some time later.

Code: Select all

files=(*.tiff); for f in "${files[@]}"; do composite *.tiff MBIAS.tiff -compose Minus_Dst "$f"; done 
Is there a better way of doing this? I have attempted numerous commands without success - difference seems the logical approach. If I understand difference correctly.

This is a pixel math problem, but I would like to find a similar solution in imagemagick.
Version: ImageMagick 7.0.7 (latest compiled from source) Q32 x86_64 (as of) 2018 - 04 - 31 xubuntu
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: difference between two files

Post by fmw42 »

compose difference is absolute value subtraction. See http://www.imagemagick.org/Usage/compose/#difference.

I would suggest you use convert ... -composite rather than composite. composite is old and may be deprecated in IM 7.

You might find that using compare is better. See
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/
geoland
Posts: 74
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: difference between two files

Post by geoland »

Thanks fwm42.

I must admit to getting a little bit lost in the jargon.

Using a real world example, I came up with the following using convert. I think this is correct; that is, the bias is subtracted from the frame (light, flat whichever) leaving the frame minus its bias.

Code: Select all

convert frame.tiff bias.tiff -compose Minus_Dst -composite frame-bias.tiff
EDIT: should be

Code: Select all

convert frame.tiff bias.tiff -compose Minus_Src -composite frame-bias.tiff
Similarly a frame divided by a flat. But I am not getting what I expect to see; that is, removal of vignetting, dust motes etc.

Code: Select all

convert frame.tiff flat.tiff -compose Divide_Src -composite frame-divided-by-flat.tiff
I have tried swapping the image order and using _Dst instead of _Src. Not sure where I am going wrong? Now resolved...

Using convert instead of composite to overcome merging of the images out of alignment. But I can't seem to properly flat-field the frames.

EDIT: The problem I have discovered is batch processing. If I apply the lines above to single images the results are very good, excellent.

Code: Select all

files=(light*.tiff); for f in "${files[@]}"; do convert light*.tiff MDARK_$NAME.tiff -compose Minus_Src -composite "$f"; done
Returns weird results - I need to process each image separately and preferably return the result to the original image as with mogrify, but it doesn't like the options I am using.
Version: ImageMagick 7.0.7 (latest compiled from source) Q32 x86_64 (as of) 2018 - 04 - 31 xubuntu
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: difference between two files

Post by fmw42 »

The order of the images with compose minusXXX is like swapped when using composite -compose and when using convert -compose ... -composite. See the syntax change at http://www.imagemagick.org/Usage/compose/#compose.

Not sure what you want. If you want pixels that are different between the two images, then -compose difference is better and you can then -threshold the result to get a mask image. If you want only pixels that are brighter in one image than the other, then -compose minus (with the correct ordering of the two images) should do that and thresholded for the mask.

Using compare -metric XX does what you may want automatically. See http://www.imagemagick.org/Usage/compare/#methods for examples of some of the above.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: difference between two files

Post by snibgo »

Code: Select all

files=(light*.tiff); for f in "${files[@]}"; do convert light*.tiff MDARK_$NAME.tiff -compose Minus_Src -composite "$f"; done
I don't do much bash, but that looks wrong to me. Insert "-verbose" or "-debug all" after "convert" to see what arguments are being passed to convert.
snibgo's IM pages: im.snibgo.com
Post Reply