Macro to create ROIs in an image stack

Hello everyone,

I have a z-stack (about 20 slices). I am looking for some help to write a macro where I would like to create an ROI (polygon) in the first image of the z-stack and “clear outside” of this ROI. Now I move to the next image of the stack where I would like to create another ROI which is 10 pixels wider than the first ROI and clear outside again and would like to continue doing this till the end of the stack. Has anyone done something similar to this in the past with the help of a macro? Any help would be greatly appreciated!

Thanks!

Hi,
can you describe in more detail what you tried already and where you are stuck? The steps you want to do sound pretty straightforward.

Use the macro-recorder to record your steps (Plugins>Macro>Record...).
For clearing the outside you can use Edit > Clear Outside (don’t use the “whole stack” option).
Then move to the next slice: write Stack.setSlice(n) in your macro.
Then enlarge your ROI: Edit > Selection > Enlarge ...
Then again clear the outside … so these procedure has to be in a loop obviously.

Hi Noreen,
Thanks for the help! I tried using the macro recorder and came up with the following steps just on a single slice:


makePolygon(626,408,753,351,840,349,887,357,912,397,921,444,930,493,936,542,927,588,871,633,798,663,695,655,666,652,624,630,581,578,570,487,593,432);
setBackgroundColor(0, 0, 0);
run("Clear Outside", "slice");
Stack.setSlice(2)
run("Enlarge...", "enlarge=15");
run("Clear Outside", "slice");

I am not sure how I can setup a loop to increase the size of my ROI and clear outside the ROI it on my entire stack of images. The “Clear outside”, “slice”
clears outside of the ROI for the entire stack, what should I run to just clear outside of the ROI on just a single slice of the stack?

Hi again Noreen,
Just an update. I found a macro here: which i modified a bit to get the below macro:


 if (isOpen("ROI Manager")) {
     selectWindow("ROI Manager");
     run("Close");
  }
  setBatchMode(true);
  selectWindow("Stack-registered-1.tif");
    for (i=1; i<=nSlices; i++) {
      setSlice(i);
      makeOval(i*2, i*2, i*2, i*2);
      roiManager("add");
        }
  n = roiManager("count");
  for (i=0; i<n; i++) {
      roiManager("select", i);
      run("Clear Outside", "slice");
  }
  updateResults();
  setBatchMode(false); // displays the stack

Now the above macro creates an oval shaped ROI that increases in size along the entire stack. How can I change this to create an ROI of specific size in the image and increase its size by 10 pixels when it moves over the stack? Thanks! again!

Hi,

you are close. What you still need is to add the lines from your recordings and replace a couple of lines in your looping macro with it.

Try this macro:

// open an example image : you can delete this line and work on your own current image
run("MRI Stack");

// get name of current image
title=getTitle();

setBackgroundColor(0, 0, 0);

// go to first slice and draw a ROI
setSlice(1);
waitForUser("Draw a ROI, then click OK"); // ask user to draw the roi
// alternatively, hardcode the roi: makePolygon(100,200,....);


if (isOpen("ROI Manager")) {
     selectWindow("ROI Manager");
     run("Close");
}
setBatchMode(true);
selectWindow(title);
// create all ROIs
for (i=1; i<=nSlices; i++) {
	setSlice(i);
	if (i>=2) {// don't modify the first roi
		run("Enlarge...", "enlarge=2"); // change the stepsize!
	}
 	roiManager("add");
}

// clear the outsides
n = roiManager("count");
for (i=0; i<n; i++) {
	roiManager("select", i);
	run("Clear Outside", "slice");
}
// updateResults(); -> you don't need this here
setBatchMode("exit and display");

To understand it better you can uncomment the setbatchMode lines: Then you see how the ROI manager gets filled up.

2 Likes

Thanks Noreen! That works like a charm. Appreciate the help!

1 Like