There is actually an algorithm for the thinning algorithm Zhang-Suen. You can find its code here: http://rosettacode.org/wiki/Zhang-Suen_thinning_algorithm
I also wrote a vectorized version of this in Python, which is about 10 times faster than this code. Here is the code:
def neighbours_vec(image): return image[2:,1:-1], image[2:,2:], image[1:-1,2:], image[:-2,2:], image[:-2,1:-1], image[:-2,:-2], image[1:-1,:-2], image[2:,:-2] def transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9): return ((P3-P2) > 0).astype(int) + ((P4-P3) > 0).astype(int) + \ ((P5-P4) > 0).astype(int) + ((P6-P5) > 0).astype(int) + \ ((P7-P6) > 0).astype(int) + ((P8-P7) > 0).astype(int) + \ ((P9-P8) > 0).astype(int) + ((P2-P9) > 0).astype(int) def zhangSuen_vec(image, iterations): for iter in range (1, iterations): print iter
Abtin rasoulian
source share