Check whether Point has an overlap with List of ROIs (single point ROI)

Dear everyone,

I am trying to establish an halfway automatized cell counter in which I can first mark all cells using the multi point tool, adding them to the ROI manager and then using a “grid” to select them in a space-dependend manner (sort of comparible to have a grid overlay and count only the ones that overlap with the grid).
What I am trying to do though is to establish “grid points” and for each of them find the closest cell that will be added for analysis by stepwise incrementing distance to this grid point and checking my list of ROIs whether there is a marked cell at this position. However, I do not know how I can use the selectionContains(x,y) in this case since I am working with single points - for some reasons it just won’t return a positive value even if I place a cell directly on the grid point. Is it only possible to use it for shape-ROIs?

my code for this part looks like this: (right now I just want to print the variable “Found” to see if it has an overlap) (marked cells are named “MG_x” previously in the script)

/// add grid in form of single points with stepwidth d

d = 50;
getDimensions(width, height, channels, slices, frames); 
y2=0;
i=1;
n1=width;
n2=height;

while (y2<=n2) {
    x2 = 0;
    while (x2<=n1) {
        makePoint(x2, y2);
        roiManager("Add");
        roiManager("Select", i+count-1);
        roiManager("Rename", "GP_"+i);    
        i = i+1;
        x2 = x2+d;
    } 
    y2 = y2+d;
} 


////// save indices from MG_ into array to select them later

count=roiManager("count");

//how many counts do we have for MG_? --> get array size
f2=0;
for (f=0; f<count; f++) { 
    name= call("ij.plugin.frame.RoiManager.getName", f);
      if(startsWith(name, "MG_")){
          f2 = f2+1;
      }}


// fill array with IDs:      
 arrayMG=newArray(f2);
 
f3=0;
for (f=0; f<count; f++) { 
      roiManager("Select", f);
    name=call("ij.plugin.frame.RoiManager.getName", f);
      if(startsWith(name, "MG_"))
      {    
          roiManager("Select", f);
          arrayMG[f3]=roiManager("index");  
      
        f3 = f3 + 1;
      }
  }
  roiManager("Select", arrayMG);


/////// Find out if @GP there is an overlap with MG
count=roiManager("count");

for (i=0; i<count; i++) { 
    name= call("ij.plugin.frame.RoiManager.getName", i);
    if(startsWith(name, "GP_")){
         roiManager("Select", i);
         getSelectionCoordinates(x, y);
         x=x[0];
         y=y[0];            //to read values out of array
         Found = 0;
         BinWidth = x+d;
         
         while(Found == 0 && x<BinWidth){
            //loop: for each pixel check if either MG_ has the same coordinates. if yes Found == "True"
            roiManager("Select", arrayMG);
            //getSelectionCoordinates(xpoints, ypoints)
            Found = selectionContains(x, y); //Roi.contains(x,y)
            print("Found: "+Found);
             //next pixel
             x=x+1;
             }
         
    }
}

Is there any easy way to check if at the selected pixel there is a Point-Roi available?

1 Like

How about just setting a minimal distance between the point in xy and the point in arrayMG?
Define a function isNear like so

function isNear(x1,y1,x2,y2, min_distance) {
    if( sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) < min_distance) {
        return true;
    } 
return false;
}

then use

getSelectionCoordinates(xpoints, ypoints);
found = isNear(xpoints[0], ypoints[0],x,y, 3); // within 3pixels

As a side note Roi.getName works just as well as call("ij.plugin.frame.RoiManager.getName", f);
you just need to have the selection selected already.

3 Likes

Thank you very much for your elegant approach!
I figured out, that the main problem was that I had the multiple points as single ROIs and not 1 single multi-point selection, thus it would always give me the coordinates of the latest selected ROI only. When I store it as a multi-point selection, the code “getSelectionCoordinates” actually works and I can use the array to compare the points to my x/y value, which has the advantage that it only gives me 1 cell per gridbox.

1 Like