def filter_with_labels(labels, img, BG=True):
    """
    Modify an image so that background regions in labels have value 0
    and the rest of the pixels have their original value.
    Setting BG to False will set foreground regions to be 0.
    """
    if BG:
        img[labels == 0] = 0
    if not BG:
        img[labels > 0] = 0
    return img