Batch processing with specific ROIs macro

Hi there, I am working on a macro to open images from a directory and their corresponding ROIs from a second directory (each image has a single ROI and their names are matched). Then I perform some thresholding and measure the pixel areas within the ROIs.

The code I have written works ok for the first image in the directory, but it does not proceed to the subsequent images in the folder. Can anyone help me out? Ideally it would be great if the macro proceeded through all the images and ROIs and appended the measurements to the results table each time.

Attached are 2 example images and the ROIs + the code below
ROIs.zip (3.7 KB)
1954_29_19.tif (1.6 MB)
2425B2_R1_16_TB1.tif (1.6 MB)

Thanks!
Steve

macro "Measure WEKA segmentation pixels 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); 
                open(path); 
                getRawStatistics(nPixels, mean, min, max); 
				imageTitle=getTitle();
				
								//loading ROIs here 
            path2 = dir2+list2[i];  
			roiManager("Open", path2); 
				n = roiManager("count");
  				for (j=0; j<n; j++) {
      				roiManager("select", j);
      			  				}
      				
for( k=min, n=0; k <= max; k++, n++ )
{	
	selectWindow(imageTitle);
	run("Duplicate...", "title=[to-be-thresholded]");
	selectWindow("to-be-thresholded");
	setThreshold( k, k );
	run("Convert to Mask");
	roiManager("Select", 0);
	run("Measure");
	setResult("Label", n, imageTitle); 
	close();
}
								
				roiManager("reset")
                            close(); 
                                
     //closes main image window 
                                if (isOpen("Exception")) { 
                                        selectWindow("Exception"); 
                                        run("Close");           
                                        //close error windows to avoid saving to results. 
                                        } 
}
}
                                        		

Hi Steve,

Just checking, do you have closing brackets for the file loop and the macro? They are not included in the code that was posted.

Oh yes I do, must have accidentally chopped them off the copy and paste. Have updated now

Hi Steve,

From looking quickly, it seems the problem is that you are using “i” as a variable twice. When using it in the processing your images part it is overwriting the “i” you are using in your image loop so it will now become whatever your max gray value is (because that’s the condition to end that for loop). If that number is larger than the number of images in your folder (list.length) then it will not continue with the outer loop.

If you use a different variable then hopefully it should work okay!

Good luck,
Laura

1 Like

Thanks Laura, that did indeed work. Rookie mistake on my part.

However, I now have a problem getting the filename labels to add to the results table correctly but I will make a new post for that.

Thanks again!
Steve

1 Like