How do I fill 4-connected cells in an matrix?

2 views (last 30 days)
Julia
Julia on 30 Mar 2011
I have a problem. I would like to convert a matrix like this:
A=[ 1 1 1 0 1 0; 1 0 1 1 0 1; 1 1 1 0 1 0; 0 0 0 0 0 0]
to
B=[1 1 1 0 1 0; 1 1 1 1 1 1 ; 1 1 1 0 1 0; 0 0 0 0 0 0]
that is fill 4-connected cells containing 0 with 1. I have tried to use imfill but I think it is not useful in this specific case.

Answers (3)

Steve Eddins
Steve Eddins on 30 Mar 2011
>> imfill(A,4,'holes')
ans =
1 1 1 0 1 0
1 1 1 1 1 1
1 1 1 0 1 0
0 0 0 0 0 0
  1 Comment
Julia
Julia on 31 Mar 2011
Hi,
thank you for the answers but this option does not give exactly what I need. The example matrix was probably not a good one. I show another example. If:
BW1 = logical([1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0
1 1 1 1 0 1 1 1
1 0 0 1 1 0 1 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0]);
with the command:
BW2 = imfill(BW1,4,'holes');
you get:
BW2 =
1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 1 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
1 0 0 1 1 1 1 0
1 0 0 0 1 1 1 0
1 0 0 0 1 1 1 0
but I would like to fill only the cells with 0 which are 4-connected with 1s, that is, I would like to get the following matrix instead:
BW3 =
1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0
1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0
Do you know if there is still an easy way to get this?
Again, thank you in advance.

Sign in to comment.


Anathea Pepperl
Anathea Pepperl on 30 Mar 2011
If you have the Image Processing Toolbox, then imfill should work in this case. I tried replicating your code here:
A = [ 1 1 1 0 1 0; 1 0 1 1 0 1; 1 1 1 0 1 0; 0 0 0 0 0 0]
B = [1 1 1 0 1 0; 1 1 1 1 1 1 ; 1 1 1 0 1 0; 0 0 0 0 0 0]
C = imfill(A, 'holes')
isequal(C, B)
And using imfill to generate the matrix C gave me the same result as B.
  1 Comment
Julia
Julia on 31 Mar 2011
Hi,
thank you for the answers but this option does not give exactly what I need. The example matrix was probably not a good one. I show another example. If:
BW1 = logical([1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0
1 1 1 1 0 1 1 1
1 0 0 1 1 0 1 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0]);
with the command:
BW2 = imfill(BW1,4,'holes');
you get:
BW2 =
1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 1 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
1 0 0 1 1 1 1 0
1 0 0 0 1 1 1 0
1 0 0 0 1 1 1 0
but I would like to fill only the cells with 0 which are 4-connected with 1s, that is, I would like to get the following matrix instead:
BW3 =
1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0
1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0
Do you know if there is still an easy way to get this?
Again, thank you in advance.

Sign in to comment.


Steve Eddins
Steve Eddins on 31 Mar 2011
I posted an answer (using bwhitmiss) in response to your new question.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!