Mean filter for smoothing images in Matlab

Gacek picture Gacek · Nov 15, 2009 · Viewed 109.9k times · Source

I need to test some basic image processing techniques in Matlab. I need to test and compare especially two types of filters: mean filter and median filter.

To smooth image using median filtering, there is a great function medfilt2 from image processing toolbox. Is there any similar function for mean filter? Or how to use the filter2 function to create the mean filter?

One of the most important things for me is to have the possibility of setting radius of the filter. I.e. for median filter, if I want the [3 x 3] radius (mask), I just use

imSmoothed = medfilt2(img, [3 3]);

I would like to achieve something similar for mean filter.

Answer

rcs picture rcs · Nov 15, 2009
h = fspecial('average', n);
filter2(h, img);

See doc fspecial: h = fspecial('average', n) returns an averaging filter. n is a 1-by-2 vector specifying the number of rows and columns in h.