Calculate perimeter of numpy array
I want to calculate the perimeter of a given numpy array structure. With perimeter i mean the exact perimeter of the structure in the numpy array. The structure could include holes. My current aproach is something like this: import numpy a = numpy.zeros((6,6), dtype=numpy.int) a[1:5, 1:5] = 1;a[3,3] = 0 # Way 1 s = ndimage.generate_binary_structure(2,1) c = ndimage.binary_dilation(a,s).astype(a.dtype) b = c - a numpy.sum(b) # The result, however the hole is calculated as 1, although there are 4 edges # Way 2 b = ndimage.distance_transform_cdt(a == 0,metric='taxicab') == 1 b = b.astype(int)