6

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
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Aidos
  • 729
  • 7
  • 20

1 Answers1

6

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.

Jonas
  • 74,690
  • 10
  • 137
  • 177
  • Thank you very much Jonas. Sorry for disturbing again, but do you know how can I write this function (bwdist) by myself? Do you know related algorithm? – Aidos Mar 12 '15 at 17:07
  • @Fabi: If you do not have access to the image processing toolbox, I suggest getting `mexopencv` and to use their distance transform. It's a lot faster than writing a distance transform yourself. If you really, really do want to cook your own distance transform, have a look at [this question](http://stackoverflow.com/questions/7426136/fastest-available-algorithm-for-distance-transform) for algorithms to implement. – Jonas Mar 13 '15 at 07:39
  • 1
    The algorithm is pretty simple. Initialize the output with 0 wherever you want the final value to be 0, and `inf` otherwise. Then for each row, do a left-to-right pass wherein you set each value to `min(current value, previous value+1)`. Repeat for each row but going right-to-left, then also do the same for each column first going top-to-bottom, followed by bottom-to-top. In your particular example, you could have of course skipped the initialization step since the 0s are where you want them and everything else is >=1. – Nicu Stiurca May 14 '15 at 21:32