Mean Intensity discrepancies between manual commans and script


I wrote a really simple script to make my life easier and was not expecting to go crazy over 3 simple instructions. The point is that when i execute the same instructions manually and in the same order, the intensity is consistent with what i would expect, but when i run them in a macro, the lower intensity images assume value much higher than normal.

macro "crop"{
     file1= getDirectory("Choose a Directory FOR Channel");
    list1= getFileList(file1); 
     file2= getDirectory("Choose a Directory FOR Processed");
    list2= getFileList(file2);
     file3= getDirectory("Choose a Directory FOR Masks");
    list3= getFileList(file3);
      fileNamewe = File.nameWithoutExtension;
      nameew = fileNamewe;
      window = getImageID();
      run("Flip Vertically");
      saveAs("Tiff", file2+nameew);
      setAutoThreshold("Default dark");
      setThreshold(71, 255);
      setOption("BlackBackground", true);
      run("Convert to Mask");
      saveAs("Tiff", file3+nameew);

The part regarding the threshold works well enough, but the seemingly stupid despeckle + 8-bit transformation do something that i son’t understand.

	MI manual	MI script
1	5.395	7.05
2	4.605	25.979
3	5.273	6.544
4	5.448	5.79
5	5.176	6.844
6	4.347	17.922
7	4.534	23.646
8	5.072	5.804
9	4.952	4.695
10	5.273	7.196
11	5.078	6.422
12	4.888	5.145
13	4.501	29.572
14	5.089	6.682
15	4.869	8.096
16	5.129	4.611
17	5	6.584
18	4.915	5.894
19	4.341	25.416
20	4.98	5.641

Someone knows the answer to this mistery? Is fairly probable that i am ignoring something basics.

Thank you.

Can you break down your macro to the 3 relevant instructions and post one of the images, please?

The instructions that generate the problem are the following ones:
run("Despeckle"); window = getImageID(); selectImage(window); run("8-bit"); run("Flip Vertically"); saveAs("Tiff", file2+nameew);

This is an example of the images got as results:
-script processed:
SINVGFP mxaCh_0039.tif (1.2 MB)

-manual processed:
SINVGFP mxaCh_0039_manual.tif (1.2 MB)

SINVGFP mxaCh_0039.tif (2.3 MB)

Thank you for your help

I have applied the following steps manually and as macro an your image SINVGFP mxaCh_0039.tif.

run("Flip Vertically");

The resulting images are identical.
There is no difference in total intensity or mean intensity.
The pixels values are identical in both images.

The problem is elsewhere in your procedure!

By the way:
The original image SINVGFP mxaCh_0039.tif was 8bit already and Flip most probably will not influence the mean image intensity.

1 Like

if i redwnload the immages from the link here, i have the original at 16 bit with the larva watching up, while the other 2 are the processed, but if i just press cntrl+m to measure the mean intensity, i obtain this 3 results:

1	SINVGFP mxaCh_0039.tif (script)	                  29.572
2	SINVGFP mxaCh_0039_manual.tif (manual)	  4.501
3	SINVGFP mxaCh_0039 (1).tif	 (original)           58.883

Hi @Valerio_Laghi,

can you try to set the display min&max values (using setMinAndMax(0, 255); , with the values you want) before you convert with run("8-bit"); ? `
Maybe you have an "auto-adjust " set when opening the 16-bit image.

I see that you have a line to switch to batch mode


Is this happening when you are in batch mode or not ?

About the thresholding , it’s weird to have these 2 lines :

      setAutoThreshold("Default dark");
      setThreshold(71, 255);

as you have to choose if you use the automatic or a specific value, as it is only the last line is applied, which makes setAutoThreshold("Default dark"); useless.



Sorry! My lack of attention.

The 16-bit type and the conversion to 8-bit is your problem.

During this conversion the values between ‘min’ and ‘max’ are compressed into the range of 0…255 if the difference between min and max exceed the 8-bit range.
(min and max are here the minimum and maximum value of the current display range !)

This is the case in your situation.
The max values in the images are > 255
(original: max = 435
original + despecle: max = 404)

The min and max values can be set in the Brightness/Contrast dialog with the button Set.

If you change my macro sequence to

getMinAndMax(min, max);
setMinAndMax(0, max);
run("Flip Vertically");

you will get the same result as if you perform this steps manually.

The important manual step is to set the min and max in the Brightness/Contrast dialog.
To do this

  • first click the Reset button (this will reset the the min and max display range to the minimum and maximum value of the image)
  • then click the Set button and change the minimum value to 0

Hope this helps.

Thank you for the hint!
I tried but the results didn’t change in the way i hoped:
SINVGFP mxaCh_0039.tif (1.2 MB)
giving me a result of:

1	0039-1.tif	36.270

I thnk that in some sort of ways there is an hidden auto adjust that get applied to the script, only to the script… but no idea where and why.

Funny part is that i changed the pc 3 days ago and this is a clean brand new installation…

@romainGuiet thank you a lot for the advice!

Thank you both of you for the help until now!

The mean intensity of 36.27 is correct if the image was processed by the above macro.

You should also get this value when working manually and setting the identical min/max values (0/404). See:

If you want to get your initial result of 29.572 then use the macro

getMinAndMax(min, max);
setMinAndMax(min, max);
run("Flip Vertically");

The difference in this macro is the position of the getMinAndMax function. Because of this modfication this macro uses the minimum and maximum image intensities of the original image (before despeckle) as min/max values during 8-bit conversion (8/435).

If you want to confirm the measurement of 29.572 by manual processing you should set the min/max to 8/435.

(You can use the print functions in the macro to display the applied min/max values.)
(Also note that this macro don’t use the function resetMinAndMax(). This function is not necessary here but it is very important in the above macro to retrieve the current minimum and maximum image intensity after despeckle.)