def perimeter(image, neighbourhood=4):
 if (neighbourhood == 4):
  strel = STREL_4
 else:
  strel = STREL_8
 image = image.astype(np.uint8)
 eroded_image = ndi.binary_erosion(image, strel, border_value=0)
 border_image = (image - eroded_image)
 perimeter_weights = np.zeros(50, dtype=np.double)
 perimeter_weights[[5, 7, 15, 17, 25, 27]] = 1
 perimeter_weights[[21, 33]] = sqrt(2)
 perimeter_weights[[13, 23]] = ((1 + sqrt(2)) / 2)
 perimeter_image = ndi.convolve(border_image, np.array([[10, 2, 10], [2, 1, 2], [10, 2, 10]]), mode='constant', cval=0)
 perimeter_histogram = np.bincount(perimeter_image.ravel(), minlength=50)
 total_perimeter = np.dot(perimeter_histogram, perimeter_weights)
 return total_perimeter

def perimeter ( image , neighbourhood = 4 ) : if ( neighbourhood == 4 ) : strel = STREL_4 else : strel = STREL_8 image = image . astype ( np . uint8 ) eroded_image = ndi . binary_erosion ( image , strel , border_value = 0 ) border_image = ( image - eroded_image ) perimeter_weights = np . zeros ( 50 , dtype = np . double ) perimeter_weights [ [ 5 , 7 , 15 , 17 , 25 , 27 ] ] = 1 perimeter_weights [ [ 21 , 33 ] ] = sqrt ( 2 ) perimeter_weights [ [ 13 , 23 ] ] = ( ( 1 + sqrt ( 2 ) ) / 2 ) perimeter_image = ndi . convolve ( border_image , np . array ( [ [ 10 , 2 , 10 ] , [ 2 , 1 , 2 ] , [ 10 , 2 , 10 ] ] ) , mode = 'constant' , cval = 0 ) perimeter_histogram = np . bincount ( perimeter_image . ravel ( ) , minlength = 50 ) total_perimeter = np . dot ( perimeter_histogram , perimeter_weights ) return total_perimeter
calculate total perimeter of all objects in binary image .