# How to Calculate Gaussian Kernel for a Small Support Size?

How to calculate the values of Gaussian kernel? I think I understand the principle of it weighting the center pixel as the means, and those around it according to the $\sigma$ but what would each value be if we should manually calculate a $3\times 3$ kernel?

• A 3x3 kernel is only possible for small $\sigma$ ($<1$). In particular, you can use the binomial kernel with coefficients $$1\ 2\ 1\\2\ 4\ 2\\1\ 2\ 1$$ The Gaussian kernel is separable and it is usually better to use that property (1D Gaussian on $x$ then on $y$). – Yves Daoust Sep 14 '15 at 18:20

Gaussian Kernel is made by using the Normal Distribution for weighing the surrounding pixel in the process of Convolution.

Since we're dealing with discrete signals and we are limited to finite length of the Gaussian Kernel usually it is created by discretization of the Normal Distribution and truncation.

I created a project in GitHub - Fast Gaussian Blur.
It uses many methods to approximate the Gaussian Blur Filter and evaluate their speed and quality.

The most classic method as I described above is the FIR Truncated Filter:

https://github.com/RoyiAvital/FastGuassianBlur/blob/master/ApplyGaussianBlur.m

Regarding small sizes, well a thumb rule is that the radius of the kernel will be at least 3 times the STD of Kernel.
If you chose $3 \times 3$ kernel it means the radius is $1$ which means it makes sense for STD of $\frac{1}{3}$ and below.

Then just fill your Kernel By:

gaussianBlurRadius  = ceil(stdToRadiusFactor * gaussianKernelStd);

As said by Royi, a Gaussian kernel is usually built using a normal distribution. Each value in the kernel is calculated using the following formula : $$f(x,y) = \frac{1}{\sigma^22\pi}e^{-\frac{x^2+y^2}{2\sigma^2}}$$ where x and y are the coordinates of the pixel of the kernel according to the center of the kernel. This approach is mathematically incorrect, but the error is small when $\sigma$ is big. I use this method when $\sigma>1.5$, bellow you underestimate the size of your Gaussian function.
The previous approach is incorrect because the kernel represents the discretization of the normal distribution, thus each pixel should give the integral of the normal distribution in the area covered by the pixel and not just its value in the center of the pixel. If we have square pixels with a size of 1 by 1, the kernel values are given by the following equation : $$f(x,y) = \int_{x-0.5}^{x+0.5}\int_{y-0.5}^{y+0.5}\frac{1}{\sigma^22\pi}e^{-\frac{u^2+v^2}{2\sigma^2}} \, \mathrm{d}u \, \mathrm{d}v$$ To compute this value, you can use numerical integration techniques or use the error function as follows: $$f(x,y) = \frac{1}{4}\big(erf(\frac{x+0.5}{\sigma\sqrt2})-erf(\frac{x-0.5}{\sigma\sqrt2})\big)\big(erf(\frac{y-0.5}{\sigma\sqrt2})-erf(\frac{y-0.5}{\sigma\sqrt2})\big)$$ Finally, the size of the kernel should be adapted to the value of $\sigma$. My rule of thumb is to use $5\sigma$ and be sure to have an odd size.