Selecting multiple ROIs that falls within selection box


Hello all. I am trying to select roi (s) which are completely (x1 y1 x2 y2) stays in a selection rectangle. Currently the only way I can think of is collecting x and y coords of each roi, compare with selection box and store indexes of correct ones. As far as I know there is no way of selecting rois that fall in a selection. Can you think of any simpler method to select rois in this fashion?

Selecting grid-created ROIs based on an Initially Defined ROI

Hi @OntheWay,

you could create a composite ROI from all ROIs (Roi Manager > More > OR (Combine)) that you subsequently AND (Roi Manager > More > AND) with the selection. This will create a new (composite) selection that contains only the ROIs within the selection. If you are looking for the individual ROIs, you will have to split the composite selection up (Roi Manager > More > Split).

This is a very “high level” approach that you could also automate with an ImageJ1 macro.



Hi @stelfrich, thanks for the answer. I was unaware of such fuctions about rois. While your solution is perfect for non-overlapping rois, but it seems not useful for overlapping rectangle bounding boxes. What I need is, after drawing the selection rectangle (it will be rectangle-only), duplicate that image portion along with rois within. Then save these ROIs with the new image. I think dealing with xy coordinates one-by-one is the only method within macro functionality.


If you have a “strong” connection between the image and the ROIs, you could use an image overlay. Maybe that functionality is enough for your needs and you don’t have to take care about whether a ROI is contained in a selection:

run("Dot Blot (7K)"); // Sample image

makeOval(8, 12, 56, 56);
run("Add Selection..."); // Add selection to overlay

makeOval(159, 86, 55, 52);
run("Add Selection..."); // Add selection to overlay

makeRectangle(3, 4, 142, 137); // Make selection that only contains 1st oval ROI

run("Duplicate...", " "); // Duplicate only selected part of image

run("List Elements"); // Show list of ROIs with only the first oval ROI

You can use the the Overlay functions to add ROIs to the overlay and activate them. Also, you can iterate the ROIs in an overlay…


@stelfrich Hello, your suggestion appears to almost completely solved the problem. Its unfortunately clipping the rois if selection intersects a roi rectangle (I prefer not to include clipped rois at all) but I think I can handle that by checking x1y1 x2y2 coords and comparing them to duplicated image’s dimensions.
To do this, I need to cycle through “Overlay Elements” table and make some calc at each row, but I stuck at a possibly easy point. I was using “nResults” for row numbers in result table but despite all search, I just could not see info on how to count the number of rows in Overlay Elements table.


If you really want to operate on the entries of the “Overlay Elements” table, you’ll have to do something like this:

selectWindow("Overlay Elements");
overlayElements = getInfo(); // get contents of table as text
lines = split(overlayElements, "\n"); // split text into lines
headings = split(lines[0], ",\t"); // get column headings
for (i=0; i<lines.length; i++) { // iterate lines
	columns = split(lines[i], ",\t"); // split line into columns
	print(columns[3]); // print content of X column

If you are fine with recomputing measurements and using the ROI Manager to temporarily store the ROIs from the overlay:

run("To ROI Manager")
run("From ROI Manager")

or if you want to avoid using the ROI Manager completely:

for (i=0; i<Overlay.size; i++) {

You can use Overlay.size to get the number of ROIs in an overlay.


Pure magic! you have mentioned everything I may need, thank you so much.