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

Image Subtraction

Post by bhim.rathor »

I have been doing Image subtraction in ImageMagic but the image i am getting is not same as what i expected and it mess up up downstream image calculation. However if i do same image subtraction in ImageJ , i got what i want. If will be very helpfull if some one please let me know what is am missing. The Subtraction done through image magic has min=1462 and max=12051 while subtraction done in image magic has min=0 and max=50396 . Attached is the input images and output images through ImageMagic and ImageJ. Can some one please let me know how do i get exact same result in ImageMagic.

below is the image magic command i m using
composite C:\Images\test190216\rawimage.tif -compose minus_dst C:\Images\test190216\DARKAv.tif C:\Images\test190216\minusImageMagick.tif

Input and output images are available at
https://drive.google.com/folderview?id= ... sp=sharing

Also it would be very helpful if someone please let me know how do i do signed 32 floating point Image Arithmetic in Image Magic. I m able to do Image Arithmatic but my need is to do it in 32 bit floating point format.

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 »

I am not sure which image you want to subtract from which other. But order is important in Imagemagick.

composite requires two input images be specified before -compose minus and then the output image. See http://www.imagemagick.org/Usage/compose/#compose

I would recommend you reverse the order of your two input images and use the more current convert syntax. To subtract image2 from image1, ie (image1 - image2), you would do

Code: Select all

convert image2 image1 -compose minus -composite result
or

Code: Select all

convert image1 image2 +swap -compose minus -composite result
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

Thanks for response, just tried it. Does not make any difference to what i was getting already with minus_dest . Any other suggestion please? Also one of the challenge i m facing is image multiplication. I need to perform floating point image multiplication but so far not able to find any image magick command for it. All i could find is "composite C:\OPGImages\test190216\minus.tif -compose multiply C:\OPGImages\test190216\65DIVBRIGHT-DARK.tif C:\OPGImages\test190216\multiply.tif", which is not giving desired result( i m using Image J Process->Image Calculator -> Multiply for reference but Image J results are not matching with Image Magick with same input images). Can you or some one else help with this too please.

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 »

If you need floating point operations, then you need to compile Imagemagick as Q16 with HDRI enabled.

If you tell me which image is to be subtracted from the other such as image1 - image2 (per your image links), then I can try to do that in IM and ImageJ, tomorrow.

But if you need floating point subtraction, then you will need HDRI compilation, which you most like will need to do by compiling from source. Though some tools such as Homebrew on a Mac (I believe) allow you to compile as HDRI from their repository in an easy manner.

Please identify your version of Imagemagick and your platform.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image Subtraction

Post by snibgo »

EDIT: Comment removed. I didn't see you were using the "composite" command. Sorry.


With ImageMagic v6.9.2-5 on Windows 8.1:

Code: Select all

convert DARKAv.tif SRC.tif -compose Minus -composite m1.tif

compare -metric RMSE m1.tif minusImageJ.tif NULL:
The result from compare is:

Code: Select all

0 (0)
Using "-metric AE", the result is 0. So my result is identical to your ImageJ version.


For floating point, as Fred says, use an HDRI version. For Windows, these are available at http://www.imagemagick.org/script/binar ... hp#windows . The last time I tested the sizes of calculations, the Q16 version used 32-bit floating points.
snibgo's IM pages: im.snibgo.com
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 »

I downloaded your image subtraction images and compared statistics in Imagemagick 6.9.3.5 Q16 Mac OSX and get the same results:

Code: Select all

minusImageJ.tif[0] DirectClass Gray  channels=gray depth=16 min=0 max=0.768994 mean=0.22222 std=0.166929 27.89MB bytes

minusImageMagick.tif[0] DirectClass Gray  channels=gray depth=16 min=0 max=0.768994 mean=0.22222 std=0.166929 27.89MB bytes

Likewise, using Imagemagick 6.9.2.10 HDRI

Code: Select all

minusImageJ.tif[0] DirectClass Gray  channels=gray depth=16 min=0 max=0.768994 mean=0.22222 std=0.166929 27.89MB bytes

minusImageMagick.tif[0] DirectClass Gray  channels=gray depth=16 min=0 max=0.768994 mean=0.22222 std=0.166929 27.89MB bytes
So I am not sure why you are getting differences. How are you measuring your statistics?



FYI, best Imagemagick 6 syntax to do subtraction would be if you want ( SRC.tif - DARKAv.tif), either

Code: Select all

convert SRC.tif DARKAv.tif +swap -compose minus -composite result.tif
or

Code: Select all

convert DARKAv.tif SRC.tif  -compose minus -composite result.tif
or I suppose

Code: Select all

convert SRC.tif DARKAv.tif -compose minus_src -composite result.tif

see
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/compose/#minus
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

Hi Guys,

