What are different types of denoising filters in MATLAB?


In MATLAB, a denoising filter is a tool that can reduce or remove noise from a digital image or a digital signal. As we know, noise is an unwanted signal or disturbance in a digital signal that impacts the quality of the signal or can cause errors in the signal processing. Therefore, it is required to remove or reduce the noise in a signal or image. This can be done with the help of a denoising filter.

In this tutorial, we will explore several different types of denoising filter in MATLAB and will implement them using MATLAB programming.

Types of Denoising Filters in MATLAB

MATLAB provides various types of denoising filter to reduce or remove different types of noise and disturbances from a digital signal or image. Some commonly use denoising filters are as follows −

  • Median Filter − The median filter reduces noise by replacing each pixel in the image with the median value of nearby pixel. The median filter is quite effective in reducing or removing the salt-and-pepper noise from the image.

  • Gaussian Filter − This filter remove noise from an image by using a convolution operation. It can remove Gaussian noise from the image to smooth it.

  • Wiener Filter − This is a type of adaptive filter used to reduce the mean square error between the true signal and the estimated signal. This filter is used when noise statistics are known.

Process to Apply Denoising Filter in MATLAB

In MATLAB, we can follow the following steps to apply a denoising filter to an image to reduce or remove noise −

  • Step (1) − Read the noisy image or signal.

  • Step (2) − Convert the input image to grayscale to apply filter.

  • Step (3) − Select an appropriate denoising filter based on the noise properties.

  • Step (4) − Apply the noise filter to the input image.

  • Step (5) − Display the result.

Now, let us discuss implementation of above given three types of denoising filters in MATLAB.

(1). Median Filter in MATLAB

The median filter performs denoising of a digital signal or an image by replacing each pixel value with the median value of its adjacent pixels. This type of filter is highly effective in reducing or removing salt-and-pepper noise from the signal or image.

Syntax

This filter can be applied by using the MATLAB's built-in function 'medfilt2'. The syntax of the 'medilt2' is as follows −

filtered_img = medfilt2(noisy_image, window_size);

The following MATLAB program demonstrates the implementation of median filter to remove noise from an image.

Matlab Example (1)

% MATLAB code for implementing median filter
% Read the noisy input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

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

% Apply the median filter to the noisy image 
filtered_img = medfilt2(gray_img, [3, 3]);

% Display the noisy image and filtered image
subplot(1, 2, 1), imshow(gray_img), title('Noisy Image');
subplot(1, 2, 2), imshow(filtered_img), title('Filtered Image');

Output

Explanation

In this MATLAB code, firstly we read the noisy image using the 'imread' function and store it in a variable 'img'. Then, we convert the input RGB image to grayscale for applying filter to it, for this we use the 'rgb2gray' function. Next, we apply the median filter to the grayscale image to remove noise and store the result in a variable 'filtered_img'. Finally, we use the 'disp' function to display the input noisy image and the filtered image.

(2). Gaussian Filter in MATLAB

The Gaussian filter is another type of denoising filter used to remove Gaussian noise while smoothing a digital image. This filter uses a Gaussian kernel to apply a convolution operation to remove noise from the image.

Syntax

In MATLAB, we can use the 'imgaussfilt' function to apply the Gaussian filter to an image. This function has the following syntax −

filtered_img = imgaussfilt(noisy_img, std_deviation);

Matlab Example (2)

The following MATLAB program demonstrates how to implement Gaussian filter to remove noise from an image.

% MATLAB code for implementing Gaussian filter
% Read the noisy input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

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

% Apply the Gaussian filter to the noisy image 
filtered_img = imgaussfilt(gray_img, 1);

% Display the noisy image and filtered image
subplot(1, 2, 1), imshow(gray_img), title('Noisy Image');
subplot(1, 2, 2), imshow(filtered_img), title('Filtered Image');

Output

Explanation

In this MATLAB code, we start by reading the noisy image using the 'imread' function and store it in a variable 'img'. Then, we convert the input RGB image to grayscale for applying the filter to it. After that we apply the Gaussian filter to the grayscale image to remove Gaussian noise and store the result in a variable 'filtered_img'. Finally, we use the 'disp' function to display the noisy image and the filtered image.

(3). Wiener Filter in MATLAB

In MATLAB, the wiener filter is a type of adaptive filter used to reduce noise in a digital signal or image. This filter performs denoising of image or signal by minimizing the mean square error between the true signal and the estimated signal.

Syntax

To apply the wiener image, we use the 'wiener2' function in the image. This function has the following syntax −

filtered_img = wiener2(noisy_img, window_size, noise_power);

Here, the noise power is estimated by using the following expression,

noise_power = abs(fft2(noisy_img)).^2 / numel(noisy_img);

Matlab Example (3)

Now, let us see the implementation of wiener filter in MATLAB.

% MATLAB code for implementing wiener filter
% Read the noisy input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

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

% Estimate the noise power for the given image
noise_power = abs(fft2(gray_img)).^2 / numel(gray_img);

% Apply the wiener filter to the noisy image 
filtered_img = wiener2(gray_img, [3, 3], noise_power);

% Display the noisy image and filtered image
subplot(1, 2, 1), imshow(gray_img), title('Noisy Image');
subplot(1, 2, 2), imshow(filtered_img), title('Filtered Image');

Output

Explanation

In this MATLAB program, we start by reading the noisy image using the 'imread' function. Then, we convert the input RGB image to grayscale for applying the filter. Next, we apply the wiener filter to the grayscale image to remove the noise. Finally, we display the noisy image and the filtered image using the 'disp' function.

Conclusion

This is all about different types of denoising filters used in MATLAB to reduce or remove noise from digital signals and images. In this tutorial, we covered the three most widely used denoising filters namely, 'median filter', 'Gaussian filter', and 'Wiener filter' and explained their implementation in MATLAB programming. You can try the above given MATLAB codes with your noisy images to see their functioning.

Updated on: 06-Sep-2023

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements