MATLAB: How to find boundaries of scattered pixels in an image

boundariescontoursImage Processing Toolbox

I have this image and I want to set boundaries over the scattered pattern/pixels. I tried bwboundaries but it only gives the contours for each set of "connected pixels".

Best Answer

One way to get the "boundary" is to call bwconvhull():
chImage = bwconvhull(binaryImage);
To find the centroid of the blob(s), you can use regionprops():
props = regionprops(chImage, 'Centroid');
% Plot centroids.
hold on;
for k = 1 : length(props)
plot(props(k).Centroid(1), props(k).Centroid(2), 'r+', 'MarkerSize', 25);
end
To find the centroid of the points (rather than the solid convex hull blob regions), you can use find
[y, x] = find(binaryImage); % Not [x,y] as in the other answer.
xCentroid = mean(x); % Column.
yCentroid = mean(y); % Row.