MATLAB: How do you find a centroid of an image

image analysisimage processingImage Processing Toolboximage segmentation

I am trying to find the centroid of a binary image using region props but I keep receiving the error "Field reference for multiple structure elements that is followed by more reference blocks is an error". Here is my code for finding the centroid:
measurements = regionprops(bw, 'Centroid');
hold on;
plot(measurements.Centroid(1), measurements.Centroid(2), 'r+', 'MarkerSize', 120, 'LineWidth', 1);
hold off;
imshow(bw)
Any help would be appreciated thank you

Best Answer

Maybe try
centroids = [measurements.Centroid];
xCentroids = centroids(1:2:end);
yCentroids = centroids(2:2:end);
plot(xCentroids, yCentroids, .............