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
ubuntu (generated by with_the_same_user script) committed Jul 1, 2024
1 parent f108856 commit 2e091b3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 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
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ torch>=1.8.1
torchvision>=0.9.1
tqdm>=4.29.1
tenacity>=6.0.0
xarray==2022.6.0
xarray>=2022.6.0
numpy>=1.20.3

0 comments on commit 2e091b3

Please sign in to comment.