ImageJ macro to measure distance between two lines/edges

Here is an ImageJ macro to measure distance between two user-drawn segmented lines (polylines) on an image. Shortest distances from points along the shorter line to the longer line are used to calculate the average distance between the two lines (their spline interpolations).

The macro works in ImageJ 2.1.0/1.53c, and may be useful to someone.

The macro prompts user to draw the two segmented lines (example below).

The macro then makes the measurements and displays statistics on the image, and prompts the user to save the values in a user-selected file.

InteredgeDistance_v1.0.1_ImageJMacro.txt (12.1 KB)
Instructions for InteredgeDistance macro.pdf (789.1 KB)

1 Like

Awesome! Wondering if I could measure tight junction lengths within my samples? :thinking:

See also this posts using the Exact Signed Distance Euclidean plug-in or similar:

Quantifying color distribution in birds (@oburri: still a nice example and in my list of links!)

1 Like

The newer version of the macro (1.1) should work when the lines are convoluted, as in your example.


Updated to version 1.1:
InteredgeDistance_v1.1_ImageJMacro.txt (16.3 KB)
Instructions for InteredgeDistance macro 1.1.pdf (786.9 KB)


Nice sharing.
Thank you

Thanks for sharing the macro. I started using it for scratch assay analysis. Is there a reference for the macro somewhere, in case we get to publish the analysis?

As of now, there is no publication to cite as a reference for the macro. The macro logic is too simple to write about. Feel free to provide the macro code as supplementary material to accompany your publication.

Let me know if you find any issue with the macro.

Ok thanks! Let me know if you want to have your name in the acknowledgments (assuming we get to the manuscript-writing stage…)

I’m okay with being named in acknowledgements. Also okay if you forget :slight_smile:

1 Like

Hi Santosh. I tried your Macro to measure the distance between two semi-parallel lines. However, the result was different from the real distance (less) and I think it’s because of the calculation method you are using (spline interpolation). In my case, I need to measure the thickness of a coating on a surface which ends up measuring the distance between two lines. Could you please let me know if I can modify this code to use in my problem?
I look forward to hearing from you.

Hi Shiva, can you point to an example image or two? Yes, the macro I wrote requires poly/multisegment lines… their spline interpolations are used to identify a set of coordinates along a line to measure distance from/against.

Hi Santosh,
thanks for your reply.
I am wondering if your canny.tif (768.2 KB) macro can be modified to measure the perpendicular distance between two lines? I am trying to measure the thickness of a coating on a surface. I have used canny edge detection to find the edges (sending an image for your reference) and now I want to measure the average distance between the two lines.

I greatly appreciate your help in this and Icanny.tif (768.2 KB) look forward to hearing from you.

Hi Shiva, I used the macro (v1.1) in Fiji/ImageJ 1.53c/2.1.0 and it seems to be working and measuring the distance accurately. No?

Hi Santosh,
Thanks for the early reply. actually, I am not sure if it is accurate. is there any way to get the measurement in micrometer instead of the pixel?

I really appreciate your advice.

looking forward to hearing from you.

Hi Shiva, all image manipulation or measurement occurs at the level of pixel. To convert the macro’s output in pixels to units of length like mm you will need image resolution (pixel per mm, etc.). The macro doesn’t know and cannot identify image resolution.

You can retrieve the current image scale by using something like

length = 1;

Thank you for the suggested command to get the image scale. Unfortunately, it will fail for images that don’t have the correct metadata.

Hi Santosh,
Thanks again for your reply.
I have another question for you. As my measurement is to be very accurate, I am using an edge detector before using your provided Macro. The problem happens when I am drawing segmented lines on the detected edges. It is very time-consuming to select the accurate points one-by-one to draw the line and I have lots of images to analyze this way. Is there any way to modify this Macro somehow to draw the lines itself based on the edges I have already detected? I am sending the edge-detected image as a reference. papre 20x.tif (4.8 MB)

Thanks for your help.

The macro cannot auto-detect edges/lines … such ability is in a completely different domain (and I have no expertise for it).

Looking at the example image, it seems that the “precision” of the best measurement that can theoretically be achieved will still have at least an 1%-2% error, similar to what may be obtained with the macro by drawing polylines with may be 5-6 segments.