def postprocess_small_regions(mask_data: MaskData,
                                  min_area: int,
                                  nms_thresh: float) -> MaskData:
        """
        Removes small disconnected regions and holes in masks, then reruns
        box NMS to remove any new duplicates.

        Edits mask_data in place.

        Requires open-cv as a dependency.
        """
        if len(mask_data["rles"]) == 0:
            return mask_data

        # Filter small disconnected regions and holes
        new_masks = []
        scores = []
        for rle in mask_data["rles"]:
            mask = rle_to_mask(rle)

            mask, changed = remove_small_regions(mask, min_area, mode="holes")
            unchanged = not changed
            mask, changed = remove_small_regions(mask, min_area, mode="islands")
            unchanged = unchanged and not changed

            new_masks.append(paddle.to_tensor(mask).unsqueeze(0))
            # Give score=0 to changed masks and score=1 to unchanged masks
            # so NMS will prefer ones that didn't need postprocessing
            scores.append(float(unchanged))

        # Recalculate boxes and remove any new duplicates
        masks = paddle.concat(new_masks, axis=0)
        boxes = batched_mask_to_box(masks)
        keep_by_nms = nms(
            boxes.cast('float32'),
            scores=paddle.to_tensor(scores),
            category_idxs=paddle.zeros(len(boxes)),  # categories
            iou_threshold=nms_thresh, )

        # Only recalculate RLEs for masks that have changed
        for i_mask in keep_by_nms:
            if scores[i_mask] == 0.0:
                mask_paddle = masks[i_mask].unsqueeze(0)
                mask_data["rles"][i_mask] = mask_to_rle_paddle(mask_paddle)[0]
                mask_data["boxes"][i_mask] = boxes[
                    i_mask]  # update res directly
        mask_data.filter(keep_by_nms)

        return mask_data

        Removes small disconnected regions and holes in masks, then reruns
        box NMS to remove any new duplicates.

        Edits mask_data in place.

        Requires open-cv as a dependency.
        
contrib/SegmentAnything/segment_anything/automatic_mask_generator.py