30

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?

Dima
  • 37,098
  • 13
  • 69
  • 112
md86
  • 301
  • 1
  • 3
  • 3

3 Answers3

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)
Community
  • 1
  • 1
Jacob
  • 33,032
  • 14
  • 105
  • 160
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.

Community
  • 1
  • 1
Jonas
  • 73,844
  • 10
  • 134
  • 175
  • 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
15

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.

chappjc
  • 29,576
  • 6
  • 70
  • 120
  • 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
    This is the most up to date answer as of Sep 2018 – ijuneja Sep 02 '18 at 13:26
  • 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