MATLAB: Fill a boundary region with white colour

binaryfill holeshole fillingImage Processing Toolbox

i have identified boundaries from a binary image using
boundaries = bwboundaries(bw);
The output of the above line of code is shown below
[108,69;108,69]
[691,69;691,69]
23x2 double
i wanted to select boundaries{2} and boundaries{3} and fill that region with white colour, how can i do it?

Best Answer

You can transform the contourns in a binary image, fill it and than translate it back to your original image. Here is an example adapted from the matlab bwboundaries documentation.
I = imread('rice.png');
BW = imbinarize(I);
[B,L] = bwboundaries(BW,'noholes');
figure
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
if k <length(B)/2 % Half with fill and half without so one can verify the difference between them
% Binary image with same size
Ibinary = I==0;
% Get contourn image and plot it in the binary image
ind = sub2ind(size(Ibinary),boundary(:,2),boundary(:,1));
Ibinary(ind) = 1;
% Close the binary image
Ibinary = imfill(Ibinary,'holes');
% Get combination of index and plot in the figure
Index = find(Ibinary==1);
[row,col] = ind2sub(size(Ibinary),Index);
plot(row,col , 'w', 'LineWidth', 2)
else % Matlab example with only the boundaries
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
end
untitled.jpg