Finding 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, and I would appreciate hearing your input in the comments below. This algorithm gives similar results to Matlab boundarymask().

Algorithm

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. In my case, I 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()`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.distance_transform_cdt.html#scipy.ndimage.distance_transform_cdt) was six times faster and gave equivalent results to the more general [`distance_transform_edt()`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.distance_transform_edt.html#scipy.ndimage.distance_transform_edt). ## Notes [Reference](https://github.com/scikit-image/scikit-image/issues/1131#issuecomment-279074053)

Tags:

Categories:

Written by Michael Hirsch, Ph.D. //

Comments