How to find distance between boundary pixels and centroid?
4 views (last 30 days)
Show older comments
Ayesha ch
on 10 May 2016
Commented: Walter Roberson
on 11 May 2016
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163891/image.png)
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
0 Comments
Accepted Answer
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
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.
More Answers (1)
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..
0 Comments
See Also
Categories
Find more on Feature Detection and Extraction 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!