fill a boundary region with white colour
Show older comments
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?
7 Comments
Guillaume
on 26 Nov 2019
@Elysi, I'd really like to know your logic for accepting the answer you've accepted, so I know whether or not to waste my time on any of your future questions.
We've narrowed down in Image Analyst's answer that what you actually wanted to do is remove the two largest blobs of your image. You were provided with two solutions that do exactly that but you go and accept the one solution that doesn't do that, so it really feels that we wasted our time helping you.
Elysi Cochin
on 27 Nov 2019
Edited: Elysi Cochin
on 27 Nov 2019
Guillaume
on 27 Nov 2019
That's fine then. But do comment on the answers you're given, so we know that our answers are useful or at least taken into account.
Elysi Cochin
on 27 Nov 2019
Image Analyst
on 27 Nov 2019
Elysi, now I / we think perhaps you want to extract only the second and third largest blob, considering their size after filling, which is a completely different thing than what were first answering about blob #2 and blob #3 using numbers as gotten from something like bwboundaries() and bwlabel(). With those, they give labels according to whatever order it finds a blob as scanning proceeds in column-major order, which is down the rows in one column (until it sees a white pixel), then move over to the next column and go down its rows, and so on until all columns have finally been scanned.
if you want to fill and keep the second and third largest blobs, you could simply have done:
bw = imfill(bw, 'holes'); % Fill FIRST!
props = regionprops(bw, 'Area'); % Measure areas of all blobs.
sortedAreas = sort([props.Area], 'descend'); % Get all areas into one vector sorted by decreasing area.
% Use bwareafilt to keep only blobs in the range from the second largest to third largest.
bw = bwareafilt(bw, [sortedAreas(3), sortedAreas(2)]);
No need for any boundary or labelling nonsense.
But reading and rereasing all your questions and comments I can't tell if you want to keep only the 2nd and 3rd largest blobs, or remove them from the rest of the image. The code above keeps them and discards all others. If instead you want to remove them and keep all other blobs as in the original mask (unfilled), then do this:
filledBlobs = imfill(bw, 'holes'); % Fill FIRST!
props = regionprops(filledBlobs, 'Area'); % Measure areas of all blobs.
sortedAreas = sort([props.Area], 'descend'); % Get all areas into one vector sorted by decreasing area.
% Use bwareafilt to keep only blobs in the range from the second largest to third largest.
blobs2and3 = bwareafilt(filledBlobs, [sortedAreas(3), sortedAreas(2)]);
% Now erase the 2nd and 3rd blobs from the original bw (unfilled) image.
bw(blobs2and3) = false;
Again, none of us would use boundaries. And it would be nice to finally know if you want to keep or eliminate the 2nd and 3rd largest blobs.
I also wonder why you're doing this. Is it something to do with artifact removal, like after thresholding you get a huge portion of the background? If it's something like that, then a better option would probably be to to a background correction (flattening) before doing a global threshold.
Elysi Cochin
on 29 Nov 2019
Edited: Elysi Cochin
on 29 Nov 2019
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?
Accepted Answer
More Answers (2)
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.
4 Comments
Image Analyst
on 24 Nov 2019
First of all, imfill() does NOT remove any areas. Secondly you should not do that for loop to get all the areas. Simply do
allAreas = [props.Area];
Explain why you're extracting the boundaries in the first place. From what I've seen so far, they are not needed at all. What are you really after anyway?
Image Analyst
on 24 Nov 2019
Edited: Image Analyst
on 24 Nov 2019
To extract and fill the 2nd and 3rd blobs, do this:
indexes = [sortIndexes(2), sortIndexes(3)];
blobs2and3 = ismember(labeledImage, indexes); % This is a binary image of only blobs 2 and 3.
% Now get binary image without them. Erase them from the original binary image.
bw = imfill(blobs2and3, 'holes');
Now you can relabel, call regionprops or whatever you want to do.
Guillaume
on 24 Nov 2019
As I explained already, tracing the boundaries of a region just to fill it is a complete waste of time. You've already got all the pixels of each region from bwlabel. You also got them again from regionprops, so using the slow bwboundaries to get something less useful is pointless.
In any case, if you want to fill the two largest blobs, bwareafilt is probably the best function. See edit to my answer.
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.
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!