MATLAB: How to find distance between boundary pixels and centroid

boundary pixelsdistance

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

Best Answer

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.