How do I fill 4-connected cells in an matrix?
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (3)
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
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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!