AND Function Creating More ROIs

Hi all!

I added another piece of code to my macro, and the goal of this part of the code is to use the AND function in the ROI Manager to combine a bunch of small ROIs with a larger overlay to exclude all the small ROIs that are not included in the overlay. However, for some reason, when I place the overlay and exclude the ROIs outside, the number of ROIs actually increases! So the total number of smaller ROIs is 193, but when I add the overlay, the number becomes 197, which, I don’t even understand how that’s even remotely possible.

roiManager(“select”, roiSelect3);




roiManager(“Select”, 2);


print(“Split…”); // 150

roiSelect2 = newArray();

size = roiManager(“count”);

This is the code that I have used for this section. I am open to any and all advice and help!


Note: roiSelect3 will select all the mini ROIs, which have been combined into one, and the larger overlay.

Hello Joseph -

If you have “small ROIs” that cross your overlay boundary multiple
times, your process can split them into multiple pieces, increasing
your total ROI count (even if you entirely exclude some ROIs).

Thanks, mm

Hi MM!

Do you know if there is any way to bypass this? For example, can I tell ImageJ to only count them once? I am not sure how it would cross multiple times since it is in 2D and it would basically be a circular shape crossing over a boundary.


Hello Joseph -

It’s hard to know without knowing more about what you are trying
to do.

Would it be possible for you to trim things down to a small,
self-contained example – one with as few ROIs as possible?

Could you post your example image (PNG format, please),
and post the relevant ROI sets. (You can save your ROIs
from the ROI Manager – More >> Save....) Then also
post a minimal, self-contained, runnable macro that shows
the ROI processing you are trying to do.

If forum participants can reproduce the issue you are seeing,
we might be able to make suggestions about how to address it.

Thanks, mm

Hi MM!

Certainly! I appreciate the help so hopefully this will make it easier to understand!


So basically, what’s happening is the ROI in yellow, which is the larger overlay, is chopping some of the smaller ROIs in half, causing them to be counted multiple times. Is there anyway to make the program only count these ROIs once? And is it possible to set a threshold as in, if 50+% of the ROI is within the overlay, it will count it?

My goal is to exclude any ROIs outside of the yellow overlay, which are scattered about if I zoom out. To do this, I have all the mini ROIs in the ROI Manager combined into a single ROI. Next, I pull up the overlay, which I have already saved, and I perform the AND function to clear out the ROIs outside of the overlay, and that is where the total number of ROIs magically increases.

roiManager(“open”, maskSource + maskList[maskNum]); // opens the overlay

size = roiManager(“count”);

roiSelect3 = newArray();

for (x=0; x<size ; x++) // for loop to select both the combined ROIs and the overlay


roiSelect3 = Array.concat(roiSelect3 ,x);


roiManager(“select”, roiSelect3);

roiManager(“AND”); // AND function to exclude ROIs outside of the graft

roiManager(“add”); // Adds the new combined ROI to the manager

Thanks for your help!

Hello Joseph -

If your goal is to determine which of your “small” ROIs are inside
of your “overlay” ROI, or, in particular, which of them exceed
some “overlap” threshold, this combining and splitting approach
seems not so good.

Consider running the “Measure” command on a black-and-white
mask of your “overlay” ROI.

Here is a complete, runnable IJ Macro:

print (IJ.getFullVersion);

roiManager ("reset");
newImage ("ramp", "8-bit ramp", 256, 256, 1);
makeEllipse (64, 128, 192, 128, 1.0);
roiManager ("add");
makeRectangle (30, 30, 20, 20);
roiManager ("add");
makeRectangle (70, 70, 20, 20);
roiManager ("add");
makeRectangle (118, 118, 20, 20);
roiManager ("add");
makeRectangle (160, 160, 20, 20);
roiManager ("add");
roiManager ("select", 0);
roiManager ("rename", "big-roi");
roiManager ("select", 1);
roiManager ("rename", "outside-roi");
roiManager ("select", 2);
roiManager ("rename", "small-overlap");
roiManager ("select", 3);
roiManager ("rename", "inside-roi");
roiManager ("select", 4);
roiManager ("rename", "big-overlap");
roiManager ("select", 0);
run ("Create Mask");
run ("Calibrate...", "function=[Straight Line] text1=[0 255] text2=[0.00 1.00]");
run ("Set Measurements...", "mean display");
roiManager ("select", newArray (1, 2, 3, 4));
roiManager ("measure");
saveAs ("Results", "overlap_results.csv");
selectImage ("ramp");
roiManager ("show all");
run ("Flatten");
saveAs ("PNG", "overlap_image.png");

Here is the resulting illustrative image:


Here are the measured results from the Results table:

overlap_results.csv (118 Bytes)

For convenience, here is the contents of “overlap_results.csv”:


The ROIs for which the “Mean” value is greater than 1/2 have
greater than 50% overlap with the “overlay” ROI.

(Fiji / Image version is “1.53e02”.)

Thanks, mm


Hi MM!

Sorry I just realized I never replied to you~thank you so much for your help, it is much appreciated!

Joseph Chen

1 Like