Macro : problem with getResult

Tags: #<Tag:0x00007fd53f949398>



I am writing a macro. From line 273, the macro is the following. getResult give “NaN”. I try getResultString and I get “null”. But there is value in the “Mean” column.

    open(chemin+titre_RATS); // open an image, already saved and closed previously during this macro execution. Image only with black and white pixels.
    done1=false; // in order to break a loop when it is necessary (variable already created at the beginning of the macro)
    run("Clear Results"); 
    //IJ.deleteRows(0, 1)  /// I try it because I saw on internet that a bug has been found //(
    for(p=0;p<y_haut_dnapl && !done1;p+=10){  //y_haut_dnapl is created before and has a value in pixel
    	makeRectangle(X_AOI_gauche,0+p,1,y_haut_dnapl-p); // creation of a vertical rectangle with 1 pixel. The bottom of //the rectangle stay at the same place but the length of the rectangle decrease pixel by pixel. The aim is to find when //there is only white pixel (ie mean = 255)
    	run("Set Measurements...", "mean limit decimal=0");
    	w=p/10; // the aim is to determine the y where only white pixel are found. In order to do it fast, I incremented 10 by //10. But in the results table there is 10 times less lines.
    	valeur=getResult("Mean",w); // here is the problem. It found "NaN" even if there is a Value in the Mean Column.
stop = p-20;`

Now if I try the same macro but without the previous 272 lines (and open the same picture), it is working without any problem. (I already try if(getResult(“Mean”,p/10)==255){…)

X_AOI_gauche = 850;
y_haut_dnapl = 3232;
var done1 = false;
run("Clear Results");
for(p=0;p<y_haut_dnapl && !done1;p+=10){ 
	run("Set Measurements...", "mean limit decimal=5");

Thanks in advance for your help.



Bonjour Fabien,

did you try:
w = round( p / 10 );

I’m aware that
p=0;p<y_haut_dnapl && !done1;p+=10
should resullt in an integer value but with numerical data on computers you never know …




Hi Herbie,

Thanks for your quick answer.

Unfortunately, no change.




Dear @Fabien,
could you please upload the full macro?
and maybe an example image to test with.
have a nice day,
Emanuele Martini


Dear Emanuele,

Thanks for your answer.

For confidentialial reasons, i would prefer not to post the full macro, even if I know and understand that it will be complicated or impossible to help me without it.

For the image, I upload a part of the image.

The “makeRectangle” step is done on the right part of the cropped image (about 1/3 from the right border of the image). The bottom of the rectangle is in the white botton part. The top of the selection starts on the top of the image. Step by step, the top of the selection goes down and the bottom stays at the same place.When the mean is equal to 255, that means that all the selection is white and the last black pixel has been found.

Have a nice day.



Here are the 2 Results tables, save in csv.

The Result_OK.csv is the results table with the small macro.
The Result_Fail.csv is the one with the big macro.

Is it possible for someone to determine if something is wrong with the Label or someting else?



Found the solution. It seems to have a bug with run(“Clear Results”), like on this page :

I replaced run(“Clear Results”) by IJ.deleteRows(0, nResults); and everything works. I think the Clear Results corrupted the label line.


Similar problem on another part of my macro. This time neither run(“Clear Results”) or Ij.DeleteRows(0, nResults) manage to resolve the problem. What is strange, is that if I used the following macro just after start Fiji, the macro works well and getResult(“X”,0) and (“Y”,0) works well.

But if a run again the exactly same macro, getResult(“X”,0) or (“Y”,0) give NaN as result.

Is there anyone who encountered the same problem or find a solution?

run("Clear Results");
IJ.deleteRows(0, nResults);

run("FindFoci", "mask=(None) background_method=[Auto threshold] background_parameter=3.0 auto_threshold=RenyiEntropy statistics_mode=Both search_method=[Half peak value] search_parameter=0.3 minimum_size=25 minimum_above_saddle connected_above_saddle minimum_peak_height=[Absolute height] peak_parameter=1.0 sort_method=[Total intensity] maximum_peaks=1 show_mask=Peaks fraction_parameter=0.1 show_table clear_table show_log_messages remove_edge_maxima object_analysis gaussian_blur=3.0 centre_method=[Centre of mass (search image)] centre_parameter=2.0");
selectWindow(titre0 +" FindFoci");
setAutoThreshold("Default dark");
setThreshold(1, 255);
setOption("BlackBackground", true);
//run("Convert to Mask");
run("Set Measurements...", "area mean centroid limit redirect=None decimal=5");
X_centre_blanc=getResult("X",0);  // working if macro is launched just after starting ImageJ but fails (NaN) if macro is used again
Y_centre_blanc=getResult("Y",0); // working if macro is launched just after starting ImageJ but fails (NaN) if macro is used again
print(X_centre_blanc); // A number if OK or NaN if macro failed
print(Y_centre_blanc); // A number if OK or NaN if macro failed
waitForUser("Vérifiez que le blanc vous satisfait, ou modifiez le.");


Good day Fabien,

does this stand-alone macro

run("Set Measurements...", "area mean centroid limit redirect=None decimal=5");
run("Blobs (25K)");
setAutoThreshold("Default dark");

work for you?

(You need an open internet connection.)




Hi Herbie,

Thanks for your answer and sorry for the delay.

I tried your macro and the result if NaN for X and Y_center_blanc

Does it help you to understand what happened?


Fiji 1.51q42.
Java 1.8.0_144 (64 bits)
Windows 10

Sometimes your macro works. For instance, if I launch only yours.

I’m doing test in order to understand when the macro fails


Good day Fabien,

as you might have expected, I encounter no problems when running my test-macro, also when commenting the second and third line and re-running it several times: No issues here!

ImageJ 1.51p
Java 1.6.0_65 (64 bit)

What do you mean by:

[…] if I launch only yours.





In fact I launch my macro once today. It fails. Then I create a new macro and copy paste yours. I run it and it fails, as I said in my first answer.

But I tried another time yours after closing and starting fiji again and it worked.

Now I try to understand exactly when my macro fails with different tests.


I have just tested several times. If I run your macro before mine, mine always works only the first time just after yours. But if I run mine a new time just after, the new attempt fails, as if yours reinitialized something. :frowning:


This sounds to me like a state-dependent issue: in your code, you change the global configuration at one point:

Can you check if the state of this option (the Black background checkbox in Process > Binary > Options…) before running your macro makes any difference?



just one peculiar part of your macro:

setAutoThreshold("Default dark");
setThreshold(1, 255);
setOption("BlackBackground", true);

If you use

setAutoThreshold("Default dark");

The threshold is set – using the default method – for


to work.

AFAIK, there is no need for the next three lines of your code.

Make sure that your code does what you expect it to do and this
may help.




Thanks. I will try your proposals.

Just for information, If I copy paste the following, based on your macro end slightly modify to close all windows created by your macro, at the beginning of mine and run it (without changing anythin in mine), it always works.

run("Set Measurements...", "area mean centroid limit redirect=None decimal=5");
run("Blobs (25K)");
setAutoThreshold("Default dark");
run("Close All");

I try to comment all these lines. I run again : i works the first time but not after.

I check now what you propose.



Thanks for your answer. I check both of your suggestion (with black background checked or unchecked). Unfortunately, no change.

I try Herbie’s suggestion now.



Thanks for your answer. I try to comment the 3 following lines but unfortunately no change.
But if I do what I wrote in answer n° 16, it works everytime.

I really don’t understand what’s happened…

I will keep the 7 lines I added at the beginning, for lack of anything better!



please try to understand what the various macro calls really do.

I have no idea what

run("FindFoci", ...

does, but it appears that it calls

setAutoThreshold("Default dark");

You can observe what every call does if you put
after a call and look at the status of your macro.

So e.g., if
run("FindFoci", ...
does some thresholding, you would see it.

It is no good idea to load the “Blobs”-image from the internet every time you run your macro and I’m sure it is not responsible for the functioning of your macro.




If I run the macro with the following lines + the 7 lines at the beginning:

//setAutoThreshold("Default dark");
setThreshold(1, 255);
//setOption("BlackBackground", true);

it works

If I comment the 7 lines and run with the 4 previous lines, it doesn’t work.

If I comment the line “setThreshold…”, it fails even with the 7 lines uncommented.