Macro : problem with getResult

macro
Tags: #<Tag:0x00007fd53f949398>

#1

Hi,

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"); 
    //selectWindow("Results");
    //IJ.deleteRows(0, 1)  /// I try it because I saw on internet that a bug has been found //(https://stackoverflow.com/questions/28799630/getresult-returns-nan-for-everything-but-area)
    for(p=0;p<y_haut_dnapl && !done1;p+=10){  //y_haut_dnapl is created before and has a value in pixel
    	done1=false;
    	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");
    	run("Measure");
    	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.
    	if(valeur==255){
    		done1=true;
    	}
    }
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){ 
	done1=false;
	makeRectangle(X_AOI_gauche,0+p,1,y_haut_dnapl-p);
	run("Set Measurements...", "mean limit decimal=5");
	run("Measure");
	w=p/10;
	valeur=getResult("Mean",w);
	if(valeur==255){
		done1=true;
	}
}
stop=p-20;

Thanks in advance for your help.

Fabien


#2

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 …

Regards

Herbie


#3

Hi Herbie,

Thanks for your quick answer.

Unfortunately, no change.

Regards

Fabien


#4

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


#5

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.

Fabien


#6

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?

Thanks


#7

Found the solution. It seems to have a bug with run(“Clear Results”), like on this page : stackoverflow.com/questions/28799630/getresult-returns-nan-for-everything-but-area

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


#8

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);

selectImage(original);
//run("8-bit");
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");
run("Threshold...");
setThreshold(1, 255);
setOption("BlackBackground", true);
//run("Convert to Mask");
run("Set Measurements...", "area mean centroid limit redirect=None decimal=5");
run("Measure");
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
selectWindow(titre0);
makeRectangle(X_centre_blanc*echelle_num-120,Y_centre_blanc*echelle_num-40,240,100); 
waitForUser("Vérifiez que le blanc vous satisfait, ou modifiez le.");

#9

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");
run("Measure");
X_centre_blanc=getResult("X",0);
Y_centre_blanc=getResult("Y",0);
print(X_centre_blanc);
print(Y_centre_blanc);

work for you?

(You need an open internet connection.)

Regards

Herbie


#10

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?

Thanks

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

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

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


#11

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)
Mac

What do you mean by:

[…] if I launch only yours.

?

Regards

Herbie


#12

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.


#13

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:


#14

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?


#15

Fabien,

just one peculiar part of your macro:

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

If you use

setAutoThreshold("Default dark");

The threshold is set – using the default method – for

run("Measure");

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
https://imagej.nih.gov/ij/docs/guide/146-28.html#toc-Subsection-28.2
may help.

Regards

Herbie


#16

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("Measure");
run("Close All");
selectWindow("Results");
run("Close")

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

I check now what you propose.


#17

Jan,

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.


#18

Herbie,

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!


#19

Fabien,

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
exit();
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.

Best

Herbie


#20

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

//setAutoThreshold("Default dark");
//run("Threshold...");
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.