Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
Julia
Julia il 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.

Risposte (3)

Steve Eddins
Steve Eddins il 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 Commento
Julia
Julia il 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.

Accedi per commentare.


Anathea Pepperl
Anathea Pepperl il 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 Commento
Julia
Julia il 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.

Accedi per commentare.


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

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by