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

Implementation of Affine transform #885

Merged
merged 10 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Spatial-level transforms will simultaneously change both an input image as well

| Transform | Image | Masks | BBoxes | Keypoints |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---: | :---: | :----: | :-------: |
| [Affine](https://albumentations.ai/docs/api_reference/augmentations/geometric/transforms/#albumentations.augmentations.geometric.transforms.Affine) | ✓ | ✓ | ✓ | ✓ |
| [CenterCrop](https://albumentations.ai/docs/api_reference/augmentations/crops/transforms/#albumentations.augmentations.crops.transforms.CenterCrop) | ✓ | ✓ | ✓ | ✓ |
| [CoarseDropout](https://albumentations.ai/docs/api_reference/augmentations/transforms/#albumentations.augmentations.transforms.CoarseDropout) | ✓ | ✓ | | |
| [Crop](https://albumentations.ai/docs/api_reference/augmentations/crops/transforms/#albumentations.augmentations.crops.transforms.Crop) | ✓ | ✓ | ✓ | ✓ |
Expand Down
80 changes: 79 additions & 1 deletion albumentations/augmentations/geometric/functional.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import cv2
import math
import numpy as np
import skimage.transform

from scipy.ndimage.filters import gaussian_filter

from ..bbox_utils import denormalize_bbox, normalize_bbox
from ..functional import angle_2pi_range, preserve_channel_dim, _maybe_process_in_chunks, preserve_shape
from ..functional import (
angle_2pi_range,
preserve_channel_dim,
_maybe_process_in_chunks,
preserve_shape,
MAX_VALUES_BY_DTYPE,
)

from typing import Union, List, Sequence

Expand Down Expand Up @@ -410,3 +417,74 @@ def perspective_keypoint(
return keypoint_scale((x, y, angle, scale), scale_x, scale_y)

return x, y, angle, scale


def _is_identity_matrix(matrix: skimage.transform.ProjectiveTransform, eps: float = 1e-4) -> bool:
identity = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.float32)
Dipet marked this conversation as resolved.
Show resolved Hide resolved
return np.average(np.abs(identity - matrix.params)) <= eps


@preserve_channel_dim
def warp_affine(
image: np.ndarray,
matrix: skimage.transform.ProjectiveTransform,
interpolation: int,
cval: Union[int, float, Sequence[int], Sequence[float]],
mode: int,
output_shape: Sequence[int],
) -> np.ndarray:
if _is_identity_matrix(matrix):
return image

dsize = int(np.round(output_shape[1])), int(np.round(output_shape[0]))
warp_fn = _maybe_process_in_chunks(
cv2.warpAffine, M=matrix.params[:2], dsize=dsize, flags=interpolation, borderMode=mode, borderValue=cval
)
tmp = warp_fn(image)
return tmp


@angle_2pi_range
def keypoint_affine(
keypoint: Sequence[float],
matrix: skimage.transform.ProjectiveTransform,
scale: dict,
) -> Sequence[float]:
if _is_identity_matrix(matrix):
return keypoint

x, y, a, s = keypoint[:4]
x, y = skimage.transform.matrix_transform(np.array([[x, y]]), matrix.params).ravel()
a += rotation2DMatrixToEulerAngles(matrix.params[:2])
s *= np.max([scale["x"], scale["y"]])
return x, y, a, s


def bbox_affine(
bbox: Sequence[float],
matrix: skimage.transform.ProjectiveTransform,
rows: int,
cols: int,
output_shape: Sequence[int],
) -> Sequence[float]:
if _is_identity_matrix(matrix):
return bbox

x_min, y_min, x_max, y_max = denormalize_bbox(bbox, rows, cols)
points = np.array(
[
[x_min, y_min],
[x_max, y_min],
[x_max, y_max],
[x_min, y_max],
]
)
points = skimage.transform.matrix_transform(points, matrix.params)
points[:, 0] = np.clip(points[:, 0], 0, output_shape[1])
points[:, 1] = np.clip(points[:, 1], 0, output_shape[0])
x_min = np.min(points[:, 0])
x_max = np.max(points[:, 0])
y_min = np.min(points[:, 1])
y_max = np.max(points[:, 1])

return normalize_bbox((x_min, y_min, x_max, y_max), output_shape[0], output_shape[1])
Loading