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

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

### Code Explanation:

• imshow(I); This line displays the input image I in the figure window
• J = medfilt3(I,[3,3,3]); This line applies the median filtering of the 3-D image I in three dimensions. Each output voxel in J contains the median value in the 3-by-3-by-3 neighborhood around the corresponding voxel in I. (A Voxel is a 3D equivalent of a pixel)
• imshow(J); This line displays the output image J in another figure window.
• This method can be applied to various other RGB Images as well and can be toggled by changing the different neighborhood sizes of the voxels for observing the difference in results depending on what size neighborhood we choose.

Previous
Next