diff --git a/monai/transforms/transforms.py b/monai/transforms/transforms.py index 4447cdd282..7f8961b4e6 100644 --- a/monai/transforms/transforms.py +++ b/monai/transforms/transforms.py @@ -64,6 +64,24 @@ def __call__(self, img): return rescale_array(img, self.minv, self.maxv, self.dtype) +@export +class GaussianNoise(Randomizable): + """Add gaussian noise to image. + + Args: + mean (float or array of floats): Mean or “centre” of the distribution. + scale (float): Standard deviation (spread) of distribution. + size (int or tuple of ints): Output shape. Default: None (single value is returned). + """ + + def __init__(self, mean=0.0, std=0.1): + self.mean = mean + self.std = std + + def __call__(self, img): + return img + self.R.normal(self.mean, self.R.uniform(0, self.std), size=img.shape) + + @export class Flip: """Reverses the order of elements along the given axis. Preserves shape. diff --git a/tests/test_gaussian_noise.py b/tests/test_gaussian_noise.py new file mode 100644 index 0000000000..400ce4ad73 --- /dev/null +++ b/tests/test_gaussian_noise.py @@ -0,0 +1,38 @@ +# Copyright 2020 MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest +import numpy as np + +from parameterized import parameterized + +from monai.transforms import GaussianNoise +from tests.utils import NumpyImageTestCase2D + + +class GaussianNoiseTest(NumpyImageTestCase2D): + + @parameterized.expand([ + ("test_zero_mean", 0, 0.1), + ("test_non_zero_mean", 1, 0.5) + ]) + def test_correct_results(self, _, mean, std): + seed = 42 + gaussian_fn = GaussianNoise(mean=mean, std=std) + gaussian_fn.set_random_state(seed) + noised = gaussian_fn(self.imt) + np.random.seed(seed) + expected = self.imt + np.random.normal(mean, np.random.uniform(0, std), size=self.imt.shape) + assert np.allclose(expected, noised) + + +if __name__ == '__main__': + unittest.main()