Finding the distance between objects
3 visualizzazioni (ultimi 30 giorni)
I have the above image which I have already labeled using bwlabel(). Now what I want to do is something similar to what was posted in: http://www.mathworks.com/matlabcentral/answers/91046-how-to-find-distance-in-binary-image
For this case I want to calculate the smallest distance between object 1&2, 1&3 and 2&3. But in general I want to make it flexible so I could input any image with any amounts of objects and be able to calculate the distances.
This is my code so far:
Timg = zeros(size(Limg));
Timg(Limg == i)=1 ;
Dist = bwdist(Timg, 'euclidean');
DistanceValues = Dist(lableledImage == j));
It doesn't work as I expected it to. Any help in editing this code or completely changing the concept would be appreciated.
Image Analyst il 5 Ago 2014
That code is no good.
First you need to get a binary image with the center filled in. I suggest you run around the border of the image and if there are any white pixels closer than 10 pixels or so, connect them. Then call imfill(binaryImage, 'holes') to get three solid branches.
Then call bwdist to get the distance transform on the binary image.
Next call bwmorph(binaryImage, 'skel', inf) to get the skeleton. The skeleton runs all the way out to the border and you don't want that since you'll get distance values of zero so you want to clip the skeleton by a few pixels with the 'spur' option of bwmorph. Think about it and you'll realize why. If not, plot the skeleton over the binary image and then you'll see why.
The distance transform has distances from every point to the border, which is not what we want. We want only those distances along the centerline, the skeleton. So multiply the skeleton by the distance transform to get only the radii. Double it to get the diameters.
Then extract all non-zero pixels and find the min. Then you're done.
An alternative way - kind of a brute force approach - is just to label the image and use the Pythaogrean theorem to find the distance from every pixel of region m to region n. Call find() on each to get a list of coordinates, then use sqrt() to get distances. Use a single for loop. Should be fast, probably faster than the first method, since there are only a few hundred or thousand elements.
Code up one of those methods, and run it by me if you want.
Alaa il 4 Mar 2015