Thanks for your help. Need one confirmations, I downloaded and installed ImageMagick-6.9.3-Q16-HDRI. Is this same as compiling Image Magick with HDRI? I am using ImageMagick-6.9.3-Q16-HDRI now on. I also updated my minus script as per Fred's recommendations. But still acing changeless in Multiply.

When i use C:\Program Files (x86)\ImageMagick-6.9.3-Q16-HDRI>composite C:\test230216\Minus.tif -compose multiply C:\test230216\calibration.tif C:\test230216\Multiply.tif , i get a 96 bit image, which is not even loadable in ImageJ

When i use C:\Program Files (x86)\ImageMagick-6.9.3-Q16-HDRI>convert C:\test230216\minus.tif C:\test230216\calibration.tif -compose multiply -composite C:\test230216\Multiply.tif i get a 48 bit image, which is loadable in ImageJ but shows Red as predominant color.

When i do same multiplication in ImageJ , result i 16 Bit Grascale or 32 Bit Grayscale(if i select 32 bit check box) , which is what my ideal image should look like.

Can you guys please help getting same multiplication results as ImageJ too. Latest images are available at https://drive.google.com/folderview?id= ... sp=sharing

And Also , i m on Win 8.1 , 64 bit.

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 »

I downloaded and installed ImageMagick-6.9.3-Q16-HDRI. Is this same as compiling Image Magick with HDRI?
Yes.

Note that the proper syntax for composite is

Code: Select all

composite input1 input2 -compose multiply result
and for convert is

Code: Select all

convert input1 input 2 -compose multiply -composite result.
Both inputs before -compose


When working with HDRI Imagemagick and TIF format, to keep floating point accuracy, you need to add -define quantum:format=floating-point. See the TIF section of http://www.imagemagick.org/script/formats.php


I cannot seem to get your calibration.tif image to download.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image Subtraction

Post by snibgo »

Your files have a resolution of one pixel per inch, which might upset some programs.

calibration.tiff is 32-bit grayscale, with every pixel being exactly 100%. Multiplying any same-size image by this should not change the image.

Code: Select all

f:\web\im>%IM32i%convert minus.tif calibration.tif -compose Multiply -composite
m.tiff

f:\web\im>%IM32i%compare -metric RMSE minus.tif m.tiff NULL:
0 (0)
This gives the expected result.
snibgo's IM pages: im.snibgo.com
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

Hi,

I tried it, belwo are the commands and results i got

convert C:\test230216\srcdarkim.tif C:\test230216\calibration.tif -compose Multiply -composite C:\test230216\multiply.tiff -define quantum:format=floating-point ---Error
convert C:\test230216\srcdarkim.tif C:\test230216\calibration.tif -define quantum:format=floating-point -compose Multiply -composite C:\test230216\multiply.tiff ---Error
convert C:\test230216\srcdarkim.tif -define quantum:format=floating-point C:\test230216\calibration.tif -compose Multiply -composite C:\test230216\multiply.tiff --- Generated 1/3 Red image
convert C:\test230216\srcdarkim.tif C:\test230216\calibration.tif -define quantum:format=floating-point -compose Multiply -composite C:\test230216\multiply.tiff --- Generated 1/3 Red image
convert C:\test230216\srcdarkim.tif C:\test230216\calibration.tif -compose Multiply -composite C:\test230216\multiply.tiff --- Generated 1/3 Red image
Composite C:\test230216\srcdarkim.tif C:\test230216\calibration.tif -compose Multiply C:\test230216\multiply.tif --Gives 96 bit image

Still getting perfect 16 bit grayscal image by multiplying C:\test230216\srcdarkim.tif and C:\test230216\calibration.tif in ImageJ

Can you please suggest something to get it working?

Thanks
Rathor
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image Subtraction

Post by snibgo »

Your first command has bad syntax. It must end with the output filename.

What is the "---Error" in the second command?

You don't seem to have uploaded srcdarkim.tif, so I can't test with that.
snibgo's IM pages: im.snibgo.com
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

"---Error" just mean this this command execution resulting in an error

I Have uploaded srcdarkim.tif in same folder on gdrive, https://drive.google.com/folderview?id= ... sp=sharing

Please have a look and suggest something.

Thanks
Rathor
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image Subtraction

Post by snibgo »

"---Error" just mean this this command execution resulting in an error
But what is the error message?
snibgo's IM pages: im.snibgo.com
bhim.rathor
Posts: 21
Joined: 2016-02-21T13:47:15-07:00
Authentication code: 1151

Re: Image Subtraction

Post by bhim.rathor »

Sorry, let me correct myself,second command is not showing error, just showing 1/3 red image.

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 »

Try adding -colorspace gray after your two input images in the convert syntax. Does that help?

Are you sure that you do not have multiple IM versions on your system and perhaps are using an older (buggy) version?

What do you get from

Code: Select all

convert -version
Post Reply