From 0b6f24405108de63a8eea29fa41563d3eef4c23c Mon Sep 17 00:00:00 2001 From: XinyuZhou <52957372+XinyuZhou@users.noreply.github.com> Date: Mon, 1 Jul 2024 17:47:46 -0700 Subject: [PATCH] feat: add crop_top_only boolean flag to control the image crop area (#165) --- dgp/annotations/camera_transforms.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dgp/annotations/camera_transforms.py b/dgp/annotations/camera_transforms.py index 2bba670..cc5ba6b 100644 --- a/dgp/annotations/camera_transforms.py +++ b/dgp/annotations/camera_transforms.py @@ -749,7 +749,7 @@ def _calc_shape( class CropScaleTransform(AffineCameraTransform): - def __init__(self, target_shape: Tuple[int, int], fix_h: bool = True) -> None: + def __init__(self, target_shape: Tuple[int, int], fix_h: bool = True, crop_top_only: bool = False) -> None: """Extracts a crop from the center of an image and resizes to target_shape. This attempts to match the aspect ratio of target_shape and does not stretch the crop. @@ -760,9 +760,14 @@ def __init__(self, target_shape: Tuple[int, int], fix_h: bool = True) -> None: fix_h: bool, default=True If True, fixes the height and modifies the width to maintain the desired aspect ratio. Otherwise fixes the width and moifies the height. + crop_top_only: bool, default=False + only valid when fix_h=False + If True, crop the top part of the image. + Otherwise crop the top and bottom. """ self.shape = target_shape[:2] self.fix_h = fix_h + self.crop_top_only = crop_top_only super().__init__(shape=self.shape) def _calc_A( @@ -780,7 +785,7 @@ def _calc_A( box = [newx / 2, 0, w - newx / 2, h] else: newy = h - w * aspect_ratio - box = [0, newy / 2, w, h - newy / 2] + box = [0, newy, w, h] if self.crop_top_only else [0, newy / 2, w, h - newy / 2] return box_crop_affine_transform(box, self.shape)