def thin(image, max_iter=None):
 max_iter = (max_iter or sys.maxsize)
 assert_nD(image, 2)
 skel = np.asanyarray(image, dtype=bool).astype(np.uint8)
 mask = np.array([[8, 4, 2], [16, 0, 1], [32, 64, 128]], dtype=np.uint8)
 for i in range(max_iter):
  before = np.sum(skel)
  for lut in [G123_LUT, G123P_LUT]:
   N = ndi.correlate(skel, mask, mode='constant')
   D = np.take(lut, N)
   skel[D] = 0
  after = np.sum(skel)
  if (before == after):
   break
 return skel.astype(np.bool)

def thin ( image , max_iter = None ) : max_iter = ( max_iter or sys . maxsize ) assert_nD ( image , 2 ) skel = np . asanyarray ( image , dtype = bool ) . astype ( np . uint8 ) mask = np . array ( [ [ 8 , 4 , 2 ] , [ 16 , 0 , 1 ] , [ 32 , 64 , 128 ] ] , dtype = np . uint8 ) for i in range ( max_iter ) : before = np . sum ( skel ) for lut in [ G123_LUT , G123P_LUT ] : N = ndi . correlate ( skel , mask , mode = 'constant' ) D = np . take ( lut , N ) skel [ D ] = 0 after = np . sum ( skel ) if ( before == after ) : break return skel . astype ( np . bool )
perform morphological thinning of a binary image .