views:

4159

answers:

3

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 funtion 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.

+6  A: 
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.

rcs
Great, that's what I needed. And found some other interesting filters there, if I could I would accept this answer twice :D Many thanks!
Gacek
+2  A: 
I = imread('peppers.png');
H = fspecial('average', [5 5]);
I = imfilter(I, H);
imshow(I)

Note that filters can be applied to intensity images (2D matrices) using filter2, while on multi-dimensional images (RGB images or 3D matrices) imfilter is used.

Also on Intel processors, imfilter can use the Intel Integrated Performance Primitives (IPP) library to accelerate execution.

Amro
You can't design a kernel for median filtering because it is a non-linear convolution: for each NxN neighborhood the calculations involve more than just products and sums.
Amro
+2  A: 

I see good answers have already been given, but I thought it might be nice to just give a way to perform mean filtering in MATLAB using no special functions or toolboxes. This is also very good for understanding exactly how the process works as you are required to explicitly set the convolution kernel. The mean filter kernel is fortunately very easy:

I = imread(...)
kernel = ones(3, 3) / 9; % 3x3 mean kernel
J = conv2(I, kernel, 'same'); % Convolve keeping size of I

Note that for colour images you would have to apply this to each of the channels in the image.

kigurai
Thanks. But in the imfilter documentation it is said that it uses conv2 in most of the cases. So reading the doc brought me to the same conclusions. Thanks anyway :)
Gacek