Filter Images Using imfilter

This example shows how to filter an image with a 5-by-5 filter containing equal weights (often called an averaging filter) using imfilter.

MATLAB® has several two-dimensional and multidimensional filtering functions. The function filter2 performs two-dimensional correlation, conv2 performs two-dimensional convolution, and convn performs multidimensional convolution. Each of these filtering functions always converts the input to double, and the output is always double. These other filtering functions always assume the input is zero padded, and they do not support other padding options.

In contrast, the imfilter function does not convert input images to double. The imfilter function also offers a flexible set of boundary padding options.

Read image.

I = imread('coins.png');

Create filter.

h = ones(5,5) / 25;

Apply filter to image using imfilter.

I2 = imfilter(I,h);

Display original image and filtered image for comparison.

imshow(I), title('Original Image');
figure, imshow(I2), title('Filtered Image')

Was this topic helpful?