MATLAB: How to trace the boundary of each object in a binary image

bwboundariesbwtraceboundary

I have a binary image like the following image. I want to plot the border of each object with different color. As far as I know, I need to use bwtraceboundary. But, I don't know how to choose the starting point that has to be parsed in the second input argument of bwtraceboundary. Any help is appreciated. Thanks.

Best Answer

If your shapes are all solid blobs with no holes or narrow necks, then there are simpler ways than bwtraceboundary:
B=A-imerode(A,ones(3))>0;
reg=regionprops(B,'PixelList');
colors={'g','r','m','y','c'};
imagesc(A); colormap(gray(256));
hold on
for i=1:numel(reg)
plot(reg(i).PixelList(:,1), reg(i).PixelList(:,2),['.' colors{i}]);
end
hold off