Algorithm used to find centroids in regionprops.

I used regionprops to find centroids in image processing. But now I have to put algorithm to show how code works to find these centroids. Someone please tell me the algorithm used for the same.

1 Comment

I am also looking for further information on the algorithms underlying the regionprops function's outputs. Did you manage to find further information from MATLAB? Thanks!

Sign in to comment.

 Accepted Answer

It's a good question but I don't know that we have an answer. I know the area from regionprops is simply the pixel count, because it says so, unlike bwarea which gives the area differently depending on how many pixels are on the outer edge of the blob. Now the unweighted centroid formula is normally just the average of the x and y coordinates of all the pixels in the blob, but whether they weight pixels differently depending on whether they are near the edge (background) is not stated.
Another flavor of centroid, the weighted centroid is just sum(g*x)/sum(g) - where g is the gray level. Note how the g "cancels out" so you're left with something with units of x. The weighted centroid may be further weighted by whether a pixel is near the edge. That said, I don't think they weight pixels differently depending on their adjacency to an edge like they do for bwarea(), though I'm not 100% sure.

5 Comments

Some quick tests show that for Centroid regionprops does not weight according to location in the image or whether the pixel is on the edge. It just does a straight forward average of the coordinates.
I develop a code to find centroid and based on that I tries to develop algorithm. code is given below:
boll1 = imread('C:\Users\Naseeb\Desktop\ff.png');% load image
boll = im2bw(boll1); % convert into binary
st = regionprops( boll, 'PixelList'); % use regionprops to get pixels coordinates
zzz = size(st); % to get size of st structure which depends on no. of blobs in image
xy1 = st(1).PixelList ;% to put first value of blob pixels into xy1
sumx = sum(xy1(:,1)); % sum of first column of xy1
sumy = sum(xy1(:,2)); % sum of second column of xy1
sizx = size(xy1(:,1));
sizy = size(xy1(:,2));
sizex = sizx(:,1); % to get the size of first column
centx = (sumx / sizex); % calculating average of first column
centy = (sumy / sizex); % calculating average of second column
% centx and centy are the centroid of first blob
By applying loop, we can calculate centroids for every blob in image. Algorithm is attached with this in image format.
What changes should I do in this algorithm to look better.
Use mean(), like I showed. That will eliminate most of your code.
This is not true:
% centx and centy are the centroid of first column
It should say
% centx and centy are the centroid of the first blob.
And it looks like sizx is simply the number of pixels in the first blob, not of a column in the blob. And so, since sizx is just a scalar, sizex is just 1. The whole thing is just a mess and you should just use mean() like Walter suggested.
Hello Image Analyst,
Thanks for your comment.
You are right that in last line of code, there should be 'blob' and not 'column'. That mistake I corrected, but as you said sizex is just 1, that is not correct. sizex gives me the scalar quantity which is equal to number of rows in st(1).PixelList (which is greater than 1 in my case). sizx give me a 1x2 array but I want the value of first row and first column which I used to calculate average using sumx and sumy. yes, I can use mean as suggested but I want to see it step by step, that's why I go like this.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!