How to find distance in binary image?
Show older comments
i have a binary image and i want to measure the distance between the lines. With bwdist i get a matrix that calculates each pixel's distance but i consider the lines as objects, so i want the distance between them. someone that can help? image link: http://tinypic.com/view.php?pic=axhgqs&s=5#.UmZJLXDKHF8

Accepted Answer
More Answers (3)
Below is what I came up with, which is an average distance between objects for an entire image. An example of the distances being calculated is the image in red, between the given lines in white. If this isn't what you had in mind, then you'll need to explain a bit more the measurement you want to make.

You may want to clean up the lines a bit, as small, isolated pixels can have a dramatic effect on the measurement.
imgin = imread('axhgqs.jpg'); % read in image from tinypic
imgin_bw = im2bw(imgin); %convert to binary image
dist_map = bwdist(imgin_bw);
%find points furthest from lines
lines = watershed(dist_map);
lines = ~(lines > 0);
%keep only the intensities that correspond to furthest points between lines
avg_distance_map = dist_map ;
avg_distance_map(~lines) = 0;
%calculate stats
avg_distance_intensity = sum(sum(avg_distance_map)) ; %add up all the kept intensities
avg_distance_area = sum(sum(logical(avg_distance_map))) ; %number of pixels that contribute to intensity
average_distance = avg_distance_intensity / avg_distance_area ;
2 Comments
maria
on 23 Oct 2013
Jeff E
on 23 Oct 2013
The watershed function (in combination with bwdist) finds the points that are furthest from any white pixel.
If you want discrete measurements for each line to its adjacent neighbors, then yes, you would need something like bwlabel to consider each one of them independently. But that is a much more difficult proposition.
David Legland
on 22 Oct 2013
0 votes
Hi maria, If you consider a label image (one label for each line), you can call "bwdist" function for each label "i". Then, for each label "j", witj j~=i, you can consider the minimum value of the distance map in the pixels corersponding to label j. This will give you the (minimum) distance between lines i and j.
Image Analyst
on 29 Oct 2013
0 votes
All these discussions hinge on doing exactly what maria says. But I would like to take a larger view and ask what if what she said she wants us to do for her is not the best approach? What if those lines are not accurate? How did she get the lines? Maybe it's not appropriate to use an edge detector. After all, we have not seen the original image that the lines were derived from, and not seen the lines overlaid on the original image. Can I trust the lines? I don't know. Perhaps she wants an average spatial frequency of some ridged or rippled pattern and it's better to take a Fourier approach. Perhaps that, or other methods, will work as well or even better. I have no idea because I can't see the original image and I don't know the larger context. Often a poster say "help me do X" but they don't say it's because they need Y. So people tell them how to do X. But if you knew they needed Y , you could say "X is not the best or most appropriate way to get Y, you should really use Z instead ."
Categories
Find more on Region and Image Properties in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!