How to Apply Median Filter for RGB Image in MATLAB?


In digital image processing, filtering is defined as a process of modifying an image to enhance its visibility. Image filtering in digital image processing involves the use of some mathematical operations applied to each pixel of the image. It alters the pixel value to achieve the desired corrections like noise reduction, edge detection, image enhancement, etc.

The image filtering is performed by applying a filter to the image. Where, the filter is nothing but a matrix of numbers that determine how to modify the pixel values.

There are several types of image filters available based on different requirements. Some common examples of filters are median filter, gaussian filter, Laplacian filter, high−pass filter, low−pass filter, etc.

In this article, we will study how to apply the median filter to an RGB image using MATLAB programming. Before that let’s have a look into the basic theory of median filter.

What is Median Filter in MATLAB?

In digital image processing, the median filter is a type of image filter applied to remove the impulse noise or salt−and−pepper noise from the image. This filter changes the value of the central pixel in a neighborhood with a median value.

In MATAB, we can use a built−in function ‘medfilt3’ to apply the median filter to an image. This function takes the input image along with the size of the neighborhood window as arguments, and then returns a filtered image.

Syntax of medfilt3

To apply median filter to an image, we use the following syntax:

f = medfilt3(img, [x, y, z]);

Where, ‘img’ is the input RGB image, [x, y, z] is the size vector of the neighborhood window.

Benefits of Median Filter

The following are some key benefits of using the median filter to an image:

  • The median filter can effectively remove the impulsive noise or salt−and−pepper noise from image and eliminates the outliers.

  • This filter preserves the images details.

  • The median filter does not affect the sharp edges in the image, instead it maintains the edge information as it is when reducing the noise.

  • The median filter is a type of non−linear filter and hence can be effectively applied to images with complex structures and varying intensities.

Disadvantages of Median Filter

Apart from advantages, the median filter also has some disadvantages. Some key disadvantages of median filter are as follows:

  • The median filter introduces a smoothening effect in the image due to noise reduction.

  • This filter can cause a loss of texture in the image.

  • The median filter requires an optimal window size depending on the image and noise properties. If the selected window size is not appropriate, it does not reduce noise effectively.

Applications of Median Filter

The median filter is widely used in the following applications:

  • The median filter is mainly used in digital image processing applications to reduce the noise.

  • The median filter is applied to enhance images where edge preservation is crucial.

  • The median filter can also be used to reduce noise in audio signals.

After getting an overview of the median filter, let us now discuss its MATLAB implementation.

The following MATLAB codes demonstrate the use of the ‘medfilt3’ function to apply median filter to an image.

Example

% MATLAB code for applying median filter to an RGB image
% Read the input RGB image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425340.jpg');
% Apply the median filter to the RGB image
filtered_img = medfilt3(img, [5, 5, 5]);
% Display the original and filtered images
figure;
subplot(1, 2, 1); imshow(img); title('Original Image');
subplot(1, 2, 2); imshow(filtered_img); title('Filtered Image');

Output

Code Explanation

In this MATLAB, we start by reading the input RGB image by using the ‘imread’ function, and store it in the ‘img’ variable. Next, we use the ‘medfilt3’ function to apply the median filter to the image and store the result in the ‘filtered_img’ variable.

Finally, we display the original and filtered images by using the ‘imshow’ function in a figure window with suitable titles.

Example

% MATLAB code for applying median filter to an RGB image
% Read the input RGB image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');
% Apply the median filter to the RGB image
filtered_img = medfilt3(img, [3, 3, 3]);
% Display the original and filtered images
figure;
subplot(1, 2, 1); imshow(img); title('Original Image');
subplot(1, 2, 2); imshow(filtered_img); title('Filtered Image');

Output

Code Explanation

The explanation for this MATLAB program is the same as that of the previous program. The only difference is that the vector defining the neighborhood window size is different. It reflects in the output as well.

Hence, this is all about applying the median filter to an RGB image in MATLAB. In this article, we have explained the median filter in MATLAB with the help of example programs.

Updated on: 07-Aug-2023

185 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements