You are on page 1of 6

fspecial

Create predefined 2-D filter

Syntax
h = fspecial(type)
h = fspecial(type,parameters)

Description
h = fspecial(type) creates a two-dimensional filter h of the specified type. fspecial
returns h as a correlation kernel, which is the appropriate form to use with imfilter. type
is a string having one of these values.
Value Description
'average' Averaging filter
'disk' Circular averaging filter (pillbox)
'gaussian' Gaussian lowpass filter
'laplacian' Approximates the two-dimensional Laplacian operator
'log' Laplacian of Gaussian filter
'motion' Approximates the linear motion of a camera
'prewitt' Prewitt horizontal edge-emphasizing filter
'sobel' Sobel horizontal edge-emphasizing filter
'unsharp' Unsharp contrast enhancement filter

h = fspecial(type,parameters) accepts a filter type plus additional modifying parameters


particular to the type of filter chosen. If you omit these arguments, fspecial uses default
values for the parameters.

The following list shows the syntax for each filter type. Where applicable, additional
parameters are also shown.
 h = fspecial('average',hsize) returns an averaging filter h of size hsize.
The argument hsize can be a vector specifying the number of rows and columns in
h, or it can be a scalar, in which case h is a square matrix. The default value for
hsize is [3 3].
 h = fspecial('disk',radius) returns a circular averaging filter (pillbox) within
the square matrix of side 2*radius+1. The default radius is 5.
 h = fspecial('gaussian',hsize,sigma) returns a rotationally symmetric
Gaussian lowpass filter of size hsize with standard deviation sigma (positive).
hsize can be a vector specifying the number of rows and columns in h, or it can be a
scalar, in which case h is a square matrix. The default value for hsize is [3 3]; the
default value for sigma is 0.5.
 h = fspecial('laplacian',alpha) returns a 3-by-3 filter approximating the
shape of the two-dimensional Laplacian operator. The parameter alpha controls the
shape of the Laplacian and must be in the range 0.0 to 1.0. The default value for
alpha is 0.2.
 h = fspecial('log',hsize,sigma) returns a rotationally symmetric Laplacian
of Gaussian filter of size hsize with standard deviation sigma (positive). hsize can
be a vector specifying the number of rows and columns in h, or it can be a scalar, in
which case h is a square matrix. The default value for hsize is [5 5] and 0.5 for
sigma.
 h = fspecial('motion',len,theta) returns a filter to approximate, once
convolved with an image, the linear motion of a camera by len pixels, with an angle
of theta degrees in a counterclockwise direction. The filter becomes a vector for
horizontal and vertical motions. The default len is 9 and the default theta is 0,
which corresponds to a horizontal motion of nine pixels.
 h = fspecial('prewitt') returns the 3-by-3 filter h (shown below) that
emphasizes horizontal edges by approximating a vertical gradient. If you need to
emphasize vertical edges, transpose the filter h'.
[111
000
-1 -1 -1 ]
To find vertical edges, or for x-derivatives, use h'.
 h = fspecial('sobel') returns a 3-by-3 filter h (shown below) that emphasizes
horizontal edges using the smoothing effect by approximating a vertical gradient. If
you need to emphasize vertical edges, transpose the filter h'.
[121
000
-1 -2 -1 ]
 h = fspecial('unsharp',alpha) returns a 3-by-3 unsharp contrast
enhancement filter. fspecial creates the unsharp filter from the negative of the
Laplacian filter with parameter alpha. alpha controls the shape of the Laplacian and
must be in the range 0.0 to 1.0. The default value for alpha is 0.2.

Note Do not be confused by the name of this filter: an unsharp filter is an


image sharpening operator. The name comes from a publishing industry
process in which an image is sharpened by subtracting a blurred (unsharp)
version of the image from itself.

Class Support
h is of class double.

Example
I = imread('cameraman.tif');
subplot(2,2,1);
imshow(I); title('Original Image');

H = fspecial('motion',20,45);
MotionBlur = imfilter(I,H,'replicate');
subplot(2,2,2);
imshow(MotionBlur);title('Motion Blurred Image');

H = fspecial('disk',10);
blurred = imfilter(I,H,'replicate');
subplot(2,2,3);
imshow(blurred); title('Blurred Image');

H = fspecial('unsharp');
sharpened = imfilter(I,H,'replicate');
subplot(2,2,4);
imshow(sharpened); title('Sharpened Image');

Algorithms
fspecial creates Gaussian filters using
fspecial creates Laplacian filters using

fspecial creates Laplacian of Gaussian (LoG) filters using

fspecial creates averaging filters using


ones(n(1),n(2))/(n(1)*n(2))

fspecial creates unsharp filters using

See Also
conv2, edge, filter2, fsamp2, fwind1, fwind2, imfilter

del2 in the MATLAB Function Reference

imfilter
N-D filtering of multidimensional images

Syntax
B = imfilter(A,H)
B = imfilter(A,H,option1,option2,...)

Description
B = imfilter(A,H) filters the multidimensional array A with the multidimensional filter H.
The array A can be a nonsparse numeric array of any class and dimension. The result B has
the same size and class as A.

Each element of the output B is computed using double-precision floating point. If A is an


integer array, then output elements that exceed the range of the integer type are truncated,
and fractional values are rounded.
B = imfilter(A,H,option1,option2,...) performs multidimensional filtering
according to the specified options. Option arguments can have the following values.
Boundary Options

Option Description
X Input array values outside the bounds of the array are implicitly assumed to
have the value X. When no boundary option is specified, imfilter uses X =
0.
'symmetric' Input array values outside the bounds of the array are computed by mirror-
reflecting the array across the array border.
'replicate' Input array values outside the bounds of the array are assumed to equal the
nearest array border value.
'circular' Input array values outside the bounds of the array are computed by implicitly
assuming the input array is periodic.

Output Size Options

Option Description
'same' The output array is the same size as the input array. This is the default behavior
when no output size options are specified.
'full' The output array is the full filtered result, and so is larger than the input array.

Correlation and Convolution Options

Option Description
'corr' imfilter performs multidimensional filtering using correlation, which is the same
way that filter2 performs filtering. When no correlation or convolution option is
specified, imfilter uses correlation.
'conv' imfilter performs multidimensional filtering using convolution.

N-D convolution is related to N-D correlation by a reflection of the filter matrix.

Note On Intel architecture processors, imfilter can take advantage of the Intel
Performance Primitives Library (IPPL), thus accelerating its execution time. IPPL is
activated only if A and H are both two-dimensional and A is of class uint8, int16, or
single.

Examples
Read a color image into the workspace and view it.
originalRGB = imread('peppers.png');
imshow(originalRGB)

Create a filter, h, that can be used to approximate linear camera motion.


h = fspecial('motion', 50, 45);

Apply the filter, using imfilter, to the image rgb to create a new image, rgb2.
filteredRGB = imfilter(originalRGB, h);
figure, imshow(filteredRGB)
Note that imfilter is more memory efficient than some other filtering operations in that it
outputs an array of the same data type as the input image array. In this example, the output is
an array of uint8.
whos rgb2
Name Size Bytes Class

h 37x37 10952 double array


rgb 384x512x3 589824 uint8 array
rgb2 384x512x3 589824 uint8 array

Specify the replicate boundary option.


boundaryReplicateRGB = imfilter(originalRGB, h, 'replicate');
figure, imshow(boundaryReplicateRGB)

See Also
conv2, convn, filter2, fspecial, ippl

You might also like