Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanklut committed Dec 13, 2023
1 parent dc2e462 commit 60b056a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions datasets/augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,18 @@ def get_transform(self, image):


class RandomOrientation(T.Augmentation):
"""
Apply a random orientation to the image
"""

def __init__(self, orientation_percentages: Optional[list[float | int]] = None) -> None:
"""
Initialize the Augmentations class.
Args:
orientation_percentages (Optional[list[float | int]]): A list of orientation percentages.
If None, default values of [1.0, 1.0, 1.0, 1.0] will be used.
"""
super().__init__()
if orientation_percentages is None:
orientation_percentages = [1.0] * 4
Expand Down
36 changes: 36 additions & 0 deletions datasets/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,34 @@ def inverse(self) -> T.Transform:


class OrientationTransform(T.Transform):
"""
Transform that applies 90 degrees rotation to an image and its corresponding coordinates.
"""

def __init__(self, times_90_degrees: int, height: int, width: int) -> None:
"""
Transform that applies 90 degrees rotation to an image and its corresponding coordinates.
Args:
times_90_degrees (int): Number of 90-degree rotations to apply. Should be between 0 and 3.
height (int): Height of the image.
width (int): Width of the image.
"""
super().__init__()
self.times_90_degrees = times_90_degrees % 4
self.height = height
self.width = width

def apply_image(self, img: np.ndarray) -> np.ndarray:
"""
Apply orientation change to the image.
Args:
img (np.ndarray): Input image.
Returns:
np.ndarray: Rotated image.
"""
if self.times_90_degrees == 0:
return img
elif self.times_90_degrees == 1:
Expand All @@ -592,6 +613,15 @@ def apply_image(self, img: np.ndarray) -> np.ndarray:
raise ValueError("Times 90 degrees should be between 0 and 3")

def apply_coords(self, coords: np.ndarray) -> np.ndarray:
"""
Apply orientation change to the coordinates.
Args:
coords (np.ndarray): Input coordinates.
Returns:
np.ndarray: Rotated coordinates.
"""
if self.times_90_degrees == 0:
return coords
elif self.times_90_degrees == 1:
Expand All @@ -610,6 +640,12 @@ def apply_coords(self, coords: np.ndarray) -> np.ndarray:
raise ValueError("Times 90 degrees should be between 0 and 3")

def inverse(self) -> Transform:
"""
Compute the inverse of the transformation.
Returns:
Transform: Inverse transformation.
"""
if self.times_90_degrees % 2 == 0:
height, width = self.height, self.width
else:
Expand Down

0 comments on commit 60b056a

Please sign in to comment.