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

1 Answer 1

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.

3
  • 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, 2015 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 for algorithms to implement.
    – Jonas
    Mar 13, 2015 at 7: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. May 14, 2015 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.