10.000 rois, randomly select rois, make sure that selected ones are equally distributed (not in clusters)

Hello!

I have 100 images of high resolution histology brain samples with different sizes (around 20000x30000pxl) and am importing ROIs from a cell segmentation software. In total each slice has around 10000 cells/rois.

I am measuring all rois and plotting a representative subset of these cells directly in imagej (15%). For the plotting I only need 15 percent of the total rois, selected randomly. I do however need to make sure, that the selected ROIs are equally distributed. Some cells are in clusters, which means that more cells in a cluster are selected than in regions with thinner cell density → plotting will be influenced by density.

I am really struggling with how I can make sure to 1. “Randomly” select rois but on the other hand make sure that the x y coordinates of the selections are equally distributed. I was thinking about creating a grid and checking for each roi if it is in grid region X and if more then X rois are in this region delete the ROI. I am however struggling on how to implement this in macro.

Does somebody have an idea?
Any input is appreciated

Thank you,
Jan

If you need the ROIs to be “equally distributed” then you need to sample non-randomly following some regular pattern, e.g. every N μm. This is close to what you’re trying to do. Create a grid then move along this grid and when you pick a cell show n ROIs that are in it then move to the next cell.
If you still need random sampling but want to avoid sampling too much from clusters, you can use some form of cluster sampling, for example, assign each ROI to a cluster then first select some clusters then sample from the selected clusters (the number of ROIs picked can be variable depending on the cluster).

Hello!
Thank you for the reply, the first idea of yours is exactly what I aimed to do as well. Do you have an idea on how I could implement this in macro? Is there a select all rois in selection function? Then I can automatically create grid selections, pick all rois in a selection and randomly choose x of them.

Thank you!