Distance between polygons

Hello,

I would like to measure the distances between the two membranes of bacteria.
I can do it manually but this is fastidious as it results in 400-500 measures per bacterium.
I figured that I can easily delimits the membranes with the polygon function but I cannot find a way to measure distances between the two polygons at defined intervals.

Sorry if it is obvious, but I am only starting to learn how to use ImageJ

Thank you for your help,

Pierre

Hello Pierre -

I’m not sure exactly what you’re asking, and specifically
which distances you want to calculate. If you could post
a sample image and offer a few more words of explanation,
that could help.

If I guess what you might want to do, I’m not sure that
there is a built-in tool for you, but if you’re comfortable
working with a script or macro, you can probably do what
you want.

Here’s a jython (python) script that calculates the distances
between all pairs of points on the boundaries of two polygons.
It illustrates the kinds of things you can do and might help
you get started with your specific task.

You can run it by opening the script editor:
File > New > Script...
setting the Language to Python, pasting the code into
the script editor, and clicking the Run button.

Here is the code:

from ij import IJ
from ij.gui import Overlay
from ij.gui import Roi, PolygonRoi

# create two PolygonRois
xp = [50, 100, 100, 50]
yp = [50, 50, 100, 100]
roip = PolygonRoi (xp, yp, 4, Roi.POLYGON)

xq = [150, 175, 200, 175]
yq = [175, 150, 175, 200]
roiq = PolygonRoi (xq, yq, 4, Roi.POLYGON)

# get points along the boundaries of the polygons, not just the corners
# (space the points five pixels apart)
fpp = roip.getInterpolatedPolygon (5.0, False)
fpq = roiq.getInterpolatedPolygon (5.0, False)

print 'number of points along first polygon boundary =', fpp.npoints
print 'number of points along second polygon boundary =', fpq.npoints

# print out distances between all pairs of boundary points
# (only use five points each instead of the whole boundaries)
print 'distances = ...'
for  i in range (5):       # for whole polygon, use fpp.npoints
    for  j in range (5):   # for whole polygon, use fpq.npoints
        dist  = (fpp.xpoints[i] - fpq.xpoints[j])**2.0
        dist += (fpp.ypoints[i] - fpq.ypoints[j])**2.0
        dist = dist**0.5
        print dist

# display the PolygonRois as an overlay on an image
# just so we can see what we're doing
imp = IJ.createImage ('polygons', '8-bit ramp', 256, 256, 1)

ovr = Overlay()
ovr.add (roip)
ovr.add (roiq)

imp.setOverlay (ovr)

imp.show()

Thanks, mm

Hello,

Thank you for your answer.
Here is an image of my “problem”.
On the left, the two membranes are shown by the red arrows. That’s the space in between that I would like to measure. So far, only managed what is shown on the upper right side: drawing lines more or less regularly and measure the length. As I said, fastidious.
However, I can “highlight” the membranes with polygons as shown on the left lower side. And that is the space separating both polygons that I would like to measure at regular intervals in a more automated way.

problem

I hope it makes more sense with the image.

Best,

Pierre

Create ROI’s from the inner and outer membrane and add them to the ROI Manager.

For the outer membrane use a segmented line (note that a segmented line or polygonal selection can also be interpolated with a Spline selection - Edit->Selection-> Fit Spline)!

Create a binary image from the selected ROI (inner membrane) in the ROI Manager.

Follow the instructions of this post to create a ‘Exact Signed Distance Euclidean’ image to create distance valued pixels:

Select the outer membrane ROI (segmented line!) in the ROI Manager and measure the pixel values below the selection with this macro (negative values from the border of the inner ROI - segemented line is used to measure only the pixels below the segmented line):

Table.create("Distances");
Roi.getContainedPoints(x,y);
for (i=0; i<x.length; i++){
     //print(getPixel(x[i], y[i]));
     Table.set("Distance", i, -1*getPixel(x[i], y[i]));//converted here to positive values!
}
Table.update;

This should give you all distances from the inner to the outer membrane (please control with a manual selection).

Of course you could try to create the ROI’s from segmentation or classification processes, too but this is hopefully a quick solution.

Please note that this method measures all pixel distances! But of course every segmented line length works or you can select the i-th value.

1 Like

I have just tried your method and it worked just fine.
Thank you very much to both of you for your help!