How to remove narrow cavities and passages in a binary image

12 views (last 30 days)
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
Walter Roberson
Walter Roberson on 25 Dec 2021
Edited: Walter Roberson on 25 Dec 2021
See imerode(), imdilate(), bwmorph(), and bwareafilt()

Sign in to comment.

Answers (2)

DGM on 25 Dec 2021
Edited: DGM on 25 Dec 2021
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));
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;
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
Mahmoud Sedahmed
Mahmoud Sedahmed on 25 Dec 2021
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.

Image Analyst
Image Analyst on 25 Dec 2021
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.
Image Analyst
Image Analyst on 25 Dec 2021
Try a larger kernel, like 7x7x7
mask = imclose(mask, true(7));

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!