Skip to content

Commit

Permalink
Issue #1263 Support for various dtypes in InvertImg. (#1277)
Browse files Browse the repository at this point in the history
* Issue #1263 Added support for float p in InvertImg and adapted test func accordingly

* Support for various dtypes in invert(). And new test function for it

* Resolved requested changes

Co-authored-by: Mikhail Druzhinin <dipetm@gmail.com>
  • Loading branch information
dmitrie-ai and Dipet committed Sep 9, 2022
1 parent 580c1cf commit e0c1ee1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 4 additions & 2 deletions albumentations/augmentations/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,8 +795,10 @@ def add_shadow(img, vertices_list):
return image_rgb


def invert(img):
return 255 - img
def invert(img: np.ndarray) -> np.ndarray:
# Supports all the valid dtypes
# clips the img to avoid unexpected behaviour.
return MAX_VALUES_BY_DTYPE[img.dtype] - img


def channel_shuffle(img, channels_shuffled):
Expand Down
2 changes: 1 addition & 1 deletion albumentations/augmentations/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ class InvertImg(ImageOnlyTransform):
image
Image types:
uint8
uint8, float32
"""

def apply(self, img, **params):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,17 @@ def test_additional_targets_for_image_only(augmentation_cls, params):
assert np.array_equal(aug1, aug2)


def test_image_invert():
for _ in range(10):
# test for np.uint8 dtype
image1 = np.random.randint(low=0, high=256, size=(100, 100, 3), dtype=np.uint8)
image2 = A.to_float(image1)
r_int = F.invert(F.invert(image1))
r_float = F.invert(F.invert(image2))
r_to_float = A.to_float(r_int)
assert np.allclose(r_float, r_to_float, atol=0.01)


def test_lambda_transform():
def negate_image(image, **kwargs):
return -image
Expand Down

0 comments on commit e0c1ee1

Please sign in to comment.