Open In App

How to Remove Salt and Pepper Noise from Image Using MATLAB?

Improve
Improve
Like Article
Like
Save
Share
Report

Impulse noise is a unique form of noise that can have many different origins. Images are frequently corrupted through impulse noise due to transmission errors, defective memory places, or timing mistakes in analog-to-digital conversion. Salt-and-pepper noise is one form of impulse noise that can corrupt the image, in which the noisy pixels can take only the most and minimal values withinside the dynamic range. Since linear filtering strategies are not powerful in removing impulse noise, non-linear filtering strategies are widely used in the recovery method. The standard median filter (SMF) is one of the most popular non-linear filters used to remove salt-and-pepper noise due to its good denoising power and computational efficiency. 

However, the major drawback of the SMF is that the filter is effective only for low noise densities, and additionally, exhibits blurring if the window size is large and leads to insufficient noise suppression if the window size is small. When the noise level is over 50%, the edge details of the original image will not be preserved by the median filter. Nevertheless, it is important that during the process the edge details have to be preserved without losing the high-frequency components of the image edges.

The ideal approach is to apply the filtering technique only to noisy pixels, without changing the uncorrupted pixel values. Non-linear filters such as Adaptive Median Filter (AMF), decision–based or switching median filters can be used for discriminating corrupted and uncorrupted pixels, and then apply the filtering technique. Noisy pixels will be replaced by the median value and uncorrupted pixels will be left unchanged. AMF performs well at low noise densities since the corrupted pixels which are replaced by the median values are very few. At higher noise densities, window size has to be increased to get better noise removal which will lead to less correlation between corrupted pixel values and replaced median pixel values.

What is Salt and Pepper noise?

As the name suggests, the color of pixels either becomes black or white completely. White is called salt and black is called pepper, thus the name comes for it.

This is a result of the random creation of pure white or black (high/low) pixels into the image. This is much less common in cutting-edge image sensors, even though can maximum usually be visible withinside the form of camera sensor faults (hot pixels which might be usually at most depth, or dead pixels which can be usually black). This kind of noise is also called impulse noise.

This noise is removed effectively by the median filter. White or black pixels behave as outliers. A neighborhood window of size [m n] is selected and slides over the image. The pixels of images hovered by the window are sorted in increasing order then white and dark pixels go on the right and left side of the sorted list respectively thus does not affect the selection of output pixel. We select the median pixel from the sorted list thus salt and pepper get reduced 100% effectively though some blurriness occurs which is fine.

Functions Used:

  • imread( ) inbuilt function is used to read the image.
  • imtool( ) inbuilt function is used to display the image.
  • size( ) inbuilt function is used to get size of the image.
  • rgb2gray( ) inbuilt function is used to convert RGB into grayscale image.
  • imnoise( ) inbuilt function is used to add noise to the image.
  • medfilt2( ) inbuilt function is used to perform median filter on noisy image.
  • pause( ) inbuilt function is used to stop execution for specified seconds.

Example:

Matlab




% MATLAB Code for removal of Salt and 
% Pepper noise from image.
k=imread("gfglogo.png");
RemoveSaltAndPepperNoise(k);
  
function RemoveSaltAndPepperNoise(k)
% Convert to grayscale if not.
[M,N,D]=size(k);
if(D==3)
    k=rgb2gray(k);
end
  
% Add noise to image.
kn=imnoise(k,'salt & pepper',0.03);
  
% Display original and noisy image.
imtool(k,[]);
imtool(kn,[]);
  
% Denoising.
dn=medfilt2(kn,[5,5]);
  
% Display denoised image.
imtool(dn,[]);
  
% Pause and close img windows.
pause(10);
imtool close all;
end


Output: 

Figure 1: Original image

Figure 2: Noised image

Figure 3: Denoised image

Figure 4: Original image

Figure 5: Noised image

Figure 6: Denoised image

Code Explanation:

  • [M,N,D]=size(k); This line gets the size of input image.
  • kn=imnoise(k,’salt & pepper’,0.03); This line adds 3% noise in the image.
  • dn=medfilt2(kn,[5,5]); This line applies the median filter on noised image.
  • k=imread(“cameraman.png”); This line reads the input image.
  • RemoveSaltAndPepperNoise(k); This line calls the user-defined function by passing input image to it.

A median filter is the best for the removal of Salt and Pepper noise, though it introduces some blurriness in the image in that area where the non-homogenous region is captured below the sliding neighborhood window. In that case, the median of the sorted list does not exactly come out of the actual underlying pixel value. This method reduces the noise by 100%.



Last Updated : 22 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads