Skip to content

Commit 0b6f244

Browse files
authored
feat: add crop_top_only boolean flag to control the image crop area (#165)
1 parent 19ded02 commit 0b6f244

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

dgp/annotations/camera_transforms.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ def _calc_shape(
749749

750750

751751
class CropScaleTransform(AffineCameraTransform):
752-
def __init__(self, target_shape: Tuple[int, int], fix_h: bool = True) -> None:
752+
def __init__(self, target_shape: Tuple[int, int], fix_h: bool = True, crop_top_only: bool = False) -> None:
753753
"""Extracts a crop from the center of an image and resizes to target_shape.
754754
This attempts to match the aspect ratio of target_shape and does not stretch the crop.
755755
@@ -760,9 +760,14 @@ def __init__(self, target_shape: Tuple[int, int], fix_h: bool = True) -> None:
760760
fix_h: bool, default=True
761761
If True, fixes the height and modifies the width to maintain the desired aspect ratio.
762762
Otherwise fixes the width and moifies the height.
763+
crop_top_only: bool, default=False
764+
only valid when fix_h=False
765+
If True, crop the top part of the image.
766+
Otherwise crop the top and bottom.
763767
"""
764768
self.shape = target_shape[:2]
765769
self.fix_h = fix_h
770+
self.crop_top_only = crop_top_only
766771
super().__init__(shape=self.shape)
767772

768773
def _calc_A(
@@ -780,7 +785,7 @@ def _calc_A(
780785
box = [newx / 2, 0, w - newx / 2, h]
781786
else:
782787
newy = h - w * aspect_ratio
783-
box = [0, newy / 2, w, h - newy / 2]
788+
box = [0, newy, w, h] if self.crop_top_only else [0, newy / 2, w, h - newy / 2]
784789

785790
return box_crop_affine_transform(box, self.shape)
786791

0 commit comments

Comments
 (0)