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),
             ImageLockMode.ReadOnly,
             PixelFormat.Format24bppRgb);
         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);
         image.UnlockBits(image_data);

         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);
                 }
                 else
                 {
                     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),
             ImageLockMode.WriteOnly,
             PixelFormat.Format24bppRgb);
         Marshal.Copy(result, 0, result_data.Scan0, bytes);
         result_image.UnlockBits(result_data);

         return result_image;
     }

Conclusion

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

Morphological Processes

How To Make Extraction Of Connected Components In C#

Extraction of connected components in image processing is a morphological process, where we isolate object to a separate image.

Posted on by Andraz Krzisnik
C# Tutorial

C# Tutorial: How To Create Image Zero Padding

Zero padding an image is useful when we’re convolving it with a filter of certain size. How much padding should we use depends on how big our filter is. What is the purpose...

Posted on by Andraz Krzisnik