# How to add two 32-bit images?

#1

Hi all:

I want to add two 32-bit images, but neither ‘Image Calculator’ nor ‘Calculator Plus’ does the job correctly. I did the following test: If I add two copies of the same image with these two plugins, I would not get the same image multiplied by 2 (using Math Plugin). Is there a way to add them that would be consistent with the Math Plugin?

Background: I want to find the average Intensity (as in Stacks>Z Project) of a big image stack. Since I’m processing a very big image sequence (>3000 images each of the size 6000*4000 pixels), I planned to do it in the following way:
Convert each image to 32-bit and divide by the number of images I have. Then I can add one image to the next, one by one, and avoid any memory issues. (Since I’m dividing by a big number, 16-bit would leave me with only ~20 levels of intensity doe the resultant image, which might not be precise enough.)
Is there an easier way to do this?

P.S. I just tried to add two 16-bit images with ‘calculator Plus’ and I still have some problem: I added (Original Image/2) to a black image, but the result is my Original Image, not (Original Image/2). Am I doing anything wrong here?

Update (3/5):
Thanks, everyone! I did the following test:

newImage(“Black”, “16-bit black”, 500, 500, 1);
newImage(“Random”, “16-bit random”, 500, 500, 1);
run(“Divide…”, “value=2”)
selectWindow(“Result of Black”);

I expect to see that “Result of Black”=“Random”/2, but visually that not the case. I see that “Result of Balck”=“Random”. The exact problem occurs when I use 32-bit images.

In the case of 8-bit images, everything is as expected: I do obtain that “Result of Black”=“Random”/2.

newImage(“Black”, “8-bit black”, 500, 500, 1);
newImage(“Random”, “8-bit random”, 500, 500, 1);
run(“Divide…”, “value=2”)
selectWindow(“Result of Black”);

I think I don’t understand what’s going on here. Can someone explain?

#2

Hi Yuchao_Chen,
By the very nature of finite number of bits that are used to represent numbers in any computer arithmetic, rounding will occur. Try to start with 32-bit images with synthetic contents, ie. generated by a macro, maybe with the help of Recorder. If you can verify that also the synthetic images are generating erroneous results, post your macro so we can recreate your synthetic images and verify the problem.
On the other hand, the synthetic images can be used to estimate the error that will be made by your approach.

#3

Good day,

from what you write in the central part of your message I conjecture that you are referrring to numerical problems that are not specific for ImageJ but for discrete mathematics in general.

Please post small image pairs that you use for your investigations (re: your PS) in TIF- or PNG-format, so we can have a look at the reasons for the observed deviations.

Regards

Herbie

#4

Thanks a lot @eljonco @Herbie ! I just updated my post with examples.

#5

Good day!

re:

``````run("Set Measurements...", "mean min redirect=None decimal=3");
newImage("Black", "16-bit black", 500, 500, 1);
newImage("Random", "16-bit random", 500, 500, 1);
run("Divide...", "value=2");
resetMinAndMax;
imageCalculator("Difference create", "Random","Result of Black");
run("Measure");
``````

I expect to see that “Result of Black”=“Random”/2, but visually that not the case.

Please note that 16bit images can’t be displayed on the available computer displays, i.e. they must be modfied at least for display purposes. This is what ImageJ does but keep in mind that the values of 16bit images are left untouched. In the above version of your demo macro I’ve introcuced the call “resetMinAndMax” which makes the display of “Random”/2 equal to that of the result image.

Furthermore, my macro version shows, by taking the difference of “Random”/2 and the result image, that both are identical.

HTH

Herbie

#6

Hi @Herbie: Thanks a lot! Now I understand. I’ll use histograms to view these images. And unchecking ‘Scale When Converting’ in Edit>Options>Conversions completely solves my issue.

Best,
Yuchao.