Skip to content

Commit

Permalink
Fixed fisheye
Browse files Browse the repository at this point in the history
  • Loading branch information
ternaus committed Dec 5, 2024
1 parent d7db5cb commit c527762
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
26 changes: 19 additions & 7 deletions albumentations/augmentations/geometric/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3357,21 +3357,22 @@ def tps_transform(
def get_camera_matrix_distortion_maps(
image_shape: tuple[int, int],
k: float,
center_xy: tuple[float, float],
) -> tuple[np.ndarray, np.ndarray]:
"""Generate distortion maps using camera matrix model.
Args:
image_shape: Image shape
k: Distortion coefficient
center_xy: Center of distortion
Returns:
tuple of:
- map_x: Horizontal displacement map
- map_y: Vertical displacement map
"""
height, width = image_shape[:2]
camera_matrix = np.array(
[[width, 0, 0], [0, height, 0], [0, 0, 1]],
[[width, 0, center_xy[0]], [0, height, center_xy[1]], [0, 0, 1]],
dtype=np.float32,
)
distortion = np.array([k, k, 0, 0, 0], dtype=np.float32)
Expand All @@ -3388,31 +3389,42 @@ def get_camera_matrix_distortion_maps(
def get_fisheye_distortion_maps(
image_shape: tuple[int, int],
k: float,
center_xy: tuple[float, float],
) -> tuple[np.ndarray, np.ndarray]:
"""Generate distortion maps using fisheye model.
Args:
image_shape: Image shape
k: Distortion coefficient
center_xy: Center of distortion
Returns:
tuple of:
- map_x: Horizontal displacement map
- map_y: Vertical displacement map
"""
height, width = image_shape[:2]

center_x, center_y = center_xy

Check warning on line 3407 in albumentations/augmentations/geometric/functional.py

View check run for this annotation

Codecov / codecov/patch

albumentations/augmentations/geometric/functional.py#L3407

Added line #L3407 was not covered by tests

# Create coordinate grid
y, x = np.mgrid[:height, :width].astype(np.float32)

x = x - center_x
y = y - center_y

Check warning on line 3413 in albumentations/augmentations/geometric/functional.py

View check run for this annotation

Codecov / codecov/patch

albumentations/augmentations/geometric/functional.py#L3412-L3413

Added lines #L3412 - L3413 were not covered by tests

# Calculate polar coordinates
r = np.sqrt(x * x + y * y)
theta = np.arctan2(y, x)

# Apply fisheye distortion
r_dist = r * (1 + k * r * r)
# Normalize radius by the maximum possible radius to keep distortion in check
max_radius = math.sqrt(max(center_x, width - center_x) ** 2 + max(center_y, height - center_y) ** 2)
r_norm = r / max_radius

Check warning on line 3421 in albumentations/augmentations/geometric/functional.py

View check run for this annotation

Codecov / codecov/patch

albumentations/augmentations/geometric/functional.py#L3420-L3421

Added lines #L3420 - L3421 were not covered by tests

# Apply fisheye distortion to normalized radius
r_dist = r * (1 + k * r_norm * r_norm)

Check warning on line 3424 in albumentations/augmentations/geometric/functional.py

View check run for this annotation

Codecov / codecov/patch

albumentations/augmentations/geometric/functional.py#L3424

Added line #L3424 was not covered by tests

# Convert back to cartesian coordinates
map_x = r_dist * np.cos(theta)
map_y = r_dist * np.sin(theta)
map_x = r_dist * np.cos(theta) + center_x
map_y = r_dist * np.sin(theta) + center_y

Check warning on line 3428 in albumentations/augmentations/geometric/functional.py

View check run for this annotation

Codecov / codecov/patch

albumentations/augmentations/geometric/functional.py#L3427-L3428

Added lines #L3427 - L3428 were not covered by tests

return map_x, map_y
3 changes: 3 additions & 0 deletions albumentations/augmentations/geometric/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1622,17 +1622,20 @@ def get_params_dependent_on_data(
k = self.py_random.uniform(*self.distort_limit)

# Calculate center shift
center_xy = fgeometric.center(image_shape)

# Get distortion maps based on mode
if self.mode == "camera":
map_x, map_y = fgeometric.get_camera_matrix_distortion_maps(
image_shape,
k,
center_xy,
)
else: # fisheye
map_x, map_y = fgeometric.get_fisheye_distortion_maps(
image_shape,
k,
center_xy,
)

return {"map_x": map_x, "map_y": map_y}
Expand Down

0 comments on commit c527762

Please sign in to comment.