def get_crop(mask_image, scale_ratio=1.2):
    """
    Get bounding box of square encompassing all positive
    values of the mask, with a margin according to scale_ratio

    Args:
        scale_ratio: final crop is obtained by taking the square
            centered around the center of the tight bounding box
            around the masked values, with a square size of
            scale_ratio * size of largest bounding box dimension
    """
    mask_image = mask_image.numpy()
    xs, ys = (mask_image[:, :, ::3].sum(2) > 0).nonzero()
    x_min = xs.min()
    x_max = xs.max()
    y_min = ys.min()
    y_max = ys.max()
    radius = max((x_max - x_min), (y_max - y_min)) // 2 * scale_ratio
    x_c = (x_max + x_min) / 2
    y_c = (y_max + y_min) / 2
    x_min = max(int((x_c - radius).item()), 0)
    y_min = max(int((y_c - radius).item()), 0)
    x_max = int((x_c + radius).item())
    y_max = int((y_c + radius).item())
    return x_min, y_min, x_max, y_max