Clear Filters
Clear Filters

How to find distance between boundary pixels and centroid?

4 views (last 30 days)
Hi I'm new in matlab and I want to find the distance between the boundary pixels of each connected component and the centroid. I'm using the following algorithm.I have found centroid but dont know how to find the boundary pixels and distance. Looking forward to your reply.
for i=1:count(boundary pixels)
distance between centroid and boundary pixels
end
sum=sum/count

Accepted Answer

Walter Roberson
Walter Roberson on 10 May 2016
Once you have a logical mask of ROIs corresponding to each cell, and you have filled the regions, then http://www.mathworks.com/help/images/ref/bwdistgeodesic.html
stats = regionprops(TheBinaryROI, 'Centroid', );
cents = vertcat(stats.Centroid);
mask = logical(size(TheBinaryROI));
cents_ind = sub2ind(size(mask), round(cents(:,2)), round(cents(:,1)));
mask(cents_ind) = true;
dists = bwdistgeodesic(TheBinaryROI, mask);
this will give the distances everywhere inside the ROIs. So you can proceed from there to http://www.mathworks.com/help/images/ref/bwboundaries.html use bwbounaries to trace the boundaries of the cells, and use those to select the information out of dists. Now that I think of it, you could instead use http://www.mathworks.com/help/images/ref/bwmorph.html bwmorph() with 'remove' to get just the outside edges of the region. multiply the result by the distance transform to get just the distances from the edges to respective centroids.
  2 Comments
Ayesha ch
Ayesha ch on 10 May 2016
Edited: Walter Roberson on 11 May 2016
Thank you for the help, I've tried this:
stats=regionprops(Iout,'Centroid');
cents=vertcat(stats.Centroid);
mask=logical(size(output));
cents_ind=sub2ind(size(mask),round(cents(:,2)),round(cents(:,1)));
dists=bwdistgeodesic(output,mask);
r=bwmorph(im2double(Iout),'remove');
figure, imshow(r);
for i=1:count(dists)
d=r*dists;
end
d=d/count(dists);
but it gives me an out of range subscript error in sub2ind. kindly help
Walter Roberson
Walter Roberson on 11 May 2016
stats=regionprops(Iout,'Centroid');
cents=vertcat(stats.Centroid);
mask = false(size(Iout));
cents_ind = sub2ind(size(mask), round(cents(:,2)), round(cents(:,1)));
mask(cents_ind) = true;
dists = bwdistgeodesic(Iout,mask);
However, I am finding that the centroids are not necessarily inside the regions. A lot depends on how you segmented into different regions: if you are able to get a clean break between regions then you probably will not do badly, but when I tried, the regions tended to run together.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 10 May 2016
You can use bwboundaries() to get a list of x,y coordinates of your perimeters. You can use bwdist() or bwdistgeodesic() to get the distance of every pixel in each blob to the closest perimeter point. Or you can use the Pythagorean theorem and the coordinates from bwboundaries if you want an "as the crow flies" distance, which in general is different than you'll get from either bwdist function..

Community Treasure Hunt

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

Start Hunting!