@@ -749,7 +749,7 @@ def _calc_shape(
749
749
750
750
751
751
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 :
753
753
"""Extracts a crop from the center of an image and resizes to target_shape.
754
754
This attempts to match the aspect ratio of target_shape and does not stretch the crop.
755
755
@@ -760,9 +760,14 @@ def __init__(self, target_shape: Tuple[int, int], fix_h: bool = True) -> None:
760
760
fix_h: bool, default=True
761
761
If True, fixes the height and modifies the width to maintain the desired aspect ratio.
762
762
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.
763
767
"""
764
768
self .shape = target_shape [:2 ]
765
769
self .fix_h = fix_h
770
+ self .crop_top_only = crop_top_only
766
771
super ().__init__ (shape = self .shape )
767
772
768
773
def _calc_A (
@@ -780,7 +785,7 @@ def _calc_A(
780
785
box = [newx / 2 , 0 , w - newx / 2 , h ]
781
786
else :
782
787
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 ]
784
789
785
790
return box_crop_affine_transform (box , self .shape )
786
791
0 commit comments