# letterbox_resize

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

## 类

### LetterboxResize

通过填充来调整图像大小以保持纵横比的增强。

#### 方法

##### 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):
```

创建一个 `LetterboxResize` 增强。

参数

 * `height` (`int`): 输出图像的所需高度
 * `width` (`int`): 输出图像的所需宽度
 * `interpolation` (`int`): `cv2` 标志，用于指定调整大小时使用的插值。默认为 `cv2.INTER_LINEAR`。
 * `image_fill_value` (`ColorLike`): 图像的填充值。可以是颜色名称、十六进制字符串、灰度整数或 RGB 元组。默认为 `"black"`。
 * `mask_fill_value` (`int`): 掩码的填充值。必须是表示类标签的整数。默认为 `0`（背景）。
 * `p` (`float`): 应用转换的概率。默认为 `1.0`。

##### apply

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

将信箱增强应用于图像。

参数

 * `img` (`np.ndarray`): 要应用信箱调整大小的输入图像，形状为 (H, W, …)。
 * `pad_top` (`int`): 图像顶部要填充的像素数。
 * `pad_bottom` (`int`): 图像底部要填充的像素数。
 * `pad_left` (`int`): 图像左侧要填充的像素数。
 * `pad_right` (`int`): 图像右侧要填充的像素数。
 * `**_`

返回

 * `np.ndarray`: 调整大小并填充后的图像。

##### 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:
```

将信箱增强应用于边界框。

参数

 * `bbox` (`np.ndarray`): 要应用信箱调整大小的输入边界框，形状为 (N, 4 + )。单个边界框应采用 (xmin, ymin, xmax, ymax,…) 格式，并归一化到 [0, 1] 范围。尾随维度（如果有）不会被增强修改。
 * `pad_top` (`int`): 图像顶部要填充的像素数。
 * `pad_bottom` (`int`): 图像底部要填充的像素数。
 * `pad_left` (`int`): 图像左侧要填充的像素数。
 * `pad_right` (`int`): 图像右侧要填充的像素数。
 * `**_`

返回

 * `np.ndarray`: 转换后的边界框，格式和归一化与输入相同。

##### 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:
```

将信箱增强应用于关键点。

参数

 * `keypoint` (`np.ndarray`): 要应用信箱调整大小的输入关键点，形状为 (N, 2 + )。单个关键点应采用 (x, y, …) 格式，并归一化到 [0, 1] 范围。
 * `pad_top` (`int`): 图像顶部要填充的像素数。
 * `pad_bottom` (`int`): 图像底部要填充的像素数。
 * `pad_left` (`int`): 图像左侧要填充的像素数。
 * `pad_right` (`int`): 图像右侧要填充的像素数。
 * `orig_height` (`int`): 调整大小前图像的原始高度。
 * `orig_width` (`int`): 调整大小前图像的原始宽度。
 * `**_`

返回

 * `np.ndarray`: 转换后的关键点，格式和归一化与输入相同。转换后超出图像边界的关键点，其坐标将设置为 − 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:
```

将信箱增强应用于输入掩码。

参数

 * `mask` (`np.ndarray`): 要应用信箱调整大小的输入掩码，形状为 (H, W, …)。
 * `pad_top` (`int`): 掩码顶部要填充的像素数。
 * `pad_bottom` (`int`): 掩码底部要填充的像素数。
 * `pad_left` (`int`): 掩码左侧要填充的像素数。
 * `pad_right` (`int`): 掩码右侧要填充的像素数。
 * `**_`

返回

 * `np.ndarray`: 调整大小并填充后的掩码。

##### compute_padding

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

计算将图像调整为信箱格式所需的填充。

参数

 * `orig_height` (`int`): 图像的原始高度。
 * `orig_width` (`int`): 图像的原始宽度。
 * `out_height` (`int`): 输出的所需高度。
 * `out_width` (`int`): 输出的所需宽度。

返回

 * `tuple[int, int, int, int]`: 图像顶部、底部、左侧和右侧的填充值。

##### get_params_dependent_on_data

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

返回依赖于输入的参数。

参数

 * `params` (`dict[str, Any]`): 现有的增强参数字典。
 * `data` (`dict[str, Any]`): 包含输入数据的字典。

返回

 * `dict[str, Any]`: 包含增强所需的额外参数的字典，例如填充值和原始图像尺寸。

#### 属性

##### height

输出图像的所需高度。

##### targets

定义将应用增强的目标。

##### width

输出图像的所需宽度。

## 函数

### 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:
```

创建一个调整大小转换，当保持纵横比时，它将是信箱式的。
