Length measurement stops working with macro

I’m running FIJI with ImageJ version 1.50g, Java 1.8.0_66 (64bit) on a Windows 10 machine. I have a series of macros that do some thresholding and particle analysis. The goal of the analysis is to perform measurements on a large number of files and then export the results table. The images are all the same resolution so a global scale is applied. As part of the workflow a distance measurement is performed using the straight line tool and cntrl-M. After 1 file the length display on the results pane stops working (displays 0). All other measurements work fine.

I have tried removing the calibration. I have also tried using the arrows tool instead. Closing the results table and then measuring new seems to resolve the problem, but then precludes performing the analysis on a large number of files. Any thoughts??

Hello @jlynch,

Can you show us the macro or at least the part of it where you do use the straight line tool and the measurements?

This tiny example works for me:

run("Blobs (25K)");
//setTool("line");
makeLine(74, 42, 178, 99);
run("Measure");
makeLine(88, 158, 129, 200);
run("Measure");
makeLine(181, 98, 166, 135);
run("Measure");
1 Like

Absolutely- here you go. [1] has user open a file which is then duplicated. a box is created for cropping- user can modify box.
[2] Crops image, selects line tool. User is supposed to create line to measure length. User then uses freehand tool to outline blob of interest
[3] ROI turned into mask, pasted back on cropped image. Thresholding performed- user can modify
[4] Area measured, color thresholding performed, particle analysis
[5] closes it all

//declare global vars
var exportit = ""
var tmp1 = ""
var tmp2 = ""
var tmp3 = ""
var N = ""

macro "Step 1 [1]"{

open()
F = File.directory
N = File.name
len = lengthOf(N)
tmp = substring(N, 0, (len-3))
tmp1 = tmp + "dup1.jpeg"
tmp2 = tmp + "dup2.jpeg"
tmp3 = tmp + "dup2.jpeg"
a = ""
//tmp3 = tmp2 + " (blue)"

run("Set Scale...", "distance=2000 known=1 pixel=1 unit=mm global");

run("Set Measurements...", "area mean min limit display redirect=None decimal=3");

run("Duplicate...", "title=&tmp1");
selectImage(tmp1);

exportit = F + tmp + "xls"
Z = getImageID();

makeRectangle(9000, 700, 12000, 3000);
setTool("rectangle");
}

macro "Step 2 [2]"{
	
run("Crop");
run("Duplicate...", "title=&tmp2");
selectImage(tmp1);

setTool("line");
// user creates line and records length

//setTool("freehand");

}

//user should use freehand tool to create ROI

macro "Step 3 [3]"{


setPasteMode("Transparent-zero");
run("Create Mask");
run("Make Binary");
run("Copy");
selectImage(tmp2);
run("Paste");
run("Duplicate...", "title=&tmp2");
selectImage(tmp2);

//is this necessary? This step is rate limiting!!

//run("Subtract Background...", "rolling=1000 light");

run("8-bit");

run("Threshold...");
//setAutoThreshold("Default");
setThreshold(0, 240);


}



macro "Step 4 [4]"{

run("Measure");
close();

selectImage(tmp3);

//This step is rate limiting!!
//run("Subtract Background...", "rolling=1000 light");
run("Color Threshold...");
// Color Thresholder 1.47v
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=0;
max[0]=187; //187
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=0;
max[2]=150; //110
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}

// Colour Thresholding-------------



imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);

run("Make Binary");
run("Close-");
run("Analyze Particles...", "size=60-Infinity pixel circularity=0.00-1.00 show=Masks summarize");//140


}

macro "Step 5 [5]"{


run("Close All");

}

Is it here where the user draws lines and click on CTRL+M? Only once?

Yes- exactly. The first time works fine. The second time a 0 is displayed. As a side note as you make the line (second time) the length is seen in the FIJI menu. And, if you bring up “set scale” you will see that the length is also visible there.

I see, and how do you call the macros? Have you tried to run them on the Script Editor of Fiji?

Parts of it. As a side note- coding in IJ is not my area of expertise per se. I can stumble through. I’ve run parts of this through the script editor. But not as a whole. I run into issues with universal variables not being passed on (guessing this is user error).

To use the macro I will install it via: plugins -> macros -> install

Then I’ll call the macros by hitting the respective number key. The various macro calls allow a break/pause for the user to evaluate and make changes, like with the cropping and thesholding.

OK, now I have reproduced the error. It has to do with the refreshment of the Results table because the values are definitely stored. If, after measuring all your images, you change the measurements (for example by adding StDev) the Results tables will show the correct length values.

Weird… Well, I added updateResults() before the last command line in macro 5. Before run(“Close All”); That adds MinThr & MaxThr and eliminates the issue. Each measurement now appears with the cntrl-M command. Thanks!!

1 Like