MATLAB: How to find distance in binary image

binary imagedistance between objectsimage processingImage Processing Toolbox

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

Best Answer

So, maria, if I understood you correctly, you wish to estimate the distance between two long nearly linear segments, i.e., you do not wish to know the minimum distance between the branched objects but rather the minimum distance between the roughly straight segments of these lines.
What David Legland suggested to you consists of following:
  1. Make a label image (where each individual object is marked by an object-specific pixel value). Let us call this label image Limg.
  2. Calculate image distance transform for each object i that is specified by pixel value i in the label image. First make test image Timg=zeros(size(Limg)), set Timg(Limg==i)=1. Now you have B&W image where only the given object "i" is present. Now you can calculate the distance transform Dist=bwdist(Timg).
  3. Now, to calculate the distance of another object, let us call this "j", from the object "i", we need to check what the distances in the pixels were. For each j that is not equal to i (i.e. j~=i) pick the pixel values from the distance transform, DistanceValues=Dist(Limg==j). Then you have the shortest distance of each pixel of object j to object i. If you take min(DistanceValues) you will get the minimum distance between objects i and j. If you take median(DistanceValues) and if the line-like objects i and j are mostly parallel and have low density of branches, then, because then the distances between the linear segments are expected to be more frequent than the distances between branches, you can get a better estimate of the distance between linear, parallel segments. -- Nevertheless, this will still relatively heavily suffer from the branches in object i, so finding the branch points, cutting at those points, removing the smaller of the objects created, and returning the branch point, and repeating iteratively until you no longer have branch points, and then repeating the calculation above will give you linear segment distances. But in this case, if you only have a single image, it is probably easier to measure the distances manually. You see, the problem is that it is difficult to make your programme detect where the linear segments are. You could try to look into Hough transforms for this purpose (see "Detecting Lines Using the Hough Transform" on page http://www.mathworks.se/help/images/analyzing-images.html#f11-27827).