Faster alternative to roi.contains() for overlapping Rois?

Hello
I am writing a macro and want to test which Rois in the overlap. Currently I can achieve this with roi.contains(x,y) which goes through each point individually from an array of all points in the other Rois. I want to know if there is a faster way to write a test for overlapping rois using the IJ Macro language.
I found a similar question in the old forum/mailing list, which suggests creating a mask to do this kind of test. I am stuck at figuring out how to make a roi mask or write this in macro. Any clarification for Roi masks or detecting overlapping points would be appreciated.

Hi,
This solution does not use masks, but the roiManager AND function. If you AND any two rois and get a result roi, they overlap. If you get no result, they don’t.
Here’s an example macro:

newImage("Untitled", "8-bit black", 256, 256, 1);
roiManager('reset');
makeRectangle(22, 63, 67, 75);
roiManager("Add");
makeRectangle(136, 83, 80, 72);
roiManager("Add");
makeOval(121, 125, 89, 73);
roiManager("Add");
for (i=0;i<roiManager('count');i++){
  for (j=0;j<roiManager('count');j++){
    roiManager('select',newArray(i,j));
    roiManager("AND");
    if ((i!=j)&&(selectionType>-1)) {
      print (i,j," do intersect");
    }
  }
}

Jerome.

3 Likes

Jerome,
Thanks for the suggestion. It is very straightforward and I don’t know why I hadn’t thought of it before. Unfortunately this approach doesn’t work when I try to fit it into my macro. Adding your code returns true for all pairs, even those that don’t intersect. I’ve attached part of my with your suggestion worked in, there are thresholding and ridge detection steps, the defaults work pretty well for clicking ‘ok’ all the way through them.

run("Set Scale...", "distance=0 known=0 global");
T = getTitle;
selectWindow(T);
run("Stack to Images");
//image names
selectImage(1);
redImage = getTitle();
selectImage(2);
greenImage = getTitle();
selectImage(3);
blueImage = getTitle();
imageCalculator("and", greenImage, redImage);
setAutoThreshold("Shanbhag dark");
setOption("BlackBackground", true);
fociChannel = getTitle();
//work on centromeres, blue channel
selectWindow(blueImage);
run("Threshold...");
waitForUser("Wait for Threshold...", "press OK" );
run("Convert to Mask");
run("Despeckle");
run("Dilate");
run("Analyze Particles...", "  show=Outlines display exclude summarize add");

IJ.redirectErrorMessages()
for (i=0;i<roiManager("count");i++){  //i=3
	roiManager("select",i);
	cIndex = i+1;
	roiManager("Rename", roiManager("index")+"_centromere "+ cIndex);
}
setBatchMode("hide");

centromereCount = roiManager("count");//centromere indices
print("end blue channel processing");
//end blue channel processing

//process SC (red channel)
selectWindow(redImage);
run("Convert to Mask");
run("Dilate");
run("Despeckle");
run("Skeletonize");
run("Invert LUT");

// Requires the Ridge Detection plugin from the Biomedgroup update site.
run("Ridge Detection", "line_width=2 high_contrast=255 low_contrast=240 estimate_width extend_line show_junction_points show_ids displayresults add_to_manager method_for_overlap_resolution=SLOPE sigma=1.2  lower_threshold=16.83 upper_threshold=40");

setBatchMode("hide"); // hide the UI for this computation to avoid unnecessary overhead

for (i=0;i<centromereCount;i++){  //3 centromeres
  for (j=centromereCount;j<roiManager('count');j++){
roiManager('select',newArray(i,j));
roiManager("AND");
if ((i!=j)&&(selectionType>-1)) {
  print (i,j," intersect");
}
  }
}

Maybe these functions don’t work with the specific types of ROIs or how I am adding them. I am not familiar with the ‘selectionType’ so maybe this is the issue. Thanks in advance for any insights.

Changing Jerome’s code from “selectionType” to “selectionType()” made the code work for me. Without change, then I get the same problem described by OP’s response: all pairs are reported as insecting, including those that don’t intersect.