Skip to content

Commit

Permalink
Merge pull request #3 from benedikt-karrasch/master
Browse files Browse the repository at this point in the history
adjust flip for polygon points
  • Loading branch information
benedikt-karrasch authored Jan 15, 2025
2 parents 5eb7add + effc669 commit beb0e15
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions imgaug/augmenters/flip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* :class:`Flipud`
"""

from __future__ import print_function, division, absolute_import

import numpy as np
Expand Down Expand Up @@ -674,9 +675,7 @@
"""
# pylint:enable=pointless-string-statement

_FLIPLR_DTYPES_CV2 = iadt._convert_dtype_strs_to_types(
"uint8 uint16 int8 int16"
)
_FLIPLR_DTYPES_CV2 = iadt._convert_dtype_strs_to_types("uint8 uint16 int8 int16")


def fliplr(arr):
Expand Down Expand Up @@ -742,8 +741,7 @@ def _fliplr_cv2(arr):
# TODO this is quite inefficient right now
channels = [
cv2.flip(_normalize_cv2_input_arr_(arr[..., c]), 1)
for c
in sm.xrange(arr.shape[-1])
for c in sm.xrange(arr.shape[-1])
]
result = np.stack(channels, axis=-1)
else:
Expand Down Expand Up @@ -869,30 +867,34 @@ class Fliplr(meta.Augmenter):
"""

def __init__(self, p=1,
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
def __init__(
self,
p=1,
seed=None,
name=None,
random_state="deprecated",
deterministic="deprecated",
):
super(Fliplr, self).__init__(
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)
seed=seed, name=name, random_state=random_state, deterministic=deterministic
)
self.p = iap.handle_probability_param(p, "p")

# Added in 0.4.0.
def _augment_batch_(self, batch, random_state, parents, hooks):
samples = self.p.draw_samples((batch.nb_rows,),
random_state=random_state)
samples = self.p.draw_samples((batch.nb_rows,), random_state=random_state)
for i, sample in enumerate(samples):
if sample >= 0.5:
if batch.images is not None:
batch.images[i] = fliplr(batch.images[i])

if batch.heatmaps is not None:
batch.heatmaps[i].arr_0to1 = fliplr(
batch.heatmaps[i].arr_0to1)
batch.heatmaps[i].arr_0to1 = fliplr(batch.heatmaps[i].arr_0to1)

if batch.segmentation_maps is not None:
batch.segmentation_maps[i].arr = fliplr(
batch.segmentation_maps[i].arr)
batch.segmentation_maps[i].arr
)

if batch.keypoints is not None:
kpsoi = batch.keypoints[i]
Expand All @@ -916,6 +918,7 @@ def _augment_batch_(self, batch, random_state, parents, hooks):
# TODO maybe reverse the order of points afterwards?
# the flip probably inverts them
poly.exterior[:, 0] = width - poly.exterior[:, 0]
poly.exterior = poly.exterior[[1, 0, 3, 2]]

if batch.line_strings is not None:
lsoi = batch.line_strings[i]
Expand Down Expand Up @@ -981,18 +984,22 @@ class Flipud(meta.Augmenter):
"""

def __init__(self, p=1,
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
def __init__(
self,
p=1,
seed=None,
name=None,
random_state="deprecated",
deterministic="deprecated",
):
super(Flipud, self).__init__(
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)
seed=seed, name=name, random_state=random_state, deterministic=deterministic
)
self.p = iap.handle_probability_param(p, "p")

# Added in 0.4.0.
def _augment_batch_(self, batch, random_state, parents, hooks):
samples = self.p.draw_samples((batch.nb_rows,),
random_state=random_state)
samples = self.p.draw_samples((batch.nb_rows,), random_state=random_state)
for i, sample in enumerate(samples):
if sample >= 0.5:
if batch.images is not None:
Expand All @@ -1001,12 +1008,12 @@ def _augment_batch_(self, batch, random_state, parents, hooks):
batch.images[i] = batch.images[i][::-1, ...]

if batch.heatmaps is not None:
batch.heatmaps[i].arr_0to1 = \
batch.heatmaps[i].arr_0to1[::-1, ...]
batch.heatmaps[i].arr_0to1 = batch.heatmaps[i].arr_0to1[::-1, ...]

if batch.segmentation_maps is not None:
batch.segmentation_maps[i].arr = \
batch.segmentation_maps[i].arr[::-1, ...]
batch.segmentation_maps[i].arr = batch.segmentation_maps[i].arr[
::-1, ...
]

if batch.keypoints is not None:
kpsoi = batch.keypoints[i]
Expand All @@ -1030,6 +1037,7 @@ def _augment_batch_(self, batch, random_state, parents, hooks):
# TODO maybe reverse the order of points afterwards?
# the flip probably inverts them
poly.exterior[:, 1] = height - poly.exterior[:, 1]
poly.exterior = poly.exterior[[1, 0, 3, 2]]

if batch.line_strings is not None:
lsoi = batch.line_strings[i]
Expand Down

0 comments on commit beb0e15

Please sign in to comment.