Random pick of cells marked by multi point tool

Hey everybody, I have tried to create a macro for my image analysis but thus far I didn’t figure out the necessary tool for it.
I have tissue sections with stained cells and want to randomly pick a specific amount in a specific region after marking all of them so that I can analyse them later.

So the idea is to mark all cells of the area (blue frame) and then randomly but somewhat distributedly get about 20 cells to analyse (the marking beforehand would happen so that I can assure that only whole cells are counted as sometimes the signal comes from processes only and not from cell bodies).

Is there a way to do this? Most randomization scripts I have found so far use rectangular ROIs and I wouldn’t know how to use them for this project.

Create a selection (area of analysis) and then add the selection to the ROI Manager. You can also create a polygon with the makePolygon macro command if you want to have a macro only.

Select the choosen ROI (area of analysis) in the Roi Manager and make a particle analysis to measure only cells within the selected ROI.

Select the particle analysis option in the dialog to add the measurements to the ROI Manager, too.

Then randomly choose 20 cells by selecting them in the ROI Manager (delete or exclude the area of analysis by name before).

Something like:

count=roiManager("count");
arraySelect=newArray(20);
for (i=0; i<20; i++) {    
     arraySelect[i]=round(random()*count);         
}
roiManager("Select", arraySelect);

Eventually use another scripting language for more options for a whatever distributed pseudo random generator.

1 Like

Thank you so much for your reply! Unfortunately, the particle analyser is not a good option for me since it will take each particle and assume it is a whole cell, which is not the case in my images! Lots of these particles are process-pieces of differing length and so on. Thats why I would like to mark all cells with a present cell body and then choose randomly. Is there a way of adding multi point tool-points as single characters in the ROI manager? This way I could use your random algorithm for picking cells.

Some new API functions to do that are available with the latest ImageJ 1.51a release, see the ImageJ 1.x news page:

  • 1.51a 3 May 2016
  • […]
  • Thanks to Wilhelm Burger and Dimitri Vanhecke, added the Roi.getContainedPoints() macro function, Roi.getContainedPoints() method and Roi.getContainedFloatPoints() method (macro example, JavaScript example).

You should be able to create a new Roi for each of the contained points and add it to the Roi manager.

1 Like

In addition to the info from @imagejan

you can add ROI’s (and rename them) from the multi point selection with the following snippet:

getSelectionCoordinates(x, y);
n=x.length;
for ( i=0; i<n; i++){
    makePoint(x[i], y[i]);
    roiManager("Add");
    roiManager("Select", i);
    roiManager("Rename", "point_"+i);
}
2 Likes

Guys, thank you so much, this is exactly what I was looking for! You made my day!
Can I add one last question: I found a code for removing all ROIs except the selected ones, but it is for a plugin, not a macro (I will have lots of breaks in between in which the user will measure the length of the process of the selected cells, thus I thought a macro might be more flexible?), do you know of the equivalent for the macro?

rm = roiManager.getInstance(); // this code is not accepted in IJ1-macros
  indexes = new Array();
  index=0;
  for (i=0; i<rm.getCount(); i++) {
     if (!rm.isSelected(i))
        indexes[index++] = i;
  }
  rm.setSelectedIndexes(indexes);
  rm.runCommand("delete”);

Look at the macro API:

http://imagej.net/developer/macro/functions.html#R

roiManager(cmd)

e.g.,

roiManager("Delete")

The necessary selection in a loop is already described in an answer above.

I think the problem is the lack of a macro equivalent for rm.isSelected(i), or am I missing something?

I thought he meant to delete the random selected ROI’s. Maybee @ghastliness could clarify this.

No, I actually want to delete all the ROIs that are NOT selected so that I can visualize the cells to be analysed and I haven’t found the equivalent to the rm.isSelected(i) for a macro or any other way of doing this (apart from maybe saving the selected rois, deleting all and loading them again, but this doesn’t seem very elegant and useful to me since I will have >200 files)

As a workaround you can give your random selections names. Then you can identify the ROI’s with the following snippet and delete the other ROI’s.

Example to delete a specific named ROI (create multipoint selections before):

getSelectionCoordinates(x, y);
n=x.length;
for ( i=0; i<n; i++){
    makePoint(x[i], y[i]);
    roiManager("Add");
    roiManager("Select", i);
    roiManager("Rename", "point_"+i);
}
count=roiManager("count");
for (i=0; i<count; i++) { 
    name= call("ij.plugin.frame.RoiManager.getName", i);
    if(startsWith(name, "point_1")){
         roiManager("Select", i);
         roiManager("Delete")
    }
}

Would that help?

Unfortunately the isSelected method is not a static method which then could be called with the call command.

Another option would be to call another scripting language in the macro to get the values. Something like:

count=roiManager("count");
  for ( i=0; i<count; i++){
     isSelected = eval("js","RoiManager.getInstance().isSelected("+i+")");
     print(isSelected);
}

As suggested by this answer:

However this is bit slow. It would be faster to get all selection indices at once.

1 Like