getResult in a loop

Hello,

I am trying to measure the feret (diameter) of particles on different pictures of the same folder. I want to output one single csv file including all the measurements of this folder.
All pictures have the same black scale bar but the distance changes so I have to detect the width of the scale bar, extract this value and use it for set scale for every iteration.
This works well outside of the loop but when I integrate this into a loop, I get an error message saying: “variable width not found”.
Why does it suddenly fails to extract (getResult(“Feret”)) when I put it in the loop?

I am new to that so I thank you for your help,
Please find the code:

macro "Automated-particles-Size" {

inputFolder=getDirectory("inputFolder");

outputFolder=getDirectory("outputFolder");

list=getFileList(inputFolder);

 

setBatchMode(true);

for(i=0; i<list.length; i++) {

 path=inputFolder+list[i];

 if(endsWith(path,".jpeg")) open(path);

 showProgress(i, list.length);

 if(nImages>=1) {


  //reset scale

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

 //thresholding

run("8-bit");

run("Sharpen");

run("Find Edges");

setOption("BlackBackground", false);

run("Convert to Mask");

run("Fill Holes");

run("Remove Outliers...", "radius=10 threshold=50 which=Dark");

run("Watershed");

//scaling

run("Set Measurements...", "feret's redirect=None decimal=2");

run("Analyze Particles...", "  circularity=0.00-0.40 display exclude include add");

width=getResult("Feret");

real=14.0;

run("Set Scale...", "distance=width known=real unit=cm");

//measuring

roiManager("Delete");

Table.deleteRows(nResults-1, nResults-1);

//measuring

run("Analyze Particles...", "size=0.2-Infinity circularity=0.5-1 display exclude include add");

 }

}


 selectWindow("Results");

  outputPath=outputFolder+list[0];

  The following two lines removes the file extension

  fileExtension=lastIndexOf(outputPath,".");

  if(fileExtension!=-1) outputPath=substring(outputPath,0,fileExtension);

  saveAs("Measurements", outputPath+".csv");

run("Close"); //closes Results window

setBatchMode(false);

}

It would be great to have an example image to reproduce the error here.
One thing that I can spot from the code is that you are not using the variable width or real in this line:

It needs to be:

run("Set Scale...", "distance=" + width + " known=" + real + " unit=cm");

Maybe that fixes the issue?

If you also say that within a loop a variable cannot be found.
It can be a scope issue as well. Variables that are instantiated within loop cannot be called outside of this scope. You would need to to create the variable outside of the for loop to call it outside again.

Hi Christopher,

Thank you, I don’t think the problem comes from run(“set Scale…”) because when
I run this part of the code outside the loop it works well. Try to run it with the image opened starting from the run(“8 bit…”) until the second run(“analyze particle…”).

It appears that when I run it inside the loop for the ROI and Results windows are hidden and the program struggles to extract the “Feret” value of the Results window because the window is not displayed. But I have no idea on how to fix it.

I join you the image of the “particles” - which are actually fruit-, I added a black scale bar simply with paint for the model (don’t pay attention to the white ball). In this way the first run(“analyze particles…”) is set up in such a way it only detect object with very low roundness <0.4 .