Extract sub-ROIs from Composite ROI

If I have a Composite ROI, and I want to extract out the rectangles that compose that ROI, is there a convenient way to do so? Obviously Composite ROIs can potentially contain non-rectangular ROIs as well; my code will need to detect this situation and error out if it occurs. I’m trying to read the ROI code in ImageJ and my eyes are crossing a bit; it’s not at all clear to me how these things are put together. Any assistance would be wonderful. Thanks!

If you transfer the ROI to the ROI Manager (e.g., Press ‘t’) you can split the composite ROI with the action ‘Split’ (in the menu ‘More’) in the ROI Manager dialog.



There are also several macro commands available to use it programmatically (use the macro recorder):


To detect x,y coordinates inside a ROI:


If you use Java you can also use the ROI API:


Sorry, yes, I should have clarified: I’m writing Java code here (as part of µManager).

The solution (figured out after manually tracing the Roi code, and some experimentation in a REPL) appears to be to treat the ROI as a ShapeRoi and call its getRois() method.

EDIT: though getRois() does return a bunch of Polygon ROIs instead of Rectangle ROIs, which I’m guessing is a limitation of its implementation. Ideally I’d be able to get Rectangle ROIs back out.

In the API the following conversion rules are specified:


Those conversion rules appear to not be being followed; if I draw two non-overlapping rectangular ROIs to create a composite ROI, then call getRois() on that, then I get two Polygon ROIs instead of two Rectangle ROIs.