MATLAB: Converting boundary to mask

boundarycontourimage processingimage selectionmask

lets assume that this is the code i used to identify the boundary. I want display the selected boundary in new window else convert this to mask. is it possible.. some one please help me
I = imread('F:\project_matlab\dataset\002.jpg');
BW = im2bw(I, graythresh(I));
[B,L] = bwboundaries(BW,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end

Best Answer

b=bwboundaries(BW); % BW being your binary image
mask=false(size(BW));
for i = 1:length(b)
for j = 1:length(b{i})
ind = b{i}(j,:);
mask(ind(1),ind(2))=1;
end
end
figure
imshow(mask)