MATLAB: How to calculate only one centroid of an image

image centroid calculation

I have a segmented binary image. I need to calculate only one centroid for the entire image ( considering only the white area), get the location and plot the centroid. I am struggling as the image is discontiguous and ending up with many centroids. I have got one centroid but sometimes its locate in black area. So bit confused whether it is right or not. I have attached the image. Assuming the centre should be in a white area of the image.
Any help will be much appreciated!

Best Answer

Well, the definition of the centroid is simply the mean of the coordinate of your points, so:
[rows, cols] =ndgrid(1:size(yourimage, 1), 1:size(yourimage, 2));
centroidrowcol = mean([rows(:) .* yourimage(:), cols(:) .* yourimage(:)])
Although, I'm not sure there is much value in calculating the centroid of the image you're showing...