MATLAB Mean Filter

Ammar Ali May 12, 2021

This tutorial will introduce how to create and apply the median or averaging filter using the `medfilt2()`, `fspecial()`, and `imfilter()` function in MATLAB.

Create and Apply the Median Filter Using the `medfilt2()` Function in MATLAB

If you want to remove noise or to smooth your image, you can use the `medfilt2()` function to create and apply the median filter to smooth the given image. The first argument of this function is the image you want to smooth, and the second argument is the m-by-n neighborhood value around the pixel of the input image. If the second argument is not given, the function will use the default value, which is 3-by-3. For example, let’s add salt and pepper noise to a given image using the `imnoise()` function and then smooth it using the `medfilt2()` function. See the code below.

``````Input_image = imread('eight.tif');
Noisy_image = imnoise(Input_image,'salt & pepper',0.03);
Smoothed_image = medfilt2(Noisy_image);
imshowpair(Noisy_image,Smoothed_image,'montage')
``````

Output:

In the above code, we used an already stored image of coins which you can change according to your requirements. In the above figure, the left image is the noisy image, and the right image is the smoothed image using the median filter. The two images are shown side by side using the `imshowpair()` function. Check this link for more details about the `medfilt2()` function.

Creating and Applying the Averaging Filter Using the `fspecial()` and `imfilter()` Function in MATLAB

If you want to remove noise or to smooth your image, you can use the `fspecial()` and `imfilter()` function to create and apply a specific filter to smooth the given image. The filters available in this function are average, disk, gaussian, laplacian, log, motion, etc. The first argument of the `fspecial()` function is the name of the filter, and the second argument is the size of the filter. The first argument of the `imfilter()` function is the image you want to smooth, and the second argument is the filter you made using the `fspecial()` function. For example, let’s add salt and pepper noise to a given image using the `imnoise()` function and then smooth it using the `imfilter()` function. See the code below.

``````Input_image = imread('eight.tif');
Noisy_image = imnoise(Input_image,'salt & pepper',0.03);
h = fspecial('average', [3 3]);
Smoothed_image = filter2(h, Input_image);
imshowpair(Noisy_image,Smoothed_image,'montage')
``````

Output:

In the above code, we used an already stored image of coins and the `fspecial()` function to create an averaging filter of size 3-by-3, and then we used the `imfilter()` function to smooth the noisy image. In the above figure, the left image is the noisy image, and the right image is the smoothed image using the median filter. The two images are shown side by side using the `imshowpair()` function. Check this link for more details about the `fspecial()` function.

Author: Ammar Ali

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.