Find boundary indices of region edges in Python

The convex hull is frequently used to process a pixel region. To find the indices of the outer edge of the convex hull may be accomplished using the distance transform. I do not claim this to be the most efficient method. This algorithm gives similar results to Matlab boundarymask().


By definition, the distance transform of all region edge pixels to the background is identically one. The chamfer distance transform is suitable for smooth regional boundaries, and has significant speed advantages over more brute force approaches that may be more generally applicable for non-smooth boundaries. Use either masked image arrays or use NaN as a sentinel value in 2-D array mask. SciPy ≥ 0.17 is assumed.

import scipy.ndimage as ndi

boundary_mask = ndi.distance_transform_cdt(~np.isnan(az), 'taxicab') == 1
coin with red boundary outline

Red boundary represents boundary_mask pixels.

For the smooth boundary cases I tried, distance_transform_cdt() was six times faster and gave equivalent results to the more general distance_transform_edt().