class documentation
class LetterboxResize(A.DualTransform):
Constructor: LetterboxResize(height, width, interpolation, image_fill_value, ...)
Augmentation that resizes an image with padding to maintain the aspect ratio.
| Static Method | compute |
Compute the padding required to resize an image to the letterbox format. |
| Method | __init__ |
Create a LetterboxResize augmentation. |
| Method | apply |
Apply the letterbox augmentation to an image. |
| Method | apply |
Apply letterbox augmentation to the bounding box. |
| Method | apply |
Apply letterbox augmentation to the keypoint. |
| Method | apply |
Apply letterbox augmentation to the input mask. |
| Method | get |
Return parameters dependent on input. |
| Instance Variable | height |
The desired height of the output image. |
| Instance Variable | width |
The desired width of the output image. |
| Property | targets |
Define the targets the augmentation will be applied to. |
| Method | _apply |
Undocumented |
| Instance Variable | _image |
Undocumented |
| Instance Variable | _interpolation |
Undocumented |
| Instance Variable | _mask |
Undocumented |
@staticmethod
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 | |
origint | Original height of the image. |
origint | Original width of the image. |
outint | Desired height of the output. |
outint | Desired width of the output. |
| Returns | |
tuple[ | Padding values for the top, bottom, left and right sides of the image. |
def __init__(self, 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. |
imageColor | Padding value for images. Can be a string color name or an RGB tuple. Defaults to "black". |
maskint | 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. |
@override
def apply(self, 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. |
padint | The number of pixels to pad at the top of the image. |
padint | The number of pixels to pad at the bottom of the image. |
padint | The number of pixels to pad on the left side of the image. |
padint | The number of pixels to pad on the right side of the image. |
| **_ | Undocumented |
| Returns | |
np.ndarray | Resized and padded image. |
@override
def apply_to_bboxes(self, 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. |
padint | The number of pixels to pad at the top of the image. |
padint | The number of pixels to pad at the bottom of the image. |
padint | The number of pixels to pad on the left side of the image. |
padint | The number of pixels to pad on the right side of the image. |
| **_ | Undocumented |
| Returns | |
np.ndarray | Transformed bounding boxes in the same format and normalization as the input. |
@override
def apply_to_keypoints(self, 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]. |
padint | The number of pixels to pad at the top of the image. |
padint | The number of pixels to pad at the bottom of the image. |
padint | The number of pixels to pad on the left side of the image. |
padint | The number of pixels to pad on the right side of the image. |
origint | Original height of the image before resizing. |
origint | Original width of the image before resizing. |
| **_ | Undocumented |
| 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. |
@override
def apply_to_mask(self, 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. |
padint | The number of pixels to pad at the top of the mask. |
padint | The number of pixels to pad at the bottom of the mask. |
padint | The number of pixels to pad on the left side of the mask. |
padint | The number of pixels to pad on the right side of the mask. |
| **_ | Undocumented |
| Returns | |
np.ndarray | Resized and padded mask. |
@override
def get_params_dependent_on_data(self, params: dict[ str, Any], data: dict[ str, Any]) -> dict[ str, Any]:
¶
Return parameters dependent on input.
| Parameters | |
params:dict[ | The existing augmentation parameters dictionary. |
data:dict[ | The dictionary with input data. |
| Returns | |
dict[ | A dictionary with extra parameters required for the augmentation, such as padding values and original image dimensions. |
def _apply_to_image_data(self, img:
np.ndarray, pad_top: int, pad_bottom: int, pad_left: int, pad_right: int, interpolation: int, fill_value: RGB) -> np.ndarray:
¶
Undocumented