def binarize_instances(
    sample_collection,
    in_field,
    threshold=1,
    output_dir=None,
    rel_dir=None,
    overwrite=False,
    progress=None,
):
    """Binarizes the instance segmentations in the specified field according to
    the provided threshold.

    Each instance mask is updated in-place with ``mask >= threshold``.

    Args:
        sample_collection: a
            :class:`fiftyone.core.collections.SampleCollection`
        in_field: the name of a
            :class:`fiftyone.core.labels.Detection` or
            :class:`fiftyone.core.labels.Detections` field containing instance
            segmentations
        threshold (1): the threshold for "instance" pixels
        output_dir (None): an optional output directory in which to write the
            instance segmentation images. If none is provided, the
            segmentations are overwritten in-place, either on disk or in the
            database
        rel_dir (None): an optional relative directory to strip from each input
            filepath to generate a unique identifier that is joined with
            ``output_dir`` to generate an output path for each instance
            segmentation image. This argument allows for populating nested
            subdirectories in ``output_dir`` that match the shape of the input
            paths. The path is converted to an absolute path (if necessary) via
            :func:`fiftyone.core.storage.normalize_path`
        overwrite (False): whether to delete ``output_dir`` prior to exporting
            if it exists
        progress (None): whether to render a progress bar (True/False), use the
            default value ``fiftyone.config.show_progress_bars`` (None), or a
            progress callback function to invoke instead
    """
    fov.validate_non_grouped_collection(sample_collection)
    fov.validate_collection_label_fields(
        sample_collection,
        in_field,
        (fol.Detection, fol.Detections),
    )

    select_fields = [in_field]
    in_field, processing_frames = sample_collection._handle_frame_field(
        in_field
    )
    if (
        processing_frames
        and output_dir is not None
        and sample_collection.has_field("frames.filepath")
    ):
        select_fields.append("frames.filepath")

    samples = sample_collection.select_fields(in_field)

    if overwrite and output_dir is not None:
        etau.delete_dir(output_dir)

    if output_dir is not None:
        filename_maker = fou.UniqueFilenameMaker(
            output_dir=output_dir, rel_dir=rel_dir, idempotent=False
        )

    for sample in samples.iter_samples(autosave=True, progress=progress):
        if processing_frames:
            images = sample.frames.values()
        else:
            images = [sample]

        for image in images:
            label = image[in_field]
            if label is None:
                continue

            if isinstance(label, fol.Detection):
                detections = [label]
            else:
                detections = label.detections

            for detection in detections:
                mask = detection.get_mask()
                if mask is None:
                    continue

                detection.mask = mask >= threshold

                if output_dir is not None:
                    mask_path = filename_maker.get_output_path(
                        _get_filepath(image, sample), output_ext=".png"
                    )
                elif detection.mask_path is not None:
                    mask_path = detection.mask_path
                    detection.mask_path = None
                else:
                    mask_path = None

                if mask_path is not None:
                    detection.export_mask(mask_path, update=True)
Binarizes the instance segmentations in the specified field according to
    the provided threshold.

    Each instance mask is updated in-place with ``mask >= threshold``.

    Args:
        sample_collection: a
            :class:`fiftyone.core.collections.SampleCollection`
        in_field: the name of a
            :class:`fiftyone.core.labels.Detection` or
            :class:`fiftyone.core.labels.Detections` field containing instance
            segmentations
        threshold (1): the threshold for "instance" pixels
        output_dir (None): an optional output directory in which to write the
            instance segmentation images. If none is provided, the
            segmentations are overwritten in-place, either on disk or in the
            database
        rel_dir (None): an optional relative directory to strip from each input
            filepath to generate a unique identifier that is joined with
            ``output_dir`` to generate an output path for each instance
            segmentation image. This argument allows for populating nested
            subdirectories in ``output_dir`` that match the shape of the input
            paths. The path is converted to an absolute path (if necessary) via
            :func:`fiftyone.core.storage.normalize_path`
        overwrite (False): whether to delete ``output_dir`` prior to exporting
            if it exists
        progress (None): whether to render a progress bar (True/False), use the
            default value ``fiftyone.config.show_progress_bars`` (None), or a
            progress callback function to invoke instead
    
fiftyone/utils/labels.py