Skip to content

Commit

Permalink
Refactor get center (#1753)
Browse files Browse the repository at this point in the history
* Refactored center of the image

* Cleanup
  • Loading branch information
ternaus authored May 28, 2024
1 parent 4cd9d90 commit 0af4dee
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ repos:
types: [python]
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.4.5
rev: v0.4.6
hooks:
# Run the linter.
- id: ruff
Expand All @@ -66,7 +66,7 @@ repos:
- id: codespell
additional_dependencies: ["tomli"]
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.40.0
rev: v0.41.0
hooks:
- id: markdownlint
- repo: https://github.com/tox-dev/pyproject-fmt
Expand Down
9 changes: 6 additions & 3 deletions albumentations/augmentations/domain_adaptation_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from typing_extensions import Protocol

from albumentations.augmentations.functional import center
from albumentations.core.types import MONO_CHANNEL_DIMENSIONS

__all__ = [
Expand Down Expand Up @@ -100,9 +101,11 @@ def adapt_pixel_distribution(
def low_freq_mutate(amp_src: np.ndarray, amp_trg: np.ndarray, beta: float) -> np.ndarray:
height, width = amp_src.shape[:2]
border = int(np.floor(min(height, width) * beta))
center_y, center_h = height // 2, width // 2
h1, h2 = max(0, center_y - border), min(center_y + border, height - 1)
w1, w2 = max(0, center_h - border), min(center_h + border, width - 1)

center_x, center_y = center(width, height)

h1, h2 = max(0, int(center_y - border)), min(int(center_y + border), height)
w1, w2 = max(0, int(center_x - border)), min(int(center_x + border), width)
amp_src[h1:h2, w1:w2] = amp_trg[h1:h2, w1:w2]
return amp_src

Expand Down
14 changes: 14 additions & 0 deletions albumentations/augmentations/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MONO_CHANNEL_DIMENSIONS,
ColorType,
ImageMode,
NumericType,
ScalarType,
SpatterMode,
)
Expand Down Expand Up @@ -1625,3 +1626,16 @@ def morphology(img: np.ndarray, kernel: np.ndarray, operation: str) -> np.ndarra
return erode(img, kernel)

raise ValueError(f"Unsupported operation: {operation}")


def center(width: NumericType, height: NumericType) -> Tuple[float, float]:
"""Calculate the center coordinates of a rectangle.
Args:
width (NumericType): The width of the rectangle.
height (NumericType): The height of the rectangle.
Returns:
Tuple[float, float]: The center coordinates of the rectangle.
"""
return width / 2 - 0.5, height / 2 - 0.5
11 changes: 6 additions & 5 deletions albumentations/augmentations/geometric/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from scipy.ndimage import gaussian_filter

from albumentations import random_utils
from albumentations.augmentations.functional import center
from albumentations.augmentations.utils import angle_2pi_range
from albumentations.core.bbox_utils import denormalize_bbox, normalize_bbox
from albumentations.core.types import (
Expand Down Expand Up @@ -245,9 +246,9 @@ def rotate(
value: Optional[ColorType] = None,
) -> np.ndarray:
height, width = img.shape[:2]
# for images we use additional shifts of (0.5, 0.5) as otherwise
# we get an ugly black border for 90deg rotations
matrix = cv2.getRotationMatrix2D((width / 2 - 0.5, height / 2 - 0.5), angle, 1.0)

image_center = center(width, height)
matrix = cv2.getRotationMatrix2D(image_center, angle, 1.0)

warp_fn = maybe_process_in_chunks(
cv2.warpAffine,
Expand Down Expand Up @@ -325,8 +326,8 @@ def keypoint_rotate(
Note:
The rotation is performed around the center of the image.
"""
center = (cols - 1) * 0.5, (rows - 1) * 0.5
matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
image_center = center(cols, rows)
matrix = cv2.getRotationMatrix2D(image_center, angle, 1.0)
x, y, a, s = keypoint[:4]
x, y = cv2.transform(np.array([[[x, y]]]), matrix).squeeze()
return x, y, a + math.radians(angle), s
Expand Down
3 changes: 2 additions & 1 deletion albumentations/augmentations/geometric/rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing_extensions import Literal

from albumentations.augmentations.crops import functional as fcrops
from albumentations.augmentations.functional import center
from albumentations.core.pydantic import BorderModeType, InterpolationType, SymmetricRangeType
from albumentations.core.transforms_interface import BaseTransformInitSchema, DualTransform
from albumentations.core.types import (
Expand Down Expand Up @@ -334,7 +335,7 @@ def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, A
height, width = image.shape[:2]

# https://stackoverflow.com/questions/43892506/opencv-python-rotate-image-without-cropping-sides
image_center = (width / 2, height / 2)
image_center = center(width, height)

# Rotation Matrix
rotation_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
Expand Down
7 changes: 2 additions & 5 deletions albumentations/augmentations/geometric/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing_extensions import Annotated, Self

from albumentations import random_utils
from albumentations.augmentations.functional import bbox_from_mask
from albumentations.augmentations.functional import bbox_from_mask, center
from albumentations.augmentations.utils import check_range
from albumentations.core.bbox_utils import denormalize_bbox, normalize_bbox
from albumentations.core.pydantic import (
Expand Down Expand Up @@ -782,10 +782,7 @@ def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, A
# Look to issue https://github.com/albumentations-team/albumentations/issues/1079
rotate = -random.uniform(*self.rotate)

# for images we use additional shifts of (0.5, 0.5) as otherwise
# we get an ugly black border for 90deg rotations
shift_x = width / 2 - 0.5
shift_y = height / 2 - 0.5
shift_x, shift_y = center(width, height)

matrix_to_topleft = skimage.transform.SimilarityTransform(translation=[-shift_x, -shift_y])
matrix_shear_y_rot = skimage.transform.AffineTransform(rotation=-np.pi / 2)
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pytest>=8.2.0
pytest_cov>=4.1.0
pytest_mock>=3.14.0
requests>=2.31.0
ruff>=0.4.5
ruff>=0.4.6
tomli>=2.0.1
types-pkg-resources
types-PyYAML
Expand Down
20 changes: 4 additions & 16 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,16 +993,10 @@ def test_affine_incorrect_scale_range(params):
},
{
"bboxes": [
[15.65896994771262, 0.2946228229078849, 21.047137067150473, 4.617219579173327, 0],
[194.29851584295034, 25.564320319214918, 199.68668296238818, 29.88691707548036, 0],
[178.9528629328495, 95.38278042082668, 184.34103005228735, 99.70537717709212, 0],
[0.47485022613917677, 70.11308292451965, 5.701484157049652, 73.70074852182076, 0],
[(16.036253471129026, 0.7268824985344293, 21.42442059056688, 5.049479254799872, 0), (194.61183288056216, 25.996579994841458, 200.0, 30.319176751106898, 0), (179.33014645626594, 95.67740324373456, 184.71831357570377, 100.0, 0), (0.8521337495555823, 70.54534260014618, 6.078767680466058, 74.1330081974473, 0)]
],
"keypoints": [
[16.466635890349504, 0.2946228229078849, 147.04220486917677, 0.0],
[198.770582727028, 26.08267308836993, 157.04220486917674, 9.30232558139535],
[182.77879706281766, 98.84085782583904, 167.04220486917674, 18.6046511627907],
[0.4748502261391767, 73.05280756037699, 177.04220486917674, 27.90697674418604],
[(16.84391941376591, 0.7268824985344293, 147.04220486917677, 0.0), (199.0, 26.514932763996473, 157.04220486917674, 9.30232558139535), (183.15608058623408, 99.0, 167.04220486917674, 18.6046511627907), (0.8521337495555823, 73.48506723600353, 177.04220486917674, 27.906976744186046)]
],
},
],
Expand All @@ -1024,16 +1018,10 @@ def test_affine_incorrect_scale_range(params):
},
{
"bboxes": [
[0.3133170376117963, 25.564320319214918, 5.701484157049649, 29.88691707548036, 0],
[178.9528629328495, 0.2946228229078862, 184.34103005228735, 4.617219579173327, 0],
[194.29851584295034, 70.11308292451965, 199.68668296238818, 74.43567968078509, 0],
[15.658969947712617, 95.38278042082668, 20.88560387862309, 98.97044601812779, 0],
[(0.8521337495555819, 25.866991802552704, 6.240300868993435, 30.18958855881814, 0), (179.4916796447933, 0.5972943062456757, 184.87984676423113, 4.919891062511116, 0), (194.61183288056216, 70.41575440785743, 200.0, 74.73835116412288, 0), (16.1977866596564, 95.68545190416447, 21.424420590566875, 99.27311750146558, 0)]
],
"keypoints": [
[0.3133170376117963, 26.212261280658684, 212.95779513082323, 0.0],
[182.6172638742903, 0.42421101519664006, 222.95779513082323, 9.30232558139535],
[198.60904953850064, 73.18239575266574, 232.9577951308232, 18.6046511627907],
[16.305102701822126, 98.97044601812779, 242.9577951308232, 27.906976744186046],
[(0.852133749555582, 26.514932763996473, 212.95779513082323, 0.0), (183.15608058623408, 0.7268824985344295, 222.95779513082323, 9.30232558139535), (199.0, 73.48506723600353, 232.9577951308232, 18.6046511627907), (16.84391941376591, 99.0, 242.9577951308232, 27.906976744186046)]
],
},
],
Expand Down

0 comments on commit 0af4dee

Please sign in to comment.