Macro to select two ROI, do manipulations, then selecting next two

Hi all,

I suspect my imagej macro problem is quite trivial but I have no experience in the basic coding to figure it out. I’ve found many discussion threads about using the ROI manager to loop through all its indices and do manipulations on each individually, but not my specific problem;

I have microscopy images where each subject is two circular objects (one big, one small) which sit atop each other in various conformations. These objects are marked as individual ROIs using freehand lines in a consistent order (big, small, big, small, etc) across all of my files.

I want to make a macro which

  1. select the two ROIs of each subject (e.g. ROI index “0, 1” for the first subject)
  2. do an OR (combine),
  3. fit a rectangle,
  4. scale the rectangle slightly (so it encompasses all the pixels of the subject)
  5. duplicate the rectangle to a new image
  6. export the duplicated field as the original filename+the subject number
  7. select the two ROIs of the next subject (e.g ROI index “2, 3”)
  8. proceed through the entire ROI manager list

I have a macro that will do this for the first two ROI indexes in each list (without the automatically appended filename)

roiManager("Select", newArray(0,1));
roiManager("Combine");
run("Fit Rectangle");
run("Scale... ", "x=1.05 y=1.05 centered");
run("Duplicate...", "duplicate");
saveAs("Tiff", "xxfilepathxx/1.tif");

here is an image showing the result of the above macro and annotations.

I wonder if anyone could please point me in the correct direction with this?

I guess I have to define each subject (ROIs 0, 1; then 2, 3; then 4,5; etc) as an array, and get the manager to skip between them, at the same time using those arrays to define the filename for export (e.g subject 1, 2, 3, etc).

Unfortunately I lack the coding experience to really understand whether this is the correct terminology or sensible! :slight_smile:

Thanks in advance!