Image Subtraction

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?".
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

1. I had 2 versions, one with HDRI and one without
2. I have now uninstalled non HDRI veriosn
3. this is what is what i m getting from convert -version

C:\Program Files (x86)\ImageMagick-6.9.3-Q16-HDRI>convert -version
Version: ImageMagick 6.9.3-5 Q16 x86 2016-02-20 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangoca
iro png ps rsvg tiff webp xml zlib

4. I try adding -colorspace gray but still getting 1/3 Red Image(though image itself don't have any red spots but show only Red chennel in Level window). Still not close to what ImageJ is giving on multiply. below is my command


C:\Program Files (x86)\ImageMagick-6.9.3-Q16-HDRI>convert C:\test230216\srcdarki
m.tif C:\test230216\calibration.tif -colorspace gray -define quantum:format=floa
ting-point -compose Multiply -composite C:\test230216\multiply.tiff

multiply.tiff is available in same gdrive folder

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

Re: Image Subtraction

Post by fmw42 »

Where do srcdarkim.tif and calibration.tif originate? Are they both created by the same software?
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

srcdarkim.tif is coming from command, convert C:\test230216\SRC.tif C:\test230216\DARK.tif -compose minus_src -composite C:\test230216\srcdarkim.tif .

SRC is coming from a Frame Grabber hardware. DARK is coming from same hardware. Calibration is coming from formula {(Mean(BRIGHT)-Mean(DARK))/(BRIGHT-DARK)} , were BRIGHT and DARK are images captured using same frame grabber

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

Re: Image Subtraction

Post by fmw42 »

In HDRI mode when doing mathematical composition, you need to turn clamping off. See http://www.imagemagick.org/script/comma ... php#define

Try the following. I get a black result.

Code: Select all

convert srcdarkim.tif calibration.tif -define quantum:format=floating-point -depth 16 -define compose:clamp=off -compose multiply -composite result.tif
You probably need to use "-define quantum:format=floating-point -depth 16 -define compose:clamp=off" when doing the -compose minus as well, before doing the -compose multiply.

Also if you created calibration.tif in Imagemagick, then it also needs to have the clamping off.


EDIT: Note the correction in the -define quantum:format. Not sure I posted it correctly originally.

I would suggest you start over with the above syntax for all your images in Imagemagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image Subtraction

Post by fmw42 »

P.S.

Your calibration.tif image has values outside the range of Q16 (0-65535), i.e. NAN, so it may not be a valid image, but if it is, it must be processed as HDRI and for tif with the quantum-format set.

Code: Select all

Image: calibration.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 2790x4999+0+0
  Resolution: 1x1
  Print size: 2790x4999
  Units: PixelsPerInch
  Type: Grayscale
  Base type: Grayscale
  Endianess: MSB
  Colorspace: Gray
  Depth: 32/16-bit
  Channel depth:
    gray: 16-bit
  Channel statistics:
    Pixels: 13947210
    Gray:
      min: 160363 (2.44698)
      max: inf (inf)
      mean: inf (inf)
      standard deviation: nan (nan)
      kurtosis: nan
      skewness: nan
      entropy: nan
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

Still no luck . I tried

convert C:\test230216\src.tif C:\test230216\dark.tif -define quantum:quantum:format=floating-point -depth 16 -define compose:clamp=off -compose minus_src -composite C:\test230216\srcdarkcclamp.tif

convert C:\test230216\srcdarkcclamp.tif C:\test230216\calibration.tif -define quantum:quantum:format=floating-point -depth 16 -define compose:clamp=off -compose multiply -composite C:\test230216\result.tif

I think the bigger issue is that i m getting 1/3 Red on multiply. Which mean i m loosing some information . Rest of the image looks closer to what i need. If somehow i do not loose 2/3 of information, that will make my life easy

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

Re: Image Subtraction

Post by fmw42 »

My test above did not produce a red image. It was black. What tool are you using to view the resulting floating point tif? I am sure many viewers will not display it correctly.

Calibration is coming from formula {(Mean(BRIGHT)-Mean(DARK))/(BRIGHT-DARK)} , were BRIGHT and DARK are images captured using same frame grabber
Which tool created calibration.tif?
convert C:\test230216\src.tif C:\test230216\dark.tif -define quantum:quantum:format=floating-point -depth 16 -define compose:clamp=off -compose minus_src -composite C:\test230216\srcdarkcclamp.tif
This will subtract src from dark. I suspect you want the other way around, so add +swap after the second image if you want to subtract dark from src (i.e., src - dark). Order is very important in subtract and divide in Imagemagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image Subtraction

Post by fmw42 »

SRC is coming from a Frame Grabber hardware. DARK is coming from same hardware. Calibration is coming from formula {(Mean(BRIGHT)-Mean(DARK))/(BRIGHT-DARK)} , were BRIGHT and DARK are images captured using same frame grabber
In order for me to try to reproduce your results, I would need all these images (SRC, BRIGHT and DARK). I do not see BRIGHT on your upload directory.
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

Hi Fred, the {(Mean(BRIGHT)-Mean(DARK))/(BRIGHT-DARK)} and all attributes within it were calculated by ImageJ. But now i m trying to start from scratch using IM. But i am stuck at {floatingNumber / Image} . Please guide me on how do i divide a image with a floating point number. Once i m able to reach the final image and if its not correct, i will ask for more help.

Meanwhile i have uploaded all raw images at https://drive.google.com/folderview?id= ... sp=sharing and the formula that i need to use for final image is

FinalImage = (src-dark) * {(Average(BRIGHT)-Average(DARK)) / (BRIGHT-DARK)}

Where Bright is the average of all images in BRIGHT Folder
DARK is the average of all images in DARKFolder

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

Re: Image Subtraction

Post by fmw42 »

Where Bright is the average of all images in BRIGHT Folder
DARK is the average of all images in DARKFolder
Is that the pixel-by-pixel mean of all the images or the global mean from all the pixels in all the images? And which of the bright and dark images should be in the denominator?

Would you mind putting all the files in a zip container and uploading that so that it is easy to download them all at one time.
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

It will be pixel by pixel mean.

did you look into my image divide question, how do i divide an image with constant? I am looking for similar command to IMageJ->Process->Math->Divide

I have uploaded all images in zip file at https://drive.google.com/file/d/0B9dG4E ... sp=sharing
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image Subtraction

Post by fmw42 »

If you are doing pixel-by-pixel mean, then why do you need to divide a constant by an image?

To do so, you would create a constant image the size of the denominator for use as the numerator with the constant value for every pixel in the numerator.

Again, which images are to be subtracted in the denominator?

Please, answer my questions so I know exactly what you want me to reproduce.

Also what platform are you using? Windows syntax and Unix syntax may differ!

EDIT: Let me try to guess at what you want. Given
FinalImage = (src-dark) * {(Average(BRIGHT)-Average(DARK)) / (BRIGHT-DARK)}
You take all the images in bright folder and average them pixel-by-pixel to form BRIGHT. You do the same for all the images in dark folder to create DARK. Then you get the global mean of BRIGHT as a single value and the global mean of DARK as a single value. You then divide that global difference single value ((Average(BRIGHT)-Average(DARK)) by the pixel-by-pixel difference of (BRIGHT - DARK). Is that correct? If not, please clarify further.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image Subtraction

Post by fmw42 »

See edit above
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

You are Correct. What is your suggestion on best way of doing it?

Actually the divide operation is a recommendation from device manufacturer which is being used to create these images.

In denominator you need Average of all images in BRIGHT Folder - Average of All Images in DARK folder that i uploaded

I m on Win 8.1 64 bit
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image Subtraction

Post by fmw42 »

Here is what I have done:

In HDRI Q16:

Line 1: Get the average image from the 3 BRIGHT Images
Line 2: Get the average image from the 3 BRIGHT Images
Line 3: Get the difference image (SRC - DARK)
Line 4: Get the difference image (BRIGHT - DARK)
Line 5: Get the global average value of (BRIGHT - DARK) image as a single value 1x1 pixel, then expand that value to the size of the images. NOTE that Average of (BRIGHT - DARK) = Average (BRIGHT) - Average (DARK), since averaging is a linear process.
Line 6: Form the result of (SRC - DARK)*Ave(BRIGHT - DARK)/(BRIGHT - DARK) = SMinusD.tif*BMinusD_ave.tif/BMinusD.tif = RESULT.tif

Code: Select all

cd desktop/RawImagesForFFC
im69210hdri convert BRIGHT/*.tif -evaluate-sequence mean BRIGHT.tif
im69210hdri convert DARK/*.tif -evaluate-sequence mean DARK.tif
im69210hdri convert SRC.tif DARK.tif +swap -define quantum:format=floating-point -depth 16 -define compose:clamp=off -compose minus -composite SMinusD.tif
im69210hdri convert BRIGHT.tif DARK.tif +swap -define quantum:format=floating-point -depth 16 -define compose:clamp=off -compose minus -composite BMinusD.tif
im69210hdri convert BMinusD.tif -scale 1x1! -scale 2790x4999! -define quantum:format=floating-point -depth 16 BMinusD_ave.tif
im69210hdri convert SMinusD.tif BMinusD_ave.tif -define quantum:format=floating-point -depth 16 -define compose:clamp=off -compose multiply -composite BMinusD.tif +swap -compose divide -composite RESULT.tif
In non-HDRI Q16:

Code: Select all

cd desktop/RawImagesForFFC
convert BRIGHT/*.tif -evaluate-sequence mean BRIGHT2.tif
convert DARK/*.tif -evaluate-sequence mean DARK2.tif
convert SRC.tif DARK.tif +swap -compose minus -composite SMinusD2.tif
convert BRIGHT.tif DARK.tif +swap -compose minus -composite BMinusD2.tif
convert BMinusD.tif -scale 1x1! -scale 2790x4999! BMinusD2_ave.tif
convert SMinusD.tif BMinusD_ave.tif -compose multiply -composite BMinusD.tif +swap -compose divide -composite RESULT2.tif
Compare results:

Code: Select all

im69210hdri compare -metric rmse RESULT.tif RESULT2.tif null:
3363.45 (0.0513229)

Although none of your tif images exceed 16 bit range (0 to 65535), there is still slight difference using HDRI vs non-HDRI.

I have put all input, intermediate and output images in a zip file at http://www.fmwconcepts.com/misc_tests/For_Rathor.zip
Post Reply