How to detect and fill a special region?

Hi.
Regarding the following image:
what I want is detecting and then removing (I mean making them black as elsewhere) the white regions. Please note that the image is binary one and it is only a part of an image. The whole image contains other white and black regions (so it might not be easy to use imfill).
what can I do?
1. Is there any function in MATLAB that can automatically detect them and make them black?
2. If not, the user shall select them from screen, right? if yes, how to make the inside black? (I know I can select them by roipoly or similar functions, but I do not know how to make inside the regions black while keeping outside the same)?
By the way! If needed, I can also work on grayscale image instead of binary, whichever is easier or more efficient.
I hope I described my problem clearly.
Thanks so much for your time.
Steven

 Accepted Answer

Two ways, you can either have the user lassoo the object and erase inside of where they drew, as shown in my demos attached below. Or, you can call ginput and have the user click on a blob and then, after you've called regionprops, figure out which blob they clicked on by looking at PixelIdxList and then using ismember() to erase the containing, or closest, blob.

10 Comments

Steven's "Answer" moved here to be a comment:
Thanks so much!
Could you please help me out with the coding? I am a bit confused in it.
Thanks again.
I thought I did. At least for the masking method. Did you try the demo? Do you want to do it like that? Or did you want the user to click on a single point instead of lassooing an area? Please clarify which method you want help with.
Thanks again for your comment.
Actually I wan thinking of the second method. How can apply those commands as you said? They seem complicated to me.
Also I wonder whether the user shall determine a point or a region? I think a region. right?
Thanks so much.
Steven
Please clarify what the "Second method" is. It's ambiguous.
Steven
Steven on 14 Dec 2013
Edited: Steven on 14 Dec 2013
I mean your suggested method of user identifying regions through screen by ginput. Could you please let me know hoe to code them and specially PixelIdxList?
And about the demo, I will try it tomorrow at my office. I am now actually at home and I do not have MATLAB here.
Thanks so much.
See demo attached below in blue text.
Note: This demo selects the blob that has a centroid closest to where they clicked. It is possible to click inside a blob that has its centroid far far away, farther than a closer blob. Like a circle with a rod near it. It's possible that if you clicked near the end of the rod that the circle centroid might be closer and then it would pick that. If you really want to make sure that they click inside a blob, then you'll have to use PixelIdxList instead of checking on centroid distances.
Thanks so much indeed.
Hi. Thanks again for your codes. In the first code, freehand_masking_demo.m, how can the user do the selection and removal several times? I mean until the user says that it is enough?
Thanks. Steven
Put it in a while loop. At the bottom of the loop, before the end statement, have this:
message = sprintf('Do you want to continue');
button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'No')
break;
end
Steven
Steven on 16 Dec 2013
Edited: Steven on 16 Dec 2013
Thanks!
But it still shows the original image for choosing the new regions.
I mean I want every time, the image that has already been edited be shown to the user for selection, so that he know what regions are left for choosing.
Thanks.

Sign in to comment.

More Answers (0)

Asked:

on 14 Dec 2013

Edited:

on 16 Dec 2013

Community Treasure Hunt

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

Start Hunting!