Clear Filters
Clear Filters

why imfill() doesnot work properly and i cannot delete the small objects from images?

4 views (last 30 days)
This is my code...
if true
a=imread('s out not good.jpg');
figure(1),imshow(a);
im=rgb2gray(a);
figure(2),imshow(im);
im= imadjust(im);
%filtering for removing artifacts..
im=medfilt2(im);
figure(3),imshow(im);
%DullRazor technique for hair removal
se = strel('disk',5);
hairs = imbothat(im,se);
BW = hairs > 15;
BW2 = imdilate(BW,strel('disk',2));
replacedImage = roifill(im,BW2);
figure(4),imshow(replacedImage);
%thresholding ....
level=graythresh(replacedImage);
a=im2bw(replacedImage,level);
figure(5),imshow(a);
%hole fills...
%a=imfill(a,'holes');
%figure(4),imshow(a);
%counting objects...(1st approach)
cc = bwconncomp(a,4);
number = cc.NumObjects;
display(number);
%counting objects...(2nd approach)
s = regionprops(a, 'Area');
N_objects = numel(s);
display(N_objects);
%display the object area
for i=1:N_objects
display(s(i).Area);
end
%closing operation..
se = strel('disk',5);
closeBW = imclose(a,se);
figure(6), imshow(closeBW);
%counting objects after closing operation...
cc = bwconncomp(closeBW,4);
number = cc.NumObjects;
display(number);
%counting objects after closing operation...
s = regionprops(closeBW, 'Area');
N_objects = numel(s);
display(N_objects);
%display the object area after closing
for i=1:N_objects
display(s(i).Area);
end
%selecting the biggest region ..
I=bwareaopen(closeBW,s(1).Area);
figure(7),imshow(I);
s = regionprops(I, 'Area');
N_objects = numel(s);
display(N_objects);
end
I have some problems. 1)I am trying to use imfill() function to fill the white region inside the black regions.But i used imfill() function it doesnot work.it gives no output.Here in the figure 6,it contains white pixel inside the foreground.i want to fill it using imfill() function just after segmentation.i want it as like figure 7.
2)after segmentation ,i found some extra regions.i want to delete those.Thats why i count number of objects .But i find different answer for this two ways for some picture.for example for one way i found 20 for others 30.This is not a big problem.the main problem is after that i found that the first components has the largest area.thats why i am trying to used only thats parts.But it doesnot show good results for some pictures..for some picture,it remains same.i donot know why it doesnot delete the other parts for some images.In the images,it seems they are a different regions.But why does it consider it as one region?
3)How can i improve the segementation results?for some images segmentations results is not good..

Accepted Answer

Image Analyst
Image Analyst on 19 Jun 2017
imfill and all other binary operations depend on the foreground being white (true, 1). To go from fig6 to fig7, you need to invert it first.
closeBW = imfill(~closeBW, 'holes');
Invert it again after imfill(), if you need to
closeBW = ~closeBW;
but you should really, really get in the habit of having your foreground and objects of interest being white, not black, and your life will be so much easier from then on.
  6 Comments
M.S. Khan
M.S. Khan on 12 Mar 2021
image analyst, i am facing the same problem. i have shared my quesiton on community. please guide me. regards

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!