Filter Images Using Predefined Filters

This example shows how to create filters using the fspecial function that can be used with imfilter. The fspecial function produces several kinds of predefined filters, in the form of correlation kernels. This example illustrates applying an unsharp masking filter to a grayscale image. The unsharp masking filter has the effect of making edges and fine detail in the image more crisp.

Read image.

I = imread('moon.tif');

Create filter, using fspecial .

h = fspecial('unsharp')
h =

   -0.1667   -0.6667   -0.1667
   -0.6667    4.3333   -0.6667
   -0.1667   -0.6667   -0.1667

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?