Skip to content

Commit

Permalink
Fix _brightness_contrast_adjust_uint when beta_by_max = True (#487)
Browse files Browse the repository at this point in the history
Co-authored-by: druzhinin <druzhinin@simicon.com>
Co-authored-by: Mikhail Druzhinin <dipet@gmail.com>
  • Loading branch information
3 people committed Jul 16, 2022
1 parent 2eac060 commit 0203864
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion albumentations/augmentations/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ def _brightness_contrast_adjust_uint(img, alpha=1, beta=0, beta_by_max=False):
if beta_by_max:
lut += beta * max_value
else:
lut += beta * np.mean(img)
lut += (alpha * beta) * np.mean(img)

lut = np.clip(lut, 0, max_value).astype(dtype)
img = cv2.LUT(img, lut)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,3 +986,19 @@ def test_normalize_np_cv_equal(image, mean, std):
res1 = F.normalize_cv2(image, mean, std)
res2 = F.normalize_numpy(image, mean, std)
assert np.allclose(res1, res2)


@pytest.mark.parametrize("beta_by_max", [True, False])
def test_brightness_contrast_adjust_equal(beta_by_max):
image_int = np.random.randint(0, 256, [512, 512, 3], dtype=np.uint8)
image_float = image_int.astype(np.float32) / 255

alpha = 1.3
beta = 0.14

image_int = F.brightness_contrast_adjust(image_int, alpha, beta, beta_by_max)
image_float = F.brightness_contrast_adjust(image_float, alpha, beta, beta_by_max)

image_float = (image_float * 255).astype(int)

assert np.abs(image_int.astype(int) - image_float).max() <= 1

0 comments on commit 0203864

Please sign in to comment.