Skip to content

Commit

Permalink
feat: Add crop_top_only boolean flag to control the image crop area
Browse files Browse the repository at this point in the history
  • Loading branch information
XinyuZhou committed Jul 1, 2024
1 parent 19ded02 commit 9d7007f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions dgp/annotations/camera_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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)

Expand Down

0 comments on commit 9d7007f

Please sign in to comment.