Remove Salt and Pepper noise with Median Filtering

Prabhavi Jayanetti
Analytics Vidhya
Published in
8 min readOct 16, 2020

Let’s see how can we remove salt and pepper noise from the image by using Median filter…

What is Noise in the image ?

Any real world sensor is affected by a certain degree of noise, whether it is thermal, electrical or otherwise.

According to the wikipedia noise can be produced by the image sensor and circuitry of a scanner or digital camera. Image noise can also originate in film grain and in the unavoidable shot noise of an ideal photon detector. Image noise is an undesirable by-product of image capture that obscures the desired information. The original meaning of “noise” was “unwanted signal”; unwanted electrical fluctuations in signals received by AM radios caused audible acoustic noise (“static”). By analogy, unwanted electrical fluctuations are also called noise. Image noise can range from almost imperceptible specks on a digital photograph taken in good light, to optical and radio astronomical images that are almost entirely noise, from which a small amount of information can be derived by sophisticated processing. Such a noise level would be unacceptable in a photograph since it would be impossible even to determine the subject. There are different types of noises available.

For an example,

  1. Gaussian Noise

2. Salt and Pepper Noise

In this article we mainly focus salt and pepper noise only and how to remove salt and pepper noise by using median filtering.

What is Salt and Pepper noise ?

Salt and pepper noise is an impulse type of noise in images. We consider salt-and-pepper noise, for which a certain amount of the pixels in the image are either black or white (black or white dots). Normally if there is black dots in the image we called it pepper noise and if there is white dots in the image we called it salt noise. This noise is generally caused by errors in data transmission, failure in memory cell or analog-to-digital converter errors.

Salt Noise Image
Pepper Noise Image
Both Salt and Pepper Noise Image

If we consider 8-bit image, salt and pepper noise randomly occur certain amount of pixels into two extremes, either 0 or 255.The noise significantly damages the image information which leads to difficulties in succeeding image processing tasks such as edge detection or image segmentation and image recognition tasks. Because the noise pixel differs from most of its local neighbors, it has large gradient value the same as the edge pixel [1].

What are the filtering techniques to remove noises in the image?

There are so many filters are available to reduce the salt and pepper noise in images. Some of the filters we can use only remove salt noise or pepper noise.

  1. Max filter
  • This filter is useful for finding the brightest points in an image. Since pepper noise has very low values, it is reduced by this filter as a result of the max selection processing the sub image area Axy.
  • We can remove pepper noise by using max filter.

2. Min filter

  • This filter is useful for finding the darkest points in an image. Also, it reduces salt noise as a result of the min operation.
  • We can remove salt noise by using min filter.

3. Median filter

  • Process is replaces the value of a pixel by the median of the gray levels in region Axy of that pixel.
  • We can remove salt and pepper noise by using median filter. In this article we are focus about how to remove salt and pepper noise by using median filter technique.

4. Mid Point filter

  • The midpoint filter simply computes the midpoint between the maximum and minimum values in the area encompassed by the filter,
  • This filter works best for randomly distributed noise, like Gaussian or uniform noise.

5. Alpha- Trimmed mean filter

  • Suppose that we delete the d/2 lowest and the d/2 highest gray-level values of r(i,j) in the area Axy.
  • Let rk(i,j) represent the remaining mn-d pixels. And averaging these remain pixels is denoted as:
  • Where the value of d can range from 0 to mn-1. When d=0, It is arithmetic mean filter and d=(mn-1)/2 is a median filter. It is useful for the multiple types of noise such as the combination of salt-and-pepper and Gaussian noise.

Mean Filters

  • These are simple methods to reduce noise in spatial domain.
  1. Arithmetic mean filter
  • Compute the average value of the corrupted image r(i,j) in the area defined by Ax,y.
  • The value of the restored image at any point (x,y),
  • We can remove Gaussian noise by using this filter.

2. Geometric mean filter

  • Using a geometric mean filter is given by the expression,
  • We can remove Gaussian noise by using this filter.

3. Harmonic mean filter

  • The harmonic mean filter operation is given by the expression,
  • We can remove only salt noise and it will fail to remove pepper noise in the image.

4. Contra harmonic mean filter

  • The contra harmonic mean filter operation is given by the expression,
  • Where Q is called the order of the filter. This filter is well suited for reducing or virtually eliminating the effects of salt-and-pepper noise.
  • Q value — (+) →If (Q>0), best for remove pepper noise and this is similar to Arithmetic mean filter.
  • Q value — (-) → If (Q<0), best for remove salt noise and this is similar to Harmonic mean filter.

Median Filter

  • In this article we will see how to remove salt and pepper noise by using Median filter.
  • The median m of a set of values is such that half of the values are greater than m and half are less than m.
  • To implement, sort the pixel values in the neighborhood, choose the median and assign this value to the pixel of interest. Forces pixels with distinct intensities to be more like their neighbors.
  • We know filters are used to reduce the amount of noise present in an image. Let’s see how does Median filtering work?
3 x 3 Matrix
  • Median filtering is excellent at reducing Salt and Pepper noise. The filtering algorithm will scan the entire image, using a small matrix, and recalculate the value by sorting the set of pixels and take the center pixel values inside the matrix.
  • With the example above, the sorted values are,
  • Median of this set is 34.
  • Example for Median filtering,

Remove Salt and Pepper noise in the original image

  • To do that I have designed a small windows form.
Sample windows form
  • Here when we click the Load Image button, it will load the original images. I used 2 noised images to show the outputs clearly.
Load button
  • This code will load the images into the picture boxes. Before that we have to create function in preprocessing.cs file. That function is LoadOrginalImage(string fname). So we have to called this function in the load button.
  • By using Cv.LoadImage() we will load the original image. Then we use Cv.SaveImage ()and save the copy of the original image. That copy we will load to the picture boxes.
  • After that when we click the Salt and pepper noise button, it will remove the salt and pepper noise and load the clear image.
Removed salt and pepper noise
  • Here also we create SaltPepperFilter() function in the preprocessing.cs file. Here we used Cv.Smooth() to remove the noise. After that here also we have to save the output image by using Cv.SaveImage() in openCV. Here we Smoothing the images by using Cv.Smooth().
  • In Salt and Pepper noise button we have to called that SaltPepperFilter() function. That saved images we will load into the picture boxes by clicking the Salt and Pepper Noise button. Below code will show the how can we load the images into the picture boxes.

Conclusion

In this article I have covered salt and pepper noise reduction by using median filtering. Median Filtering is very effective to remove salt and pepper noise, and preserving edges in an image after filtering out noise. In here the implementation of median filtering is very straightforward. Load the image, pass it through Cv.Smooth() and we can get the clear image.

Therefore, in the future, I hope to discuss more Image Processing techniques with you.

Thank you for going through this article and feel free to leave a few claps if you found this helpful.

References

[1]. “Removal of Salt and Pepper Noise in Corrupted Image Based on Multilevel Weighted Graphs and IGOWA Operator”,Hindawi, 15th May 2018, [online]: https://www.hindawi.com/journals/mpe/2018/7975248/, [Accessed: 15-October-2020]

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Prabhavi Jayanetti
Prabhavi Jayanetti

Written by Prabhavi Jayanetti

SAP Consultant - ABAP | Academic Instructor | Sri Lanka Institute of Information Technology. Visit me @ https://www.linkedin.com/in/prabhavi-jayanetti-5b900418a

No responses yet