How calculate the pixels distance


Hello everyone,

I would like to calculate the pixels distance between the red zones.

I was convert the type of my picture to binary with segmentation threshold and I obtained this result:

After that, I observe the level gray with plot profile (see a picture bellow):

Now my question, how can I calculate the distance of this region (in pixels and in cm)? Can I calculate that with other steps because is seems to me that it wasn’t the best methods?

Thank you for your response.



Hi and welcome to the forum

Seeing your image, and the low contrast between the two structures, I would not recommend a segmentation approach. You’ll notice that the black part of your mask image does not necessarily mark the edge of what you are measuring.

Another approach could be to detect peaks on the line profile you draw, and extract the two largest to finally computing the distance between these two.

I have attached an ImageJ macro that does this.

Just download the .ijm file and open it in Fiji, you should then be able to hit the Run button to start it. Draw a line on the image first to avoid the interactive prompt-

It seems to work rather well on your image, but I’d be curious of the result on the real dataset.

In short,

  1. Filter the image with a median filter to preserve edges and remove the white speckles.
  2. Get the values along the line and try to find exactly two peaks.
  3. Make a line where the two peaks were found and output the length.

If you want to see how it works, you should remove line 17 setBatchMode(true), then you can see the intermediate images that the macro uses.

Hope this helps!


Great, thank you for your help.
Can I convert the length in cm or in mm? because the real height of my sample (I mean for all of my picture) is equal to 3 cm.
And now it remains for me to convert the obtained result of the region of interest (red zone).
Is it possible to convert that?


Right I knew I forgot to add something.

If the real height of all your images is exactly 3 cm, do the following.

  1. Draw a line along the full height of your image
  2. Use Analyze > Set Scale…
  3. Known distance = 30
  4. Unit of length mm

Something like this:

Then when you run the script, the second column will be in calibrated units


Great! Thank you for your help.


Hi it’s me again,

I’m very great full about your code but I have some questions about your code? I don’t understand very well how you select the two peaks? Why you choose these two values 2 and 1000 for the minTolerance and maxTolerance? Can you explain more your method to calculate this distance?
Thank you for your reply.