Unverified Commit bcbe3e12 authored by Bharath Ramsundar's avatar Bharath Ramsundar Committed by GitHub
Browse files

Merge pull request #1516 from RishalAggarwal/master

Added functionality and unit tests for image shifting and addition of random noise (gaussian, salt and pepper) 
parents 3e97dbd2 e689f091
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -537,3 +537,39 @@ class TestTransformers(unittest.TestCase):
    scale = scipy.misc.imresize(self.d, (h, w))
    check_scale = dt.scale(h, w)
    np.allclose(scale, check_scale)

  def test_shift(self):
    # Check shift
    dt = DataTransforms(self.d)
    height = 5
    width = 5
    if len(self.d.shape) == 2:
      shift = scipy.ndimage.shift(self.d, [height, width])
    if len(self.d.shape) == 3:
      shift = scipy.ndimage.shift(self.d, [height, width, 0])
    check_shift = dt.shift(width, height)
    assert np.allclose(shift, check_shift)

  def test_gaussian_noise(self):
    # check gaussian noise
    dt = DataTransforms(self.d)
    np.random.seed(0)
    random_noise = self.d
    random_noise = random_noise + np.random.normal(
        loc=0, scale=25.5, size=self.d.shape)
    np.random.seed(0)
    check_random_noise = dt.gaussian_noise(mean=0, std=25.5)
    assert np.allclose(random_noise, check_random_noise)

  def test_salt_pepper_noise(self):
    # check salt and pepper noise
    dt = DataTransforms(self.d)
    np.random.seed(0)
    prob = 0.05
    random_noise = self.d
    noise = np.random.random(size=self.d.shape)
    random_noise[noise < (prob / 2)] = 0
    random_noise[noise > (1 - prob / 2)] = 255
    np.random.seed(0)
    check_random_noise = dt.salt_pepper_noise(prob, salt=255, pepper=0)
    assert np.allclose(random_noise, check_random_noise)
+41 −0
Original line number Diff line number Diff line
@@ -1225,3 +1225,44 @@ class DataTransforms(Transformer):
            sigma - std dev. of the gaussian distribution
    """
    return scipy.ndimage.gaussian_filter(self.Image, sigma)

  def shift(self, width, height, mode='constant', order=3):
    """Shifts the image
        Parameters:
          width - amount of width shift(positive values shift image right )
          height - amount of height shift(positive values shift image lower)
          mode - Points outside the boundaries of the input are filled according to the given mode
          (‘constant’, ‘nearest’, ‘reflect’ or ‘wrap’). Default is ‘constant’
          order - The order of the spline interpolation, default is 3. The order has to be in the range 0-5.
          """
    if len(self.Image.shape) == 2:
      return scipy.ndimage.shift(
          self.Image, [height, width], order=order, mode=mode)
    if len(self.Image.shape == 3):
      return scipy.ndimage.shift(
          self.Image, [height, width, 0], order=order, mode=mode)

  def gaussian_noise(self, mean=0, std=25.5):
    '''Adds gaussian noise to the image
    Parameters:
      mean - mean of gaussian.
      std - standard deviation of gaussian.
        '''

    x = self.Image
    x = x + np.random.normal(loc=mean, scale=std, size=self.Image.shape)
    return x

  def salt_pepper_noise(self, prob=0.05, salt=255, pepper=0):
    '''Adds salt and pepper noise to the image
    Parameters:
      prob - probability of the noise.
      salt - value of salt noise.
      pepper - value of pepper noise.
        '''

    noise = np.random.random(size=self.Image.shape)
    x = self.Image
    x[noise < (prob / 2)] = pepper
    x[noise > (1 - prob / 2)] = salt
    return x