How to fill BLACK a region in an image?

I have an image (no matter gray or binary). I want to fill several regions (that are selected by the user through interface) in BLACK!
imfill and roifill cannot do this, because they fill in white!
(I think roifill is exactly what I need, but it fills them white not black!)
Does anybody know how I can resolve my problem?

 Accepted Answer

It does matter if they're grayscale or binary. You will probably have to use morphology. For example you might need to erode or dilate your binary image so that you don't fill with white. Or you might be able to use logical indexing. Can you post your image so I can recommend a method?

More Answers (2)

Steven
Steven on 12 Dec 2013
Edited: Steven on 18 Dec 2013
Hi. Thanks for your reply. Actually by "no matter binary or gray", I meant I may do the filtering (or whatever is needed) in either format. I have attached my picture here. For example, I want to convert the white areas at the left (inside the curve) to black (while the white part at the right side of the whole picture shall be remain white). I hope I can make it clear!
Then, what shall I do? I was thinking of letting the user choose polygons inside the region (left) to make it black, but it is time consuming and we do not know how many times user has to do it. So is it possible for MATLAB to do it automatically?
Thanks so much. Steven

1 Comment

That looks like a binary image. So why is imfill() not working?
filledImage = imfill(binaryImage, 'holes');

Sign in to comment.

Why not just assign 0 to the roi regions? If they are defined as polygons, then poly2mask() to get the bitmap.

8 Comments

Thanks Walter for your reply. Do you mean first set roi on the screen and then how to get the data inseide it? Also what does poly2mask do with it? Thanks!
BW = poly2mask(x, y, m, n) computes a binary region of interest (ROI) mask, BW, from an ROI polygon, represented by the vectors x and y. The size of BW is m-by-n. poly2mask sets pixels in BW that are inside the polygon (X,Y) to 1 and sets pixels outside the polygon to 0.
You can use impoly to interactively create the polygon, if desired.
You had implied that you already had the ROIs, but you did not mention what form they were in, so I gave some information for the polygon case, under the assumption that if they were already in bitmask form, you would have already just set the area to 0.
Thanks again so much! Actually the problem here is that I want to make some parts (for every image out of too many!) black. Since, there are too many images and each one has different features, so this came to mind that maybe inserting polygons on screen can be useful. But do you think there are other efficient ways to do so? especially if I can do them in a loop (automatically)? Thanks so much for your time. Steven
I'm really puzzled why my suggestion of using imfill() is not working for you. Why are you heading down this more complicated path?
Steven
Steven on 14 Dec 2013
Edited: Steven on 14 Dec 2013
Thanks. Doesn't imfill fill them WHITE? Sorry I could not understand what you mean. Could you kindly explain more?
Forgive me, since I am so new in MATLAB. Thanks.
I thought you wanted to "fill several regions (that are selected by the user through interface) in BLACK!" In other words, take the black areas ( or possibly just some of them that are selected by the user) and fill them in - with the surrounding color, which is white. If that's not what you meant, then please explain in a lot more detail. Because the only thing I see that can be "filled in" are the black holes in the big white blob on the left.
Steven
Steven on 14 Dec 2013
Edited: Steven on 14 Dec 2013
Hi. Thanks again.
Sure. Actually what I want is exactly the opposite! I want to make "these white holes in the black blob on the left" BLACK! Let me put it this way:
I have an image in which there are some places (some interferences) that are wrongly white (because of photographing errors) and I want to make them black. It is something like the image attached here (it is a part of the original image).
1.How may I do so? Can MATLAB automatically detect them and black them?
2.Also what about the small white point at the right? how can I detect it and black it?
Thanks so much for your time. Steven
Like this???
% Find the binary image
binaryImage = grayImage > thresholdValue; % say 200 or something.
% Make those pixels black.
grayImage(binaryImage) = 0;

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 12 Dec 2013

Edited:

on 26 Apr 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!