automatic calculation of length of object in a binary image

2 views (last 30 days)
I have the following binary image:
I manually select the start and end points using the ruler in imtool to get the length. Is there a way to automatically get the length i.e the first white pixel to last white pixel (longest length) without doing it manually.

Accepted Answer

Image Analyst
Image Analyst on 27 Apr 2014
The most robust way would be to call bwboundaries and run through the coordinates finding which two are farthest from each other. Using MajorAxisLength in regionprops() won't be as accurate as that since it fits the blob to an ellipse.
boundaries = bwboundaries(binaryImage);
x = boundaries(:, 1);
y = boundaries(:, 2);
maxDistance = -inf;
for index1 = 1 : length(x)
for index2 = 1 : length(y)
deltaX = x(index1) - x(index2);
deltaY = y(index1) - y(index2);
distance = sqrt(deltaX^2+deltaY^2);
if distance > maxDistance
and so on. I trust you know how to finish it.
  3 Comments
Yan Zhao
Yan Zhao on 18 May 2016
thanks for your answer. I find a easy way accordingly:
D = pdist2(boundaries,boundaries);
maxDistance = max(max(D));
Image Analyst
Image Analyst on 18 May 2016
Yes, if you have the Statistics and Machine Learning Toolbox, you can use that function.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!