# letterbox_resize

Python API: `luxonis_ml.data.augmentations.custom.letterbox_resize`

## Classes

| Name | Description |
| --- | --- |
|
[`LetterboxResize`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| Augmentation that resizes an image with padding to maintain the aspect ratio. |

### class `LetterboxResize`

```python
class LetterboxResize(A.DualTransform)
```

Augmentation that resizes an image with padding to maintain the aspect ratio.

Attributes

| Name | Description |
| --- | --- |
| `height` | The desired height of the output image. |
| `width` | The desired width of the output image. |

Properties

| Name | Type | Description |
| --- | --- | --- |
|
[`targets`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| `dict[str, Any]` | Define the targets the augmentation will be applied to. |

Methods

| Name | Description |
| --- | --- |
|
[`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| Create a `LetterboxResize` augmentation. |
|
[`apply`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| Apply the letterbox augmentation to an image. |
|
[`apply_to_bboxes`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| Apply letterbox augmentation to the bounding box. |
|
[`apply_to_keypoints`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| Apply letterbox augmentation to the keypoint. |
|
[`apply_to_mask`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| Apply letterbox augmentation to the input mask. |
|
[`compute_padding`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| Compute the padding required to resize an image to the letterbox format. |
|
[`get_params_dependent_on_data`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| Return parameters dependent on input. |

#### property `targets`

```python
targets: dict[str, Any]
```

Define the targets the augmentation will be applied to.

#### method `__init__`

```python
def __init__(
    height: int,
    width: int,
    interpolation: int = cv2.INTER_LINEAR,
    image_fill_value: ColorLike = 'black',
    mask_fill_value: int = 0,
    p: float = 1.0,
)
```

Create a `LetterboxResize` augmentation.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `height` | `int` | The desired height of the output image | required |
| `width` | `int` | The desired width of the output image | required |
| `interpolation` | `int` | `cv2` flag to specify interpolation used when resizing. Defaults to `cv2.INTER_LINEAR`. |
`cv2.INTER_LINEAR` |
| `image_fill_value` | `ColorLike` | Padding value for images. Can be a color name, a hex string, a grayscale integer, or an RGB
tuple. Defaults to `"black"`. | `'black'` |
| `mask_fill_value` | `int` | Padding value for masks. Must be an integer representing a class label. Defaults to `0`
(background). | `0` |
| `p` | `float` | The probability of applying the transform. Defaults to `1.0`. | `1.0` |

#### method `apply`

```python
def apply(
    img: np.ndarray,
    pad_top: int,
    pad_bottom: int,
    pad_left: int,
    pad_right: int,
    **_,
) -> np.ndarray
```

Apply the letterbox augmentation to an image.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `img` | `np.ndarray` | The input image of shape (H, W, …) to which the letterbox resize will be applied. | required |
| `pad_top` | `int` | The number of pixels to pad at the top of the image. | required |
| `pad_bottom` | `int` | The number of pixels to pad at the bottom of the image. | required |
| `pad_left` | `int` | The number of pixels to pad on the left side of the image. | required |
| `pad_right` | `int` | The number of pixels to pad on the right side of the image. | required |
| `**_` |  |  |  |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Resized and padded image. |

#### method `apply_to_bboxes`

```python
def apply_to_bboxes(
    bbox: np.ndarray,
    pad_top: int,
    pad_bottom: int,
    pad_left: int,
    pad_right: int,
    **_,
) -> np.ndarray
```

Apply letterbox augmentation to the bounding box.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `bbox` | `np.ndarray` | The input bounding boxes of shape (N, 4 + ) to which the letterbox resize will be applied. Individual
bounding boxes should be in the format (xmin, ymin, xmax, ymax,…) and normalized to the range [0, 1]. The trailing dimensions (if
any) are not modified by the augmentation. | required |
| `pad_top` | `int` | The number of pixels to pad at the top of the image. | required |
| `pad_bottom` | `int` | The number of pixels to pad at the bottom of the image. | required |
| `pad_left` | `int` | The number of pixels to pad on the left side of the image. | required |
| `pad_right` | `int` | The number of pixels to pad on the right side of the image. | required |
| `**_` |  |  |  |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Transformed bounding boxes in the same format and normalization as the input. |

#### method `apply_to_keypoints`

```python
def apply_to_keypoints(
    keypoint: np.ndarray,
    pad_top: int,
    pad_bottom: int,
    pad_left: int,
    pad_right: int,
    orig_height: int,
    orig_width: int,
    **_,
) -> np.ndarray
```

Apply letterbox augmentation to the keypoint.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `keypoint` | `np.ndarray` | The input keypoints of shape (N, 2 + ) to which the letterbox resize will be applied. Individual
keypoints should be in the format (x, y, …) and normalized to the range [0, 1]. | required |
| `pad_top` | `int` | The number of pixels to pad at the top of the image. | required |
| `pad_bottom` | `int` | The number of pixels to pad at the bottom of the image. | required |
| `pad_left` | `int` | The number of pixels to pad on the left side of the image. | required |
| `pad_right` | `int` | The number of pixels to pad on the right side of the image. | required |
| `orig_height` | `int` | Original height of the image before resizing. | required |
| `orig_width` | `int` | Original width of the image before resizing. | required |
| `**_` |  |  |  |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Transformed keypoints in the same format and normalization as the input. Keypoints that fall outside the image
boundaries after transformation will have their coordinates set to − 1. |

#### method `apply_to_mask`

```python
def apply_to_mask(
    mask: np.ndarray,
    pad_top: int,
    pad_bottom: int,
    pad_left: int,
    pad_right: int,
    **_,
) -> np.ndarray
```

Apply letterbox augmentation to the input mask.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `mask` | `np.ndarray` | The input mask of shape (H, W, …) to which the letterbox resize will be applied. | required |
| `pad_top` | `int` | The number of pixels to pad at the top of the mask. | required |
| `pad_bottom` | `int` | The number of pixels to pad at the bottom of the mask. | required |
| `pad_left` | `int` | The number of pixels to pad on the left side of the mask. | required |
| `pad_right` | `int` | The number of pixels to pad on the right side of the mask. | required |
| `**_` |  |  |  |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Resized and padded mask. |

#### static method `compute_padding`

```python
def compute_padding(
    orig_height: int,
    orig_width: int,
    out_height: int,
    out_width: int,
) -> tuple[int, int, int, int]
```

Compute the padding required to resize an image to the letterbox format.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `orig_height` | `int` | Original height of the image. | required |
| `orig_width` | `int` | Original width of the image. | required |
| `out_height` | `int` | Desired height of the output. | required |
| `out_width` | `int` | Desired width of the output. | required |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, int, int, int]` | Padding values for the top, bottom, left and right sides of the image. |

#### method `get_params_dependent_on_data`

```python
def get_params_dependent_on_data(
    params: dict[str, Any],
    data: dict[str, Any],
) -> dict[str, Any]
```

Return parameters dependent on input.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `params` | `dict[str, Any]` | The existing augmentation parameters dictionary. | required |
| `data` | `dict[str, Any]` | The dictionary with input data. | required |

Returns

| Type | Description |
| --- | --- |
| `dict[str, Any]` | A dictionary with extra parameters required for the augmentation, such as padding values and original image
dimensions. |

## Functions

| Name | Description |
| --- | --- |
|
[`create_letterbox_or_resize`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/letterbox_resize.md)
| Create a resize transform, letterboxed when aspect ratio is kept. |

### function `create_letterbox_or_resize`

```python
def create_letterbox_or_resize(
    keep_aspect_ratio: bool,
    height: int,
    width: int,
    p: float = 1.0,
) -> LetterboxResize | A.Resize
```

Create a resize transform, letterboxed when aspect ratio is kept.
