Fill 4-connected cells exclusively

조회 수: 13 (최근 30일)
Julia
Julia 2011년 3월 31일
Dear all,
yesterday a asked a question about how to fill 4-connected cells. Several comments about the use of 'imfill' were given to me. I thank the contributors for them but the option chosen does not give exactly what I need. The example matrix that I gave 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 an easy way to get this?
Again, thank you in advance.

채택된 답변

Steve Eddins
Steve Eddins 2011년 3월 31일
I assume you meant your sample matrices to be 8-by-8 instead of 1-by-64? If that's true, then it looks like you want to change a pixel from a 0 to a 1 only if all of its 4-connected neighbors are 1. The following code uses bwhitmiss to identify such pixels:
se1 = [0 1 0; 1 0 1; 0 1 0];
se2 = [0 0 0; 0 1 0; 0 0 0];
pixels_to_change = bwhitmiss(BW1, se1, se2);
Now just elementwise-or with the original matrix:
BW3 = BW1 | pixels_to_change

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by