Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
Julia
Julia el 30 de Mzo. de 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.

Respuestas (3)

Steve Eddins
Steve Eddins el 30 de Mzo. de 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 comentario
Julia
Julia el 31 de Mzo. de 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.

Iniciar sesión para comentar.


Anathea Pepperl
Anathea Pepperl el 30 de Mzo. de 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 comentario
Julia
Julia el 31 de Mzo. de 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.

Iniciar sesión para comentar.


Steve Eddins
Steve Eddins el 31 de Mzo. de 2011
I posted an answer (using bwhitmiss) in response to your new question.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by