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


MATLAB is a digital tool that is widely used in image processing, such as image transformation, noise removal from an image, detecting edges, and more. In this tutorial, I will explain how you can remove salt and pepper noise from a digital image using MATLAB.

In a digital image, the "salt and pepper noise" is a type of impulse noise that degrade the image quality and cause errors in image processing, analysis, and transmission. Therefore, it is important to remove the salt and pepper noise from the image to minimize the challenges coming to image processing and analysis.

Before going to discuss the process of removing the salt and pepper noise from an image, let us get a brief overview of salt and pepper noise.

What is Salt and Pepper Noise?

Salt and pepper noise is a type of impulse noise that degrades the quality of a digital image and is characterized by the appearance of distinct black and white pixels scattered throughout the image. This noise makes tasks like image processing and analysis challenging.

An image with salt and pepper noise is shown below.

It can be seen that the salt and pepper noise is visualized as distinct pixels of high contrast that appear distinctly from the rest of the image.

Hence, to improve quality of an image, we have to remove the salt and pepper noise so that the image can become suitable for various image processing jobs.

The most commonly used technique to remove the salt and pepper noise from an image is median filtering. This technique detects and replaces all the noisy pixels in the image with suitable pixels based on their local neighborhoods.

We can use digital tools like MATLAB to remove the salt and pepper noise from a digital image using the median filtering method.

Causes of Salt and Pepper Noise

The following are major causes behind the salt and pepper noise in an image:

  • Transmission of an image over a network can introduce salt and pepper noise.

  • When an image is stored in a noisy storage medium, it can also corrupt the pixels of the image and introduce salt and pepper noise.

  • The salt and pepper noise in an image can also occur due to errors during the process of digitization, i.e. analog to digital transformation.

  • The salt and pepper noise in an image can also be the result of malfunctioning of sensors and hardware parts of imaging devices like camera, scanner, etc.

Let us now discuss how we can use MATLAB to remove salt and pepper noise from an image.

Removing Salt and Pepper Noise from Image using MATLAB

MATLAB is a digital tool that can be used to remove salt and pepper noise from an image. To remove salt and pepper noise, the median filtering method can be used. The step−by−step process to remove the salt and pepper noise using the median filter is explained below:

Step (1) − Load the noisy image into the workspace by using the 'imread' function. Convert the input noisy image to gray scale if required.

Step (2) − Define the size of the median filter kernel based on the noise characteristics in the image.

Step (3) − Remove the noise from the image by applying the median filter. For this, MATLAB's built-in function 'medfilt2()' is used.

Step (4) − Display the denoised image.

Now, let us dive into the practical implementation of this process.

Example

The following MATLAB program demonstrates the process of removing salt and pepper noise from a digital image using a median filter.

% MATLAB code to remove salt and pepper noise from image
% Read the noisy image
noisy_img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');	% This image does not have noise

% Convert the color image to grayscale
gray_img = rgb2gray(noisy_img);

% Add salt and pepper noise to image (our image does not have)
noisy_img2 = imnoise(gray_img, 'salt & pepper');	% Skip this step as your input image has noise

% Specify the size of median filter kernel
filter_size = [5, 5]; % Adjust the size as per noise characteristics

% Apply the median filter to the noisy image
denoised_img = medfilt2(noisy_img2, filter_size);

% Display the noisy and denoised images
figure;
subplot(1, 2, 1);
imshow(noisy_img2);
title('Noisy Image');

subplot(1, 2, 2);
imshow(denoised_img);
title('Denoised Image');

Output

Code Explanation

In this MATLAB code, we start by reading the noisy image. Then, we convert it from RGB color scale to gray scale for processing. After that we define the median filter kernel size depending on the noise characteristics in the image.

Next, we apply the median filter to the image to remove the salt and pepper noise from it, for this we use the 'medfilt2' function. Finally, we display the noisy and denoised images using the 'imshow' function.

Hence, this is how we can easily remove the salt and pepper noise from an image using MATLAB.

Conclusion

In conclusion, salt and pepper noise is a type of impulse noise that can occur in a digital image due to factors like transmission errors, digitization errors, hardware malfunctioning, etc. It appears in the form of isolated white (salt) and black (pepper) pixels scattered throughout the image. This noise degrades the quality of a digital image and makes the image process tasks complex.

There several different methods present that can be used to remove salt and pepper noise from an image. One such technique is 'median filtering' which I have described in the above sections of this article with the help of MATLAB example. You can try the above given code with your own noisy image and adjusted filter size 'filter_size' depending on the noise characteristics of your image.

Updated on: 10-Oct-2023

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements