How to loop through RoiSet.zip in ImageJ macro

I have the following attached RoiSet.zip

RoiSet.zip (1.1 KB)

It looks like this as ROI Manager:

What I want to do is to write an ImageJ macro that

  1. opens the RoISet.zip
  2. Loops through the selection
  3. Access the name of the selection (e.g. patient-01-007-minus8wks)

Later based on the accessed name, I will open the file and process it in usual way.

How can I achieve that?

Hi @Peverall_Dubois,

there is a couple of ways for approaching this. First of all, you can turn on the Macro recorder (menu Plugins > Macros > Record) and see what gets recorded while opening ROI.zip files and selecting items:
image

Furthermore, use auto-complete to explore what you can do with the ROI Manager by entering “roim”. E.g. there is a code snippet for looping through all ROIs in the ROI Manager (by @jerome.mutterer):

And you can also discover what you can do with ROIs in the same way:

A script roughly doing what you asked for looks then like this:

// open ROIs
roiManager("Open", "C:/Users/lenovo/Downloads/RoiSet.zip");

// loop through the ROI Manager
n = roiManager('count');
for (i = 0; i < n; i++) {
    roiManager('select', i);

    // process roi here
	print (Roi.getName());
}

and prints that:
image

Not sure what you mean with that. Feel free to explain in more detail.

Let us know if this answers your question :slight_smile:

Cheers,
Robert

2 Likes

Thanks so much for your reply. Your answer gets to the point I want to make.

But upon print (Roi.getName());
I get no image open error message.

Screen Shot 2020-09-08 at 16.01.26

What’s wrong with that?

At the end of the day, based on the loop construct you suggest I want to process the image one by one.

I’m thinking of doing something in this line:

roiManager("Open", "C:/Users/lenovo/Downloads/RoiSet.zip");

// loop through the ROI Manager
n = roiManager('count');
for (i = 0; i < n; i++) {
    roiManager('select', i);

    // process roi here
	roi_name = Roi.getName();
        filename = roi_name + ".png";
        open(filename);
       run("Subtract Background...", "rolling=100 light");
      // etc etc.
     
}

Well, it tries to access the current ROI in the current image. As there is no image open at that point, it cannot do this. A workaround would be opening a blank image in the first place. Furthermore, maybe we can ask @Wayne to add a RoiManager.getName() method?

There is a method:

RoiManager.getName(index)

Returns the name of the selection with the specified index, or an empty string if the selection does not have a name.

Would that help?

You can use the RoiManager.getName(index) method to get the name of an ROI in the ROI Manager. This function does not require an open image. Here is an example:

  newImage("Untitled", "8-bit black", 400, 400, 1);
  makeRectangle(131, 72, 96, 118);
  Roi.setName("Rectangular ROI");
  roiManager("reset");
  roiManager("Add");
  close;
  name = RoiManager.getName(0);
  print("name="+name);
1 Like