MATLAB: Plotting object boundaries over an image in the same figure

boundariesImage Processing Toolboxplotting

Hi, I'd like to plot the object boundaries of an image shown in a figure. If I donot use subplot, when I plot the boundary objects, the figure windows gets white and the result of plotting does not appear. The same if I use the same subplot, e.g. subplot(2,1,1) for both imshow and plot. Just in case I specify different subplot, e.g. subplot(2,1,1) before imshow, and subplot(2,1,2) before plot, then both graphics are displayed in the same window. But, the coordinates of the boundaries donot fit, and besides, the window shows both results on differents sub-windows.
That is, the effect I'm trying is to highlight the objects boundaries over an image.
Below, you have my code. If you see anything wrong … thanks a lot:
figure;
clf;
I = imread("saturno.jpeg");
imshow(I)
BW = im2bw(I, graythresh(I));
[B,L] = bwboundaries(BW);
subplot(2,1,1);
hold on
imshow(label2rgb(L, @jet, [.5 .5 .5]))
for k=1:length(B)
boundary = B{k};
subplot(2,1,2);
hold on
plot(boundary(:,2), boundary(:,1))
end

Best Answer

You mean like this:
grayImage = imread('moon.tif');
imshow(grayImage)
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Threshold to binarize the image.
BW = im2bw(grayImage, graythresh(grayImage));
% Find boundaries.
[B,L] = bwboundaries(BW);
hold on
% Plot boundaries over original gray scale image.
for k=1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1),...
'r-', 'LineWidth', 3)
end
title('Moon with boundaries overlaid', 'FontSize', 25);
Related Question