# masks_utils

Python API: `depthai_nodes.node.parsers.utils.masks_utils`

## Functions

| Name | Description |
| --- | --- |
| [`crop_mask`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/masks_utils.md#masks_utils-Functions-crop_mask) | It takes a mask and a bounding box, and returns a mask that is cropped to the bounding box. |
| [`get_segmentation_outputs`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/masks_utils.md#masks_utils-Functions-get_segmentation_outputs) | Extract dequantized NCHW mask coefficients and prototypes. |
| [`probability_to_logit_threshold`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/masks_utils.md#masks_utils-Functions-probability_to_logit_threshold) | Convert a probability threshold to logit space. |
| [`process_single_mask`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/masks_utils.md#masks_utils-Functions-process_single_mask) | Process a single mask. |
| [`process_single_mask_rfdetr`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/masks_utils.md#masks_utils-Functions-process_single_mask_rfdetr) | Process a single RF-DETR instance segmentation mask. |

### function `crop_mask`

```python
def crop_mask(
    mask: np.ndarray,
    bbox: np.ndarray,
    fill_value: float | int = 0,
) -> np.ndarray
```

It takes a mask and a bounding box, and returns a mask that is cropped to the bounding box.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `mask` | `np.ndarray` | [h, w] numpy array of a single mask | required |
| `bbox` | `np.ndarray` | A numpy array of bbox coordinates in (x_center, y_center, width, height) format | required |
| `fill_value` | `float | int` | Value assigned to pixels outside the bounding box. | `0` |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | A mask that is cropped to the bounding box |

### function `get_segmentation_outputs`

```python
def get_segmentation_outputs(
    output: dai.NNData,
    mask_output_layer_names: list[str] | None = None,
    protos_output_layer_name: str | None = None,
) -> tuple[list[np.ndarray], np.ndarray, int]
```

Extract dequantized NCHW mask coefficients and prototypes.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output` | `dai.NNData` | Neural network output message. | required |
| `mask_output_layer_names` | `list[str] | None` | Candidate mask-layer names. If omitted or empty, inspect every layer; select names containing `"mask"` and sort them lexically. | `None` |
| `protos_output_layer_name` | `str | None` | Prototype layer name; defaults to `"protos_output"`. | `None` |

Returns

| Type | Description |
| --- | --- |
| `tuple[list[np.ndarray], np.ndarray, int]` | A list of float32 coefficient tensors, the float32 prototype tensor, and its channel count. |

### function `probability_to_logit_threshold`

```python
def probability_to_logit_threshold(probability: float) -> float
```

Convert a probability threshold to logit space.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `probability` | `float` | Probability threshold; values at or below 0 and at or above 1 are handled as boundary cases. | required |

Returns

| Type | Description |
| --- | --- |
| `float` | The log odds, or negative/positive infinity at the lower/upper boundary. |

### function `process_single_mask`

```python
def process_single_mask(
    protos: np.ndarray,
    mask_coeff: np.ndarray,
    mask_conf: float,
    bbox: np.ndarray,
    output_shape: tuple[int, int],
) -> np.ndarray
```

Process a single mask.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `protos` | `np.ndarray` | Protos. | required |
| `mask_coeff` | `np.ndarray` | Mask coefficient. | required |
| `mask_conf` | `float` | Mask confidence. | required |
| `bbox` | `np.ndarray` | A numpy array of bbox coordinates in (x_center, y_center, width, height) normalized format. | required |
| `output_shape` | `tuple[int, int]` | Target mask shape as (height, width). | required |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Processed binary mask resized to `output_shape`. |

### function `process_single_mask_rfdetr`

```python
def process_single_mask_rfdetr(
    mask_logits: np.ndarray,
    mask_conf: float,
    input_shape: tuple[int, int],
) -> np.ndarray
```

Process a single RF-DETR instance segmentation mask.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `mask_logits` | `np.ndarray` | Mask logits for a single detection. | required |
| `mask_conf` | `float` | Mask confidence threshold. | required |
| `input_shape` | `tuple[int, int]` | Target output mask shape as (height, width). | required |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Processed mask resized to the model input shape. |
