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?

  • $\begingroup$ 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$). $\endgroup$
    – user7657
    Sep 14, 2015 at 18:20

2 Answers 2


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. I implemented it in ApplyGaussianBlur.m in my FastGaussianBlur GitHub Repository.

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);     
vGaussianKernel = exp(-([-gaussianBlurRadius:gaussianBlurRadius] .^ 2) / (2 * gaussianKernelStd * gaussianKernelStd));
vGaussianKernel = vGaussianKernel / sum(vGaussianKernel);
  • $\begingroup$ how would you calculate the center value and the corner and such on? $\endgroup$
    – asd
    May 17, 2015 at 16:06
  • $\begingroup$ Look at the MATLAB code I linked to. It's all there. $\endgroup$
    – Royi
    May 17, 2015 at 17:45
  • $\begingroup$ @asd, Could you please review my answer? If it works for you, please mark it. Otherwise, Let me know what's missing. $\endgroup$
    – Royi
    Mar 26, 2022 at 17:19

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.

  • $\begingroup$ First, this is a good answer. I +1 it. In discretization there isn't right or wrong, there is only how close you want to approximate. I agree your method will be more accurate. But there are even more accurate methods than both. In many cases the method above is good enough and in practice this is what's being used. Welcome to DSP! $\endgroup$
    – Royi
    Aug 22, 2018 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.