Random generation of ROI's in a selected area

I have brain slices in which I will assign myself the hippocampal area of interest (so no simple rectangular, but a self-drawn shape). In this hippocampal area I would like to generate 2 random ROI’s.
Further analysis of the ROI’s can be done manually.
Can someone help me writing a macro for the random selection of ROI’s within a self-drawn ROI?
Cheers and many thanks,

Can you indicate what stereology-theory border conditions you need/have in mind?
Generating a random coordinate pair for the starting location, checking if that is within your ROI and checking if the corresponding sample area is (completely or partially) within your ROI then is trivial. Well, sort of trivial.

Hi, thanks for your help. I’m not sure what you mean with stereology-theory border conditions? I’m not so familiar with image analysis…

Stereology (the google search is because I’m not sure which articles are behind paywalls) is a collection of techniques whose objective it is to reduce the amount of labour by sub-sampling objects –possibly in a different dimension– but to come to results that are valid for the entire object. Originally developed by geologists to tell the mineral content by measuring the surface area of various minerals in a slice of rock.

It is easy to create a macro that delivers a (number of) (arbitrarily sized) area(s) to sample a region of interest, it is hard to have the data collected from these sample(s) accurately representing true quantities in the original object, be it cells in a tissue or minerals in a rock. How big need the sampling areas need to be, do they have to be entirely within the tissue, where do you place the sampling area(s), how many sampling areas do you need? Stereology has it all spelled out.

I don’t have time to code an example right now, and I’m not a mathematician so plenty of people can probably give you a more robust example of a truly ‘random’ solution, but from a “Just pick a couple of areas inside the original ROI practical sense” if I were approaching this problem, this is probably what I would do:

  • Take a freehand selection and convert it into a Spline [ Edit > Selection > Fit Spline ] and get the coordinates of the spline points (using getSelectionCoordinates(xpoints, ypoints) )


  • Find the centre of mass of the selection ( using List.setMeasurements looking for the centroid )

  • Pick a random value from the array of spline points, so now you have an imaginary line between the centre of the object and a point on the circumference.


  • Calculate the X and Y lengths of the line with respect to the centroid then pick a random point along this length for X and Y
    ASIDE: what you probably want to do is pick a random point between 0+ROIRADIUS and LENGTH-ROIRADIUS so that your ROI has a better chance of being within the original ROI.

  • Create an ROI of your choosing at that point with [ Edit > Selection > Specify ]

This will not robustly work if your original ROI has any degree of concavity, as then the ROI can appear in an area outside of your original.

The other things you will need to do to make this a (slightly more) robust solution is to check that there is no overlap between the two objects. This is not something I have ever done but off the top of my head, I would calculate the area of the ROIs (you will know this as you specify the size and shape), then take any new ROI you make, run roiManager("Combine"); to create a selection that is the logical OR of all your ROIs and measure the area of the resultant ROI. If it’s less than the sum of the individuals then you have overlap and should throw away the latest ROI and make a new one.

I’m genuinely interested in how other people would solve this as I’m sure there’s a sensible mathematical way to approach the problem. This is not it, but may work regardless.

You probably want systematic random sampling, depending on what you want to measure, a point grid can do. See this (commercial? no affiliation) site for an explanation.
In stead of point grid, you could use rectangles if you insist on “Further analysis of the ROI’s can be done manually.”. There are enough pitfalls left.

@dnmason: Implementation:
create temp image M
fill ROI area R in M with 1
place probing roi P at random location (the ‘random’ in ‘systematic random sampling’) in M
Measure roi P and compare resulting area to size of P
if equal, then P is entirely contained in M.
else not all pixels in P contain a 1 and thus P is not entirely in M.
Measure the input image using P. Move P (dx,dy) as given by the stereology rules (this is the systematic in 'systematic random sampling) and repeat.

1 Like

Hi @Lindsay_p . Firstly, the example I gave using spline points and radial random points is completely wrong, don’t do this, it’s a terrible idea.

Secondly, your questions provided grist for my blogging mill, so here’s a post I wrote about how to solve and implement the problem in a way closer to how @eljonco suggested using random X and Y coordinates (which seems like the obvious answer once you think about it).

You can find code and supporting example data in the public repo (also linked from the blog post):

Hope that helps!

Just having read your blog, I want to remark that you use random sampling rather than systematic random sampling. Delesse, 1847, shows that relative volume occupied by a certain compound is equal to the relative area in a slice of the volume. Random sampling indeed suffices there.

Thompson, 1930, then suggests to throw a point grid randomly on the slice to estimate the area. Hence Vv=Aa=Pp, or in words: volume per volume is area per area is points per points. The latter is the number of points of the grid that hit the object and the number of those points that contain the substance of interest respectively. The least amount of work is to use the point grid as probe.

To measure the surface area per volume, use lines as probe and measure the number of [intersecting points of horizontal lines with the substance] within a slice of the volume. To measure the length of object within a volume, count the intersections of a plane with the object. To determine the number of objects per volume, finally use the disector method (search term D.C. Sterio).

I hope this sums up most of the theory behind stereology.

1 Like

I really appreciate your contribution to this thread. Also, I’m having a very hard time grasping the concepts that you have introduced.

Basically, I’m trying to understand if (and why) random sampling would be inappropriate in these situations?

It sounds like Stereology deals more with determining the size and/or number of ROIs that can accurately represent the original than necessarily choosing the location of those ROIs. Is that correct?

It wasn’t me, it were Delesse, Thompson, Cavalieri and Gunderson who introduced the concepts. And they show that you don’t have to put in more work than necessary. Inappropriate is not the applicable term here, by the way.
Here is a comprehensive list (again: no affilation, might be commercials web site) of early literature on stereology. And by the looks of it, some very nice source of information and inspiration.