How to fill the unclosed shape?

5 views (last 30 days)
I am now trying to create a mask to use in a machine learning annotation file.
In doing so, I would like to binarize the droplet image with a threshold value and then fill the inside of the droplet with imfill.
However, sometimes the boundary becomes an open curve due to some thinness in the boundary area.
In this case, what can I do to fill this area automatically?
Here is the code.
This is the first time to ask a question on matlab and I am not a fluent in English, so I apologize if there is any rudeness.
I_pp=double(rgb2gray(imread("img0.jpg")));
%I_bg=double(rgb2gray(imread("bg.jpg")));
diff=I_pp; %-I_bg;
min(min(diff))
result=((diff-min(min(diff)))/(max(max(diff))-min(min(diff))));
imshow(result)
level=graythresh(result);
BW=imbinarize(result,level);
BW(1:7, :)=1;
BW(end-7:end,:)=1;
BW= 1-BW;
BW2 = bwmorph(BW, 'bridge');
BW2=imclose(BW2,strel('disk',2));
imshow(BW2)
%%
I2 = imfill(BW2,'holes');
imshow(I2)

Accepted Answer

Image Analyst
Image Analyst on 12 Dec 2022
You can try imclose, but that's not your biggest problem. Your biggest problem is the non-uniformity of your illumination. It's most likely due to lens shading. You need to divide that out. First take a "blank" shot of nothing -- just the light background itself. Then divide your images with stuff in them by the background image. I attach a demo.
  3 Comments
Kei Mukoyama
Kei Mukoyama on 13 Dec 2022
By the way, when I use imclose, droplets from different locations merge, is there any way to prevent this?
Is there any way to prevent this? Is the only way to prevent this is to specify the position of each droplet individually?
Image Analyst
Image Analyst on 13 Dec 2022
Instead of imclose, you can use bwmorph with the 'thicken' option. This won't join blobs. Then follow that with an imerode with the same structuring element.

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!