# [ACCEPTED]-By which measures should I set the size of my Gaussian filter in MATLAB?-gaussian

Score: 15

One of the most common and heuristic measures 45 on determining the size and ultimately the 44 standard deviation of the Gaussian filter 43 is what is known as the 3-sigma rule. If you recall 42 from probability, the Gaussian distribution 41 has most of its values centered between 40 `[mu - 3*sigma, mu + 3*sigma]` where `mu` is the mean of the distribution 39 and `sigma` is the standard deviation of the distribution. This 38 is actually known as a 99% confidence interval. A good diagram 37 of this is shown below: Source: Wikipedia

By taking a look 36 at `[mu - 3*sigma, mu + 3*sigma]`, most of the variation can be contained 35 within 99% of the total area underneath 34 the Gaussian distribution. As a sidenote, between 33 `[mu - 2*sigma, mu + 2*sigma]`, this covers roughly 95% of the total area 32 and finally for `[mu - sigma, mu + sigma]`, this covers roughly 68% of 31 the total area.

As such, what people usually 30 do is take a look at an image and figure 29 out what the smallest feature is. They measure 28 the width or height of the feature and ensure 27 that the width / height / span of the feature 26 fits within the 99% confidence interval. Measuring 25 across gives us a total width of `6*sigma`. However, because 24 we are dealing in the discrete domain, we 23 need to also accommodate for the centre of the 22 Gaussian as well. As such, you want to 21 ensure that the total width is thus: `2 * floor(3*sigma) + 1`. Therefore, what 20 you need to do is figure out the width you 19 want. Once you do that, you can figure 18 out what `sigma` is required to satisfy this width. As 17 an example, let's say the width of our smallest 16 feature was `19`. You would then figure out 15 what your `sigma` was by:

``````19 = 2*floor(3*sigma) + 1
19 = 6*sigma + 1
18 = 6*sigma
sigma = 3
``````

Therefore, you would create 14 your Gaussian kernel like so:

``````h = fspecial('gaussian', [19 19], 3);
``````

If you want 13 to play around with the mask size, simply 12 use the above equation to manipulate and 11 solve for `sigma` each time. Now to answer your 10 question about size, this is a low-pass filter. As such, increasing 9 the size of the matrix will actually increase 8 the effects of the LPF. Your image will 7 become more progressively blurred as you 6 increase its size. Play around with the 5 size and see what you get. If you don't 4 have any particular image in mind when trying 3 this out, you can use any built-in image 2 in MATLAB instead. As such, try doing the 1 following:

``````%// Read in the image - Part of MATLAB path

%// Determine widths and standard deviations
width1 = 3; sigma1 = (width1-1) / 6;
width2 = 7; sigma2 = (width2-1) / 6;
width3 = 13; sigma3 = (width3-1) / 6;
width4 = 19; sigma4 = (width4-1) / 6;

%// Create Gaussian kernels
h1 = fspecial('gaussian', [width1 width1], sigma1);
h2 = fspecial('gaussian', [width2 width2], sigma2);
h3 = fspecial('gaussian', [width3 width3], sigma3);
h4 = fspecial('gaussian', [width4 width4], sigma4);

%// Filter the image using each kernel
out1 = imfilter(im, h1, 'replicate');
out2 = imfilter(im, h2, 'replicate');
out3 = imfilter(im, h3, 'replicate');
out4 = imfilter(im, h4, 'replicate');

%// Display them all on a figure
figure;
subplot(2,2,1);
imshow(out1);
title(['Width = 3']);
subplot(2,2,2);
imshow(out2);
title(['Width = 7']);
subplot(2,2,3);
imshow(out3);
title(['Width = 13']);
subplot(2,2,4);
imshow(out4);
title(['Width = 19']);
``````

You'll get the following output: Score: 2

Theoretically the gauss bell has a infinite 11 size, but this would simply last to long 10 to calculate.

Take a look at this output:

``````>> fspecial('gaussian', [7, 7], 1)

ans =

0.0000    0.0002    0.0011    0.0018    0.0011    0.0002    0.0000
0.0002    0.0029    0.0131    0.0216    0.0131    0.0029    0.0002
0.0011    0.0131    0.0586    0.0966    0.0586    0.0131    0.0011
0.0018    0.0216    0.0966    0.1592    0.0966    0.0216    0.0018
0.0011    0.0131    0.0586    0.0966    0.0586    0.0131    0.0011
0.0002    0.0029    0.0131    0.0216    0.0131    0.0029    0.0002
0.0000    0.0002    0.0011    0.0018    0.0011    0.0002    0.0000
``````

You 9 can see that the outer columns/rows are 8 filled with very small values, which will 7 have no relevant input to the result. For 6 such a small standard derivation, you can 5 use a smaller filter to save computation 4 time. I would suggest to apply different 3 sizes to an image with sharp edges, if the 2 size is small and the derivation high you 1 will see artefacts.

More Related questions