# Which method to remove small unwanted region and fill holes

I have a hard problem that need your help. I have a binary image that maintains some unwanted region (small white dot) and hole regions (in figure 1).My idea is that the first I will remove unwanted region by calculating area these region and then filter with small area value.At the second step, I fill in hole region to make clear image.What do you think best method to fill in hole region. D.Because my idea need threshold value to remove unwanted region. I want to find automatic method to do it. Do you have any idea to resolve it? Thank you so much. This is my reference code for remove unwanted region. But it need threshold term. You can download image test at here

function exImage=rmUnwantedRegion(Img,threshold)
lb = bwlabel(Img);
st = regionprops(lb, 'Area', 'PixelIdxList' );
toRemove = [st.Area] <threshold; % fix your threshold here
exImage = Img;
exImage( vertcat(st(toRemove).PixelIdxList ) ) = 0; % remove
end


This code work fine for me. You try

RGB = imread('Image/input.png');

GRAY = rgb2gray(RGB);
threshold = graythresh(GRAY);
originalImage = im2bw(GRAY, threshold);

originalImage = bwareaopen(originalImage,250);
se = strel('disk', 10); %# structuring element
closeBW = imclose(originalImage,se);

imshow(closeBW);


• You can increase or decrease the value in se = strel('disk', 10); to get more appropriate result. – Asif Sharif Apr 29 '14 at 14:54
• Thank you sir, my error is that my upload image (in above link) is RGB image. But my original image (create by my code is binary image. So I cannot apply "RGB = imread('Image/input.png'); GRAY = rgb2gray(RGB); threshold = graythresh(GRAY);". So the my result is bad. Please check agian with original image and re-implement help me?dropbox.com/s/mvodekty93scckm/input.mat – John Apr 29 '14 at 16:17
• Then you have to remove these three line and just use below code only.. originalImage = imread('Image/input.png'); originalImage = bwareaopen(originalImage,250); se = strel('disk', 10); %# structuring element closeBW = imclose(originalImage,se); imshow(closeBW); – Asif Sharif Jul 2 '14 at 5:31
• Do you know a Python solution? – Sigur Dec 17 '18 at 11:55