Gaussian filter in MATLAB
Asked Answered
L

3

31

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?

Lithiasis answered 5/5, 2010 at 13:30 Comment(1)
See also: stackoverflow.com/questions/2771114Jacquard
I
55

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)
Intelligentsia answered 5/5, 2010 at 13:37 Comment(1)
Can you kindly specify what does 'same' mean in Ig = imfilter(I,G,'same');Unforgettable
F
21

@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.

Ferrick answered 5/5, 2010 at 16:55 Comment(2)
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)?Wadi
@hatorade: With 'size' I don't mean dimensionality, but size of the mask. I.e. whether to choose [5 5] or [7 7].Ferrick
A
18

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 image A with a 2-D Gaussian smoothing kernel with standard deviation specified by sigma.

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 gpuArrays, 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.

Arola answered 6/3, 2015 at 22:1 Comment(3)
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?Ashe
This is the most up to date answer as of Sep 2018Ohara
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.Diameter

© 2022 - 2024 — McMap. All rights reserved.