How To Add Salt And Pepper Noise On Image – C# Guide

Salt and pepper noise or impulse noise is one of the noise models we can use to simulate image data corruption in real life.

Andraz Krzisnik
How To Add Salt And Pepper Noise On Image...

Salt and pepper noise is also known as impulse noise. It’s the simplest noise model we can implement on an image. However, we should use it with grayscale images only where minimum and maximum color values represent black and white.

We use noise models to simulate image data corruption so we can try out various filters for image restoration and reconstruction. In practice, noise appears in images during acquisition and transmission.

For example, if we’re using an old camera, there’s a higher chance it’s going to be more sensitive to lightning and temperature changes. In other words, noise can appear because of low grade equipment and envirnmental conditions.

Another reason for noise corruption might be wireless transmission. Lightning and other atmospheric disturbances can also have an impact on a signal, while it’s travelling to it’s destination.

Salt and pepper noise

As I mentioned before, this is one of the simplest noise models among others. Because it’s probability distribution function are just chances of each impulse appearing randomly across the image. For instance, you could set it so there would be twice as many of black pixels than white pixels.

Salt and pepper noise probability distribution function
Salt and pepper noise probability distribution function

As we can see from the formula, salt and pepper needn’t be strictly black and white. But we can also set these impulse values to custom pixel values. The following graph illustrates the distribution of each values. As you can see, there are spikes only at two intensity values.

Salt and pepper noise probability distribution graph
Salt and pepper noise probability distribution graph

C# function

Although I mentioned that we could set up custom intensities for each impulses, I’ve written this function only for black and white intensities. If you wish to implement this functionality you can easly modify it.

public static Bitmap ImpulseNoise(this Bitmap image)
         int w = image.Width;
         int h = image.Height;

         BitmapData image_data = image.LockBits(
             new Rectangle(0, 0, w, h),
         int bytes = image_data.Stride * image_data.Height;
         byte[] buffer = new byte[bytes];
         byte[] result = new byte[bytes];
         Marshal.Copy(image_data.Scan0, buffer, 0, bytes);

         Random rnd = new Random();
         int noise_chance = 10;
         for (int i = 0; i < bytes; i+=3)
             int max = (int)(1000 / noise_chance);
             int tmp = rnd.Next(max + 1);
             for (int j = 0; j < 3; j++)
                 if (tmp == 0 || tmp == max)
                     int sorp = tmp / max;
                     result[i + j] = (byte)(sorp * 255);
                     result[i + j] = buffer[i + j];

         Bitmap result_image = new Bitmap(w, h);
         BitmapData result_data = result_image.LockBits(
             new Rectangle(0, 0, w, h),
         Marshal.Copy(result, 0, result_data.Scan0, bytes);

         return result_image;


Adding salt and pepper noise demonstrates the noise that appears on images taken by really old cameras. We’re talking about those that produce grayscale photos.

This was a simple exercise in adding noise to images and I hope this post was helpful. You can also download the entire project and play with the settings yourself.

Related Articles

Point Detection

How To Make Point Detection Algorithm With C#

Point detection is a segmentation technique in image processing, we can use to get the position of point objects in the image.

Posted on by Andraz Krzisnik
C# Tutorial

C# Tutorial: How To Scale Images Without Deforming Them

Let’s Get Started A very basic problem that we face in image processing is how to scale images without making them look deformed when we want to make it of an arbitrary...

Posted on by Andraz Krzisnik