Run 16 bit on 32 bits images using a macro in headless mode

Hello Community,

I have an issue with a macro which converts 32 bit images in 16 bit images in headless mode. My 32 bits images background has only one value. When I apply the converting code, I see in different images, different background values whereas when I do it by opening all the images in imageJ, I have only one background value.
Here are the codes that I tried:
InputDir ="";
OutputDir="";
InputFileList = getFileList(InputDir);
for(i=174; i<=260; i++ ) {
imageName = InputFileList[i];
open(InputDir+imageName);
for( x = 0; x < getWidth(); x++ ) {
for( y = 0; y < getHeight(); y++ ) {
c = round(65535*(getPixel(x,y)-(5987.0))/(125083.0));
setPixel(x,y,c);
}
}
run(“16-bit”);
save(OutputDir+File.name);
}
exit();

or

InputDir ="";
OutputDir="";
InputFileList = getFileList(InputDir);
for(i=174; i<=260; i++ ) {
imageName = InputFileList[i];
open(InputDir+imageName);
run(“16-bit”);
save(OutputDir+File.name);
}
exit();

or

InputDir ="";
OutputDir="";
InputFileList = getFileList(InputDir);
for(i=174; i<=260; i++ ) {
imageName = InputFileList[i];
open(InputDir+imageName);
getStatistics(area,mean,min,max)
}
setMinAndMax(min,max)
run(“16-bit”);
save(OutputDir+File.name);
}
exit();

Thanks for your help!

Hi @Franck ,

as far as I can see, the versions of your code actually do very different things. For instance, version 3 uses the “setMinAndMax(min,max)” command - which just adjusts the displayed color range. For instance if you have an image with color values from 0-255 and you use setMinAndMax(100,200), ImageJ will just make all pixels <100 look black and all pixels >200 look white - but not change any of the actual pixel values. Thus, code #3 should be identical to code #2
And…Code #1 does some hardcoded gray value normalization?

Maybe what you are looking for is a gray value normalization:

run("Enhance Contrast...", "saturated=0 normalize");

could do what you want, if you apply it to your 32-bit images at first, it will rescale the gray values to a range of 0.0 - 1.0. If you convert the images to 16-bit in the next step, you should end up with images of a gray value range of 0-2^16. Is that what you are looking for?

1 Like

Hello Johannes,

Thank you for you answer. I agree with you about the fact that some codes are doing the same calculation.

I tried the normalization and it gave me the same. In fact I have a 32 bit stack of 2000 images in which the background has the same grey value in all images (let’s say 70000).

When I run 16 bit, I am supposed to get 16 bit stack of images with background of the same grey value. But it is not the case, even when I run what you proposed.!

The question is: If I find a way to have all my pixels values between 0 and 2^(16) on my 32bits images and I run 16 bit transform, do all the pixels will remain the same or they will change ? And how could I run 16 bit transform by taking into consideration the whole stack min and max ? (I think imagej does the transform by taking min a max image per image)

Greetings,

Franck

TOTAL Classification: Restricted Distribution TOTAL - All rights reserved

I haven’t run your code, but here are a couple of things I think might help.

As default ImageJ scales the conversion based on the display range for lossy down conversions. Your top example probably isn’t behaving as you expect because of this. You can switch on or off ‘scale when converting’ in the menu: Edit ▷Options ▷ Conversions

Or you can add the command to switch it off:
run("Conversions...", " ");

…and you can switch it back on with:

run("Conversions...", "scale");

In answer to your other question, if you want the stack statistics to manually set your display range, you can use this command to get statistics for the entire stack:

Stack.getStatistics(voxelCount, mean, min, max, stdDev);

Or tell the enhance contrast command (as Johannes recommended) to use the stack statistics.

run("Enhance Contrast...", "saturated=0 normalize use");

Just a word of warning though… obviously if you use different conversions for your different images, the intensities will not be cross comparable between images.