Unit IV
fspecial() Function in MATLAB
The fspecial() function in MATLAB is used to create predefined 2-D filters (filter
masks). These filters are mainly used for image processing operations such as
smoothing, sharpening, edge detection, and motion blurring.
It generates a filter matrix that is usually applied to an image using functions like
imfilter() or filter2().
Syntax
w = fspecial(type)
w = fspecial(type, parameters)
w → filter (kernel) matrix
type → type of filter
parameters → values required for specific filters
Common Filter Types
Average (Smoothing Filter)
Used to blur or smooth an image.
h = fspecial('average', [3 3]);
Creates a 3×3 averaging filter.
Gaussian Filter
Used for noise reduction and smoothing.
h = fspecial('gaussian', [5 5], 1.0);
[5 5] → filter size
1.0 → standard deviation (sigma)
Laplacian Filter
Used for edge detection.
h = fspecial('laplacian', 0.2);
Sobel Filter
Detects edges (horizontal or vertical).
h = fspecial('sobel');
Prewitt Filter
Another edge detection operator.
h = fspecial('prewitt');
Example –
>> f = imread('[Link]');
>> w = fspecial('gaussian', [5 5], 2);
>> g = imfilter(f, w);
>> imshow(g);
***