diff --git a/albumentations/augmentations/functional.py b/albumentations/augmentations/functional.py index 9a221f599..ce30b9676 100644 --- a/albumentations/augmentations/functional.py +++ b/albumentations/augmentations/functional.py @@ -1512,10 +1512,8 @@ def fancy_pca(img, alpha=0.1): return orig_img -@clipped def glass_blur(img, sigma, max_delta, iterations, dxy, mode): - coef = MAX_VALUES_BY_DTYPE[img.dtype] - x = np.uint8(cv2.GaussianBlur(np.array(img) / coef, sigmaX=sigma, ksize=(0, 0)) * coef) + x = cv2.GaussianBlur(np.array(img), sigmaX=sigma, ksize=(0, 0)) if mode == "fast": @@ -1542,7 +1540,7 @@ def glass_blur(img, sigma, max_delta, iterations, dxy, mode): dx = dxy[ind, i, 1] x[h, w], x[h + dy, w + dx] = x[h + dy, w + dx], x[h, w] - return np.clip(cv2.GaussianBlur(x / coef, sigmaX=sigma, ksize=(0, 0)), 0, 1) * coef + return cv2.GaussianBlur(x, sigmaX=sigma, ksize=(0, 0)) def _adjust_brightness_torchvision_uint8(img, factor): diff --git a/tests/test_transforms.py b/tests/test_transforms.py index 4dcc4b9f9..6c26bf1c7 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -827,3 +827,27 @@ def test_shift_scale_separate_shift_x_shift_y(image, mask): ) assert np.array_equal(data["image"], expected_image) assert np.array_equal(data["mask"], expected_mask) + + +@pytest.mark.parametrize(["val_uint8"], [[0], [1], [128], [255]]) +def test_glass_blur_float_uint8_diff_less_than_two(val_uint8): + + x_uint8 = np.zeros((5, 5)).astype(np.uint8) + x_uint8[2, 2] = val_uint8 + + x_float32 = np.zeros((5, 5)).astype(np.float32) + x_float32[2, 2] = val_uint8 / 255.0 + + glassblur = A.GlassBlur(always_apply=True, max_delta=1) + + np.random.seed(0) + blur_uint8 = glassblur(image=x_uint8)["image"] + + np.random.seed(0) + blur_float32 = glassblur(image=x_float32)["image"] + + # Before comparison, rescale the blur_float32 to [0, 255] + diff = np.abs(blur_uint8 - blur_float32 * 255) + + # The difference between the results of float32 and uint8 will be at most 2. + assert np.all(diff <= 2.0)