Add filename to results for batch processing ROI macro

Hi there, I have this little macro to batch process some images and ROIs but the filename of the images isn’t being correctly added to the results table.

The incorrect behavior is that only the filename of the final image processed in the directory is added to the table, and it is added to the incorrect position in the table also. In this example of two images the final image filename appears in rows 1-3, whereas it should be in rows 4-6. And the first image filename should be in rows 1-3 but is omitted. - Hope that makes sense…

If anyone can point me in the right direction it would be greatly appreciated!
Steve (146.9 KB)

macro "Measure WEKA segmentation pixels area within ROI" { 
      run("Set Measurements...", "area limit redirect=None decimal=3"); 
	dir1 = getDirectory("Choose an Image Directory "); 
        dir2 = getDirectory("Choose an ROI file Directory"); 
		setBatchMode( false )
        list = getFileList(dir1); 
        list2 = getFileList(dir2); 
         for (i=0; i<list.length; i++) { 
                path = dir1+list[i]; 
                showProgress(i, list.length); 
                getRawStatistics(nPixels, mean, min, max); 
			//loading ROIs here 
            path2 = dir2+list2[i];  
			roiManager("Open", path2); 
				n = roiManager("count");
  				for (j=0; j<n; j++) {
      				roiManager("select", j);

   // measure the pixel area of WEKA segmentation classes within ROI   		
for( k=min, n=0; k <= max; k++, n++ )
	run("Duplicate...", "title=[to-be-thresholded]");
	setThreshold( k, k );
	run("Convert to Mask");
	roiManager("Select", 0);
	setResult("Label", n, imageTitle);
       //clear and close ROI manager					
     //closes main image window 
                if (isOpen("Exception")) { 


I had this issue myself recently… for me, it was a timing issue. Things were moving too fast for the name to be written in the table in the correct place. So I would play a little with adding wait(2000); calls (using whatever wait time you wish) before writing the name into the table… then perhaps also including an updateResults(); call right after your setResult("Label", n, imageTitle);

It’s better for us to help you if you provide a Minimal, Complete and Verifiable Example (MCVE)… a short (as short as possible) snippet of code and image(s) that we can run to mimic the issue at hand.

Hi Ellen, thanks for your reply! Unfortunately adding the wait(); didn’t seem to work for me - still had the same problem. Sorry for not adding enough of the example; I have added two example images and ROIs to the original post that can be used for testing.

Thanks again,

That’s helpful @SteveBooth. Another ‘quick’ test you can try - is there a reason you aren’t just enabling the Label to be directly added via Set Measurements when you Measure?


You can add the following code call early on… run("Set Measurements...", "area limit display redirect=None decimal=3"); - to be sure the correct things are measured and added to the Results table.

1 Like

Ahhh, changing the code call that I had from “area limit” to “area limit display” and removing the setResult(“Label”, n, imageTitle) seems to have done the trick!

Thanks so much - I am clearly new to this :grimacing:

1 Like