# How can I measure ALL the shortest distances between all ROIs in an image?

I want to measure the distribution of all nearest neighbors of ROIs drawn over a micrograph. The micrograph is of a neural network and the ROIs are from Ca imaging. My goal is to get the distribution of the distances between active cells. Some are immediate neighbors, but others are far apart.

I can get from a map of the ImageJ ROIs (I create an overlay with the ROIs filled in black) to an x,y map (in Excel) of points using Analyze->Measure->Center of Mass>copy XM, YM to Excel:

Now that I have a map of all the points in an x,y plot, how can I get the distances of the nearest neighbor for each point in this x,y plot?

A while ago (last ImageJ conference) I created a tutorial for the R package Spatstat for point pattern analysis and more:

http://bio7.org/?p=2618

I created several methods Bio7 ImageJ/R interface methods to easily transfer selection and image data to R and vice versa. For some methods I had the Spatstat package in mind.

However with the condensed tutorial I hope it should’nt be a problem to feed in your data in a custom R script to measure and plot nearest neighbour distances even without Bio7.

The scripts can be found here:

``````X <- ppp(x, y, c(0,1), c(0,1))
nndist(X)
``````

from a point pattern object.

Distances from a marked point pattern could also be interesting for calculations (active cells, inactive cells)

1 Like

Thanks Marcel! I will try to work through your code. I am not familiar with Spatstat, R, or Bio7, so this may be a bit of a challenge.

If you or anyone else has an ImageJ or Excel macro that I can use, this would make life easier (for me).

There are also some ImageJ macros available which where posted on the ImageJ mailing, e.g. :

http://imagej.1557.x6.nabble.com/distribution-analysis-nearest-neighbourhood-td5004447.html

Also have a look at the BioVoxxel toolbox for some nearest neighbour visualizations and measurements:

1 Like

Hi @sroper

I have create a small KNIME workflow to solve your nearest neighbour problem.
This is the result table:

And the visual representation (candidate in green, nearest neighbour in red):

3 Likes

@sroper your post title is a bit misleading. You don’t want do get all the distances between all ROIs (with the shortest distance being the shortest line between point on the periphery of each object), do you? If the centroid distances is all that you want, @tibuch’s workflow is just right.

@tibuch the way you’re calculating the nearest distance is quite complex. I’ve used the Similarity Search node (with a neighbor count of 2 and followed by a Row Filter for `nearest neighbor index=1`) in the past for similar tasks.

1 Like

Thank you for the hint!

1 Like

This seems like a similar problem Counting dots and distance in ImageJ?

1 Like

Thanks to all those above for the help! I think I found my answer and will now plow through the various possibilities to see what works best, fastest, and easiest.

1 Like