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.
Filter by Category
Salt and pepper noise or impulse noise is one of the noise models we can use to simulate image data corruption in real life.
Uniform noise is one of the noise models we can use to simulate real life data corruption. This guide shows how to make in on images.
Exponential noise is one of the noise models we can use to simulate corruption of data. This guide show how to use it on images.
Gamma noise is one of the noise models we use to simulate practical data corruption. This guide shows how to apply it on images.
Rayleigh noise is one of the noise models with which we can simulate data corruption. Guide to making noise from PDF and image histogram.
Gaussian noise on images is generated with Gaussian probability distribution function. It simulates noise that appears in practice.
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.
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.
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.
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;
}
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.