Does the 'gaussian' filter in MATLAB convolve the image with the Gaussian kernel? Also, how do you choose the parameters hsize (size of filter) and sigma? What do you base it on?
3 Answers
You first create the filter with fspecial and then convolve the image with the filter using imfilter (which works on multidimensional images as in the example).
You specify sigma
and hsize
in fspecial
.
Code:
%%# Read an image
I = imread('peppers.png');
%# Create the gaussian filter with hsize = [5 5] and sigma = 2
G = fspecial('gaussian',[5 5],2);
%# Filter it
Ig = imfilter(I,G,'same');
%# Display
imshow(Ig)
-
1Can you kindly specify what does 'same' mean in Ig = imfilter(I,G,'same'); – Ali Mohsan Nov 05 '18 at 23:40
@Jacob already showed you how to use the Gaussian filter in Matlab, so I won't repeat that.
I would choose filter size to be about 3*sigma in each direction (round to odd integer). Thus, the filter decays to nearly zero at the edges, and you won't get discontinuities in the filtered image.
The choice of sigma depends a lot on what you want to do. Gaussian smoothing is low-pass filtering, which means that it suppresses high-frequency detail (noise, but also edges), while preserving the low-frequency parts of the image (i.e. those that don't vary so much). In other words, the filter blurs everything that is smaller than the filter.
If you're looking to suppress noise in an image in order to enhance the detection of small features, for example, I suggest to choose a sigma that makes the Gaussian just slightly smaller than the feature.
-
not sure what the etiquette is for this, but i have a question regarding your answer - how can you tell what "size" the filter is based on the sigma? can you somehow get the dimensionality of the filter based on the sigma value? i thought the filter size was actually the first parameter of the function (5x5 in the post above)? – Tony Stark Jun 30 '10 at 12:24
-
@hatorade: With 'size' I don't mean dimensionality, but size of the mask. I.e. whether to choose [5 5] or [7 7]. – Jonas Jun 30 '10 at 13:56
In MATLAB R2015a or newer, it is no longer necessary (or advisable from a performance standpoint) to use fspecial
followed by imfilter
since there is a new function called imgaussfilt
that performs this operation in one step and more efficiently.
The basic syntax:
B = imgaussfilt(A,sigma)
filters imageA
with a 2-D Gaussian smoothing kernel with standard deviation specified bysigma
.
The size of the filter for a given Gaussian standard deviation (sigam
) is chosen automatically, but can also be specified manually:
B = imgaussfilt(A,sigma,'FilterSize',[3 3]);
The default is 2*ceil(2*sigma)+1
.
Additional features of imgaussfilter
are ability to operate on gpuArray
s, filtering in frequency or spacial domain, and advanced image padding options. It looks a lot like IPP... hmmm. Plus, there's a 3D version called imgaussfilt3
.

- 30,359
- 6
- 75
- 132
-
I have a confusion over very naive thing. Can you please clarify: Is Gaussian filter we use in Matlab technically the inverse Fourier of actual Gaussian filter devised for low-pass filtering? – Failed Scientist Feb 12 '16 at 10:52
-
1
-
You don't have to use an array `[3 3]` to specify a square filter. You can simply use `3`. The array notation is used when the filter is not squared. – nbro Mar 26 '19 at 20:03