def distance_transform(mask):
    """
    For every non-zero value, compute the distance to the closest zero value.
    Based on :cite:`felzenszwalb2012distance`.

    Parameters
    ----------
    mask: numpy.ndarray
        2D matrix of zero and nonzero values.

    Returns
    -------
    distance: numpy.ndarray
        Distance to closest zero-valued pixel.

    Example
    -------
    >>> from pymatting import *
    >>> import numpy as np
    >>> mask = np.random.rand(10, 20) < 0.9
    >>> distance = distance_transform(mask)
    """
    # Ensure that mask.dtype is boolean
    if mask.dtype != np.bool_:
        mask = mask != 0

    ny, nx = mask.shape
    distance = np.zeros((ny, nx), np.float64)
    distance[mask] = np.inf

    _propagate_distance(distance)

    return distance

    For every non-zero value, compute the distance to the closest zero value.
    Based on :cite:`felzenszwalb2012distance`.

    Parameters
    ----------
    mask: numpy.ndarray
        2D matrix of zero and nonzero values.

    Returns
    -------
    distance: numpy.ndarray
        Distance to closest zero-valued pixel.

    Example
    -------
    >>> from pymatting import *
    >>> import numpy as np
    >>> mask = np.random.rand(10, 20) < 0.9
    >>> distance = distance_transform(mask)
    
pymatting/util/distance.py