# How to Apply Median Filter For RGB Image in MATLAB?

Last Updated : 21 Feb, 2022

Filtering by definition is the process of enhancing or modifying an image by applying some method or algorithm over a small area and extending that algorithm over all the areas of the image. Since we apply the methods over an area, filtering is classified as a neighboring operation.

Filters are essentially a matrix of values which when hovered over our region of interest in our image give new values by the mathematical computations depending on what type of filter is being applied. One of the popular and widely used filters is the Median Filter.

### Advantages of the Median Filter

• Preservation of sharp edges.
• Removal of salt and pepper noise, that is removal of instances in the image where there is spiky noise.

• The analytical analysis is difficult. There is no error propagation.

In this article, we will be discussing how to apply a Median filter for an RGB Image in MATLAB.

### Approach :

• Use the medfilt3(I,[m n p]) command to apply the median filter to each channel of the RGB Image I with neighborhood size m-by-n-by-p.
• Show both the Images together for comparison purposes.

Example:

## Matlab

 `% Matlab Code for implementing ` `% a Median Filter on an RGB Image.  ` `% Reading the Image  ` `I = imread(``'GFG.jpeg'``); ` ` `  `% Creating figure window for input image ` `figure  ` ` `  `% Displaying the input image ` `imshow(I); ` ` `  `% Applying the median filter on the image  ` `J = medfilt3(I,[3,3,3]); ` ` `  `% Creating figure window for output image ` `figure  ` ` `  `% Displaying the output image  ` `imshow(J); `

Output:

Figure 1: Input Image

Figure 2: Output Image