Search code examples
pythonnumpynoise

How to quantify the amount of noise you add to an image


I am looking at greyscale data that exists in a 3D volume that can be imported as a 3D numpy array with values from -1 to 1. The data was taken on an imaging system and depicts a 3D volume with higher values and background noise as random values.

To test an alignment program I am currently trying to add noise of different levels to this numpy array. My current method for this is as follows:

def RandomNoise():
    """Function to make a numpy array of 100x100x100 of random noise"""
    NoiseArray = np.random.normal(-0.5,0.5,size=(100,100,100))

    return NoiseArray

I then just alter the values -0.5 or 0.5 to change the amount of noise I create.

I then add the noise by doing:

Noise = RandomNoise()

Volumewithnoise = (np.clip((Volume + Noise) * (1 - Volume), -1, 1))

Whilst this does indeed make my image noisy I don't really know how to quantify in any way the amount of noise I am adding relative to the initial image. Does anyone know of a better way to do this?


Solution

  • There is an error in your function , in fact random.normal is a gaussian noise function and not an uniform distribution, So [-0.5, 0.5] doesn't mean the noise is between 0.5 and -0.5

    You should make your function like this

    def RandomNoise( magnitude ):
        """Function to make a numpy array of 100x100x100 of random noise"""
        NoiseArray = np.random.normal(0, magnitude ,size=(100,100,100))
    
        return NoiseArray
    

    As you can read in the docs the random normal has a mean and a standard deviation. If you want to simulate noise, the mean should be zero and the standard deviation is equivalent to the magnitude of the noise.

    So this way you have a single parameter to control the amount of noise