Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add crop_top_only boolean flag to control the image crop area. #165

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading