Search code examples
matlabmatrixwavefront

MATLAB: Making matrix like in Wavefront algorithm


I have this kind of matrix (13 x 13):

0   0   0   0   0   0   0   0   0   0   0   0   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   1   1   1   1   0   1   1   1   1   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   0   0   0   0   0   0   0   0   0   0   0   0

Can I somehow increase the values around the central zero and the zeros that make the "walls" by one at each iteration and make it as this?

0   0   0   0   0   0   0   0   0   0   0   0   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   1   2   2   2   2   2   2   2   2   2   1   0
0   1   2   3   3   3   3   3   3   3   2   1   0
0   1   2   3   2   2   2   2   2   3   2   1   0
0   1   2   3   2   1   1   1   2   3   2   1   0
0   1   2   3   2   1   0   1   2   3   2   1   0
0   1   2   3   2   1   1   1   2   3   2   1   0
0   1   2   3   2   2   2   2   2   3   2   1   0
0   1   2   3   3   3   3   3   3   3   2   1   0
0   1   2   2   2   2   2   2   2   2   2   1   0
0   1   1   1   1   1   1   1   1   1   1   1   0
0   0   0   0   0   0   0   0   0   0   0   0   0

Solution

  • If you have the image processing toolbox, it's a one-liner:

    %# assume your matrix is called A
    result = bwdist(~A,'cityblock')
    

    'result' is the distance of each non-zero pixel in A to the nearest zero if you can only step horizontally or vertically.