def compute_dilated_mask(input_mask, kr, r):
    """Buffer the input rain mask using the given kernel. Add a grayscale rim
    for smooth rain/no-rain transition by iteratively dilating the mask.

    Parameters
    ----------
    input_mask : array_like
        Two-dimensional boolean array containing the input mask.
    kr : array_like
        Structuring element for the dilation.
    r : int
        The number of iterations for the dilation.

    Returns
    -------
    out : array_like
        The dilated mask normalized to the range [0,1].
    """
    # buffer the input mask
    input_mask = np.ndarray.astype(input_mask.copy(), "uint8")
    mask_dilated = binary_dilation(input_mask, kr)

    # add grayscale rim
    kr1 = generate_binary_structure(2, 1)
    mask = mask_dilated.astype(float)
    for _ in range(r):
        mask_dilated = binary_dilation(mask_dilated, kr1)
        mask += mask_dilated

    # normalize between 0 and 1
    return mask / mask.max()
Buffer the input rain mask using the given kernel. Add a grayscale rim
for smooth rain/no-rain transition by iteratively dilating the mask.

Parameters
----------
input_mask : array_like
Two-dimensional boolean array containing the input mask.
kr : array_like
Structuring element for the dilation.
r : int
The number of iterations for the dilation.

Returns
-------
out : array_like
The dilated mask normalized to the range [0,1].
pysteps/nowcasts/utils.py