# fill a boundary region with white colour

21 views (last 30 days)
Elysi Cochin on 24 Nov 2019
Commented: Image Analyst on 29 Nov 2019
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?
Image Analyst on 29 Nov 2019
How do you decide which to keep in the various images? What are you looking at? What criteria is it that you check to determine which blobs to keep and which to discard?

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.
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 Guillaume on 24 Nov 2019
"And Guillaume, it is indeed a kind of reverse work, but it could be that the other contours are needed for something else, and if this is indeed the case in your approach one would need use bwlabel, get the needed regions, then either use bwboundaries again or implement the additional routines in the other select regions, also lots of work. If the other contours aren't needed than it would be more efficient"
If the boundaries are needed for something else, then grab the 2nd output of bwboundaries then. It's the same labeled image returned by bwlabel. Tracing the boundaries and the reverse operation of flood filling a boundary are both complex operations, so doing one just to reverse it is a bit of a waste:
[boundaries, labelimage] = bwboundaries(bw); %labelimage is the output of bwlabel(bw) that bwboundaries call.

### More Answers (2)

Guillaume on 24 Nov 2019
Edited: Guillaume on 24 Nov 2019
boundaries{2} is only two pixels, there's nothing to fill!
Anyway, if all you want is to fill the region, then bwboundaries is a waste of time. You only need the first step of the bwboundaries algorithm, which is the region labels obtained with bwlabel.
labels = bwlabel(bw);
white = uint8(255); %or 1 if your image is double
bw(ismember(labels, [2 3]) = white; %fill pixels whose labels are 2 or 3 with white.
edit: so it turns out you want to fill the two largest objects of your image. Why didn't you ask help on that to start with?
The easiest way to fill the two largest blobs of image bw:
tofill = bwareafilt(bw, 2, 'largest');
bw(tofill) = 0;
done!

Image Analyst on 24 Nov 2019
Simply do this:
bw = imfill(bw, 'holes');
since boundaries came from BW, and the first two boundaries are not really closed boundaries, the code above will fill the third boundary in bw.
Image Analyst on 24 Nov 2019
Right. No need to call bwboundaries(), and better to extract what is wanted in advance with bwareafilt() than to get properties of them with regionprops(), and then figure out from the areas which ones to keep.