# masks_utils

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

## Functions

### 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.

Parameters

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

Returns

 * `np.ndarray`: A mask that is cropped to the bounding box

### 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.

Parameters

 * `output` (`dai.NNData`): Neural network output message.
 * `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.
 * `protos_output_layer_name` (`str | None`): Prototype layer name; defaults to `"protos_output"`.

Returns

 * `tuple[list[np.ndarray], np.ndarray, int]`: A list of float32 coefficient tensors, the float32 prototype tensor, and its
   channel count.

### probability_to_logit_threshold

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

Convert a probability threshold to logit space.

Parameters

 * `probability` (`float`): Probability threshold; values at or below 0 and at or above 1 are handled as boundary cases.

Returns

 * `float`: The log odds, or negative/positive infinity at the lower/upper boundary.

### 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.

Parameters

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

Returns

 * `np.ndarray`: Processed binary mask resized to `output_shape`.

### 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.

Parameters

 * `mask_logits` (`np.ndarray`): Mask logits for a single detection.
 * `mask_conf` (`float`): Mask confidence threshold.
 * `input_shape` (`tuple[int, int]`): Target output mask shape as (height, width).

Returns

 * `np.ndarray`: Processed mask resized to the model input shape.
