Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Julia
Julia am 30 Mär. 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.

Antworten (3)

Steve Eddins
Steve Eddins am 30 Mär. 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 Kommentar
Julia
Julia am 31 Mär. 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.

Melden Sie sich an, um zu kommentieren.


Anathea Pepperl
Anathea Pepperl am 30 Mär. 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 Kommentar
Julia
Julia am 31 Mär. 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.

Melden Sie sich an, um zu kommentieren.


Steve Eddins
Steve Eddins am 31 Mär. 2011
I posted an answer (using bwhitmiss) in response to your new question.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by