How to remove narrow cavities and passages in a binary image

I have a binary image representing a porous medium where fluid flows. The points marked with (1) in the array represent the solid points, where the points marked with (0) represent the fluid points. There are several regions in the image where the fluid nodes form a fluid passage that has a width of only one or two fluid points (narrow passage). I need to remove such narrow passages from the image and replace them with solid points. Alternatively, I could widen them as well to have at least 4 points (or a selected number of points) width to represent a wider passage. If removing the narrow passages produces a cavity in the solid region that is not connecteed to fluid network, I need to remove this cavity as well.
Attached is a binary image that shows what is mentioned above.
Thanks in advance.

1 Comment

See imerode(), imdilate(), bwmorph(), and bwareafilt()

Sign in to comment.

Answers (2)

Simple morphological operations should suffice. Morphological closing removes background (zero-valued) features smaller than a given strel.
S = load('binaryArray_2D.mat');
A = S.data_binary;
B = imclose(A,ones(2));
imshow(B)
This will remove all the passages <=2px wide.
If you want to open up narrow channels, you can go further. The bottom hat operation returns only the pixels closed by a closing operation.
S = load('binaryArray_2D.mat');
A = S.data_binary;
B = imdilate(imbothat(A,ones(2)),ones(2));
C = A & ~B;
imshow(C)
In summary:
dilation: make foreground bigger according to the applied strel
erosion: make background bigger according to the applied strel
opening: the dilation of an erosion
closing: the erosion of a dilation
tophat: the foreground content removed by opening
bothat: the background content removed by closing

4 Comments

Thank you so much for your answer, It got the required effects of removing and widening the narrow passages.
Would you please indicate how it could be modified to apply for 3D porous medium as well?
Attached is a 3D porous medium sample.
Thanks again.
Another note, the effect do not work near the boundaries (Top and Bottom sides), how could the same effects be achieved in these layers.
Thanks.
As IA mentions, the morphological tools should work in 3D as well, so long as you specify an appropriate strel.
I tried the same tools and changed the strel on the attached sample, but still there are narrow paths that were not widen nor removed from the geometry. It could be observed for example at slice #167 (z-direction) that there are one fluid point paths.

Sign in to comment.

You can use a morphological closing, imclose(). This dilates the 1 areas and can cause them to join, then erodes the area so that the dilated regions now have about the same size and shape as they had before except that it does not break/separate the regions that were merged by dilation.
mask = imclose(mask, true(3));
(I'm calling your "data_binary" "mask") because that's what I usually call it. Experiment with the number 3 until you achieve the effect you are looking for.
Now, as you said, this can cause some fluid regions to become completely enclosed with no flow possible through those regions. To remove those regions, as you requested, you can use imfill():
mask = imfill(mask, 'holes');
This will fill all holes (fluid regions) with 1 (solid regions). If you want to fill holes of only a certain size range, you can do
mask = ~bwareafilt(~mask, [minSize, maxSize]);
where size is the area in pixels.

5 Comments

Thank you so much for your answer, It got the required effects of removing narrow passages and removing the resulted isolated regions as well.
Would you please indicate how it could be modified to apply for 3D porous medium as well?
Attached is a 3D porous medium sample.
Thanks again.
imfill() works on 3-D binary images.
I think bwareafilt() does too. If not, you can use bwareaopen() which does work with 3-D images.
Thank you, I will try them,
How about the removing the narrow passages or widening them in 3D?
Another note, the effect does not work near the boundaries (Top and Bottom sides), there are still narrow passages that did not get closed, how could the same effect be achieved in these layers.
Thanks
Try a larger kernel, like 7x7x7
mask = imclose(mask, true(7));

Sign in to comment.

Categories

Find more on Fluid Dynamics in Help Center and File Exchange

Products

Release

R2019b

Asked:

on 25 Dec 2021

Commented:

on 25 Dec 2021

Community Treasure Hunt

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

Start Hunting!