MATLAB: How to extract boundary coordinates of multiple objects in an image

image analysisimage processingimage segmentationMATLAB

Hi, I am working on a path planning project which requires me to find the boundary (in the form of coordinates) of three black regions in the picture given. When I use convhull(after finding the coordinates of the black portion), it gives me the entire region enclosed within the boundary. Is there a way to get the boundary coordinates of the black portion (separately). Thank you! P.S- I don't require the red dot.
Matlab code is appreciated.

Best Answer

Yes. Use bwboundaries().
% Extract red channel and threshold it.
binaryImage = rgbImage(:,:, 1) < 128;
% Extract the 3 largest blobs ONLY.
binaryImage = bwareafilt(binaryImage, 3);
% Find the boundaries. Each boundary is in a cell.
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
% Display each boundary over the image in red.
hold on;
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Pull N by 2 array from k'th cell.
% Get x and y from the N-by-2 array.
x = thisBoundary(:,2);
y = thisBoundary(:,1);
plot(x, y, 'r', 'LineWidth', 2); % Plot outline over blob.
end
hold off;