# letterbox_resize

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

## Classes

### LetterboxResize

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

#### Methods

##### init

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

Create a `LetterboxResize` augmentation.

Parameters

 * `height` (`int`): The desired height of the output image
 * `width` (`int`): The desired width of the output image
 * `interpolation` (`int`): `cv2` flag to specify interpolation used when resizing. Defaults to `cv2.INTER_LINEAR`.
 * `image_fill_value` (`Color`): Padding value for images. Can be a string color name or an RGB tuple. Defaults to `"black"`.
 * `mask_fill_value` (`int`): Padding value for masks. Must be an integer representing a class label. Defaults to `0`
   (background).
 * `p` (`float`): The probability of applying the transform. Defaults to `1.0`.

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

Parameters

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

Returns

 * `np.ndarray`: Resized and padded image.

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

Parameters

 * `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.
 * `pad_top` (`int`): The number of pixels to pad at the top of the image.
 * `pad_bottom` (`int`): The number of pixels to pad at the bottom of the image.
 * `pad_left` (`int`): The number of pixels to pad on the left side of the image.
 * `pad_right` (`int`): The number of pixels to pad on the right side of the image.
 * `**_`

Returns

 * `np.ndarray`: Transformed bounding boxes in the same format and normalization as the input.

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

Parameters

 * `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].
 * `pad_top` (`int`): The number of pixels to pad at the top of the image.
 * `pad_bottom` (`int`): The number of pixels to pad at the bottom of the image.
 * `pad_left` (`int`): The number of pixels to pad on the left side of the image.
 * `pad_right` (`int`): The number of pixels to pad on the right side of the image.
 * `orig_height` (`int`): Original height of the image before resizing.
 * `orig_width` (`int`): Original width of the image before resizing.
 * `**_`

Returns

 * `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.

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

Parameters

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

Returns

 * `np.ndarray`: Resized and padded mask.

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

Parameters

 * `orig_height` (`int`): Original height of the image.
 * `orig_width` (`int`): Original width of the image.
 * `out_height` (`int`): Desired height of the output.
 * `out_width` (`int`): Desired width of the output.

Returns

 * `tuple[int, int, int, int]`: Padding values for the top, bottom, left and right sides of the image.

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

Parameters

 * `params` (`dict[str, Any]`): The existing augmentation parameters dictionary.
 * `data` (`dict[str, Any]`): The dictionary with input data.

Returns

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

#### Attributes

##### height

The desired height of the output image.

##### targets

Define the targets the augmentation will be applied to.

##### width

The desired width of the output image.
