def grey_erosion(image, radius=None, mask=None, footprint=None):
    '''Perform a grey erosion with masking'''
    if footprint is None:
        if radius is None:
            footprint = np.ones((3,3),bool)
            radius = 1
        else:
            footprint = strel_disk(radius)==1
    else:
        radius = max(1, np.max(np.array(footprint.shape) // 2))
    iradius = int(np.ceil(radius))
    #
    # Do a grey_erosion with masked pixels = 1 so they don't participate
    #
    big_image = np.ones(np.array(image.shape)+iradius*2)
    big_image[iradius:-iradius,iradius:-iradius] = image
    if not mask is None:
        not_mask = np.logical_not(mask)
        big_image[iradius:-iradius,iradius:-iradius][not_mask] = 1
    processed_image = scind.grey_erosion(big_image, footprint=footprint)
    final_image = processed_image[iradius:-iradius,iradius:-iradius]
    if not mask is None:
        final_image[not_mask] = image[not_mask]
    return final_image
Perform a grey erosion with masking