Batch processing with file specific ROIs?

roi-manager
batch-processing
macro
Tags: #<Tag:0x00007fa306d936a8> #<Tag:0x00007fa306d93428> #<Tag:0x00007fa306d93108>

#1

Hi Forum,

I’m a complete beginning in coding and am trying to write a macro to do the following:

  1. Open a file from Directory 1
  2. Open the ROI for that file from Directory 2 (file with same name, so they are easy to match) into ROI manager.
  3. Run a “Find maxima” command for each of the ROIs for that file. (this is where I’m stuck)
  4. Save the counts in Directory 3
  5. Empty results table.
  6. Delete ROIs from ROI manager.
  7. Return to step 1 and repeat cycle with next image file.

I’ve found a macro that someone else wrote, and am trying to combine it with a macro I recorded… and this is where I have no idea how to stitch the two together.
I would like to select ROI “0”, then “1”, “2” etc but the number of ROIs for each image varies. How can I code this into a loop?

Below is the code I’ve come up with so far, but it could be laughably wrong… please help!!

Thanks,
Ting

macro "Batch count cells" { 
	
        dir1 = getDirectory("Choose an Image Directory "); 
        dir2 = getDirectory("Choose an ROI file Directory"); 
        dir3 = getDirectory("Choose an Results Directory"); 
        setBatchMode(false); 

        list = getFileList(dir1); 
        list2 = getFileList(dir2); 
         for (i=0; i<list.length; i++) { 
                path = dir1+list[i]; 
                showProgress(i, list.length); 
                if (!endsWith(path,&quot;/&quot;)) open(path); 
                if (nImages>=1) { 

                //loading ROIs here 
                                 path2 = dir2+list2[i]; 
                                if (!endsWith(path2,"/")) 
roiManager("Open", path2); 
				for (i=0; i<n; i++) {
      			roiManager("select", i);
				run("Find Maxima...", "noise=25 output=Count light");
				roiManager("Select", i+1);
				run("Find Maxima...", "noise=25 output=Count light");
                                roiManager("Measure"); 
                                path3 = dir3+list2[i]; 
                                saveAs("Results", path3+"Results.xls"); 
                                run("Clear Results"); 
                                roiManager("Delete"); 
                                close(); 
     //closes main image window 
                                if (isOpen("Exception")) { 
                                        selectWindow("Exception"); 
                                        run("Close");           //close 
error windows to avoid saving to results. 
                                        } 

                         } 

         if (isOpen("Results")) { 
                selectWindow("Results"); 
                run("Close"); 
                } 
        } 
}

#2

Update:

I read a lot of instructions and somehow it’s now working!
For those who are also beginners like me and looking for a code that counts within drawn ROIs:

macro "Batch count cells within ROIs" { 
	
        run("Set Measurements...", "area mean min integrated limit display redirect=None decimal=3"); 
	dir1 = getDirectory("Choose an Image Directory "); 
        dir2 = getDirectory("Choose an ROI file Directory"); 
        dir3 = getDirectory("Choose an Results 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); 
        
                //loading ROIs here 
                                 path2 = dir2+list2[i];  
roiManager("Open", path2); 
				n = roiManager("count");
  				for (j=0; j<n; j++) {
      				roiManager("select", j);
				run("Find Maxima...", "noise=25 output=Count light");
				}
				roiManager("reset") 
                                close(); 
     //closes main image window 
                                if (isOpen("Exception")) { 
                                        selectWindow("Exception"); 
                                        run("Close");           //close 
error windows to avoid saving to results. 
                                        } 
		}
path3 = dir3; 
saveAs("Results", path3+"Results.xls"); 
run("Clear Results");

         if (isOpen("Results")) { 
                selectWindow("Results"); 
                run("Close"); 
               
}

#3

I am glad you got it working on your own, it is the best way to learn!

To make your code easier to read on this forum in the future, use the code button or designate it manually like this:

Edit: I see a moderator fixed it now, for this thread.


#4

@Sverre,

Thanks - I was wondering why the code was not formatting correctly.
Yes, having done it by myself, I feel at can at least try to automate some of the other processes that are bogging me down!

Best wishes,
Ting


#5

Hello both,

I came across this topic when I was looking for a way to measure the area fraction for a batch of images (which I have already thresholded and saved as a new image), with each file having its own ROI.
In directory 1 I have placed all the thresholded images, and in directory 2 all the ROIs belonging to each of the files in directory 1 (also named the files with the same name, as you @Ting_Kwok mentioned).
So basically I thought I needed the same macro as you wrote, but only to run a “Measure” command for the ROI.

This is what I have used:

macro “Batch measure area fraction within ROIs” {

run(“Set Measurements…”, “area mean min area_fraction limit redirect=None decimal=3”);
dir1 = getDirectory("Choose an Image Directory ");
dir2 = getDirectory(“Choose an ROI file Directory”);
dir3 = getDirectory(“Choose an Results 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);

//loading ROIs here
path2 = dir2+list2[i];
roiManager(“Open”, path2);
n = roiManager(“measure”);
roiManager(“reset”)
close();
//closes main image window
if (isOpen(“Exception”)) {
selectWindow(“Exception”);
run(“Close”);
//close error windows to avoid saving to results.
}
}
path3 = dir3;
saveAs(“Results”, path3+“Results.xls”);
run(“Clear Results”);

if (isOpen(“Results”)) {
selectWindow(“Results”);
run(“Close”);

}

However, only the first image and ROI seem to match with each other. Also, after a while I receive an error saying:
Index (6) out of 0-5 range in line 18:
path2 = dir2 + list2 [ i <]> ;

Could someone please help me with this?

Thank you in advance,

Emma