1. Introduction to Gaussian filter, or Gaussian blur

Gaussian filter is windowed filter of linear class, by its nature is weighted mean. Named after famous scientist Carl Gauss because weights in the filter calculated
according to Gaussian distribution — the function Carl used in his works. Another name for this filter is Gaussian blur.

2. Understanding Gaussian filter, or Gaussian blur

First of all let us have a look at what that Gaussian distribution is. Gaussian distribution, or normal distribution, is really a function of probability theory.
Often this function is referenced as bell-function because of its shape. The most general function formula is:

(1)

And its plot is depicted below — fig. 1.

Fig. 1. Gaussian or normal distribution.

In our case we can suppose parameter a — which called distribution mean or statistical expectation — responsible for distribution shifting
along x axis to be zero: a=0; and work with simplified form:

(2)

Thus the function is negative exponential one of squared argument. Argument divider σ plays the role of scale factor. σ parameter has special name:
standard deviation, and its square σ2 — variance. Premultiplier in front of the exponent is selected the area below plot to be 1.
Pay attention the function is defined everywhere on real axis x∈(−∞, ∞) which means it spreads endlessly to the left and to the right.

Now, first point is we are working in discrete realm, so our Gaussian distribution turns into the set of values at discrete points.

Second, we cannot work with something that spreads endlessly to the left and to the right. It means Gaussian distribution is to be truncated. The question is — where?
Usually in practice used the rule of 3σ that is the part of Gaussian distribution utilized is x∈[−3σ, 3σ] — see fig. 2.

Fig. 2. Gaussian distribution truncated at points ±3σ.

Why? Good question. To understand that let us see how much we have trimmed. The area below truncated part is:

(3)

Now we can start up MatLab and type lines like:

G=inline('1/sqrt(2*pi)*exp(-x.^2/2)');
quad(G, -3, 3)

Which tells MatLab to calculate our integral (3). The result is

ans =
0.9973

So, the area below our trimmed part is ≈0.997 — that is function is trimmed at points where we have accuracy better than 0.5%. Very good in our case.

What shall we do now with this set of values? Use it as window weights! That means we have weighted window of 2N+1 size. To be perfect we are to scale our
Gn: G'n=Gn/k so that ∑G'n=1 — sum of all of them to be one:
G'n=Gn/∑Gn — which means k=∑Gn.

Thus, we have our window weights {G'n | n=−N, −N+1, ... , N} where
G'n=Gn/k, Gn=G(xn), k=∑Gn and
xN=3σ which means as well xn=3σn/N. To get expression for G'n for practical use let us write down
the detailed formula:

(4)

As you can see we have simplification: σ is eliminated from consideration and G'n could be calculated easier via new values G"n and
their sum k'.

How to use these weights? If we have some input signal S={si} then for every signal element si the new modified value s'i
will be s'i=∑G'nsi+n, n=−N, −N+1, ... , N. In words that means “for every element
put our window so that this element is in the center of the window, multiply every element in the window by corresponding weight and sum up all those products, the sum got is the new filtered
value”.

Now we can write down step-by-step instructions for processing by 1D Gaussian filter or blur.

3. 2D case

Gaussian distribution has surprising property. Look, its expression could be rewritten as:

(6)

Which means 2D distribution is split into a pair of 1D ones, and so, 2D filter window (fig. 3) is separated into a couple of 1D ones from fig. 2.
Filter version that utilizes this fact is called separable one.

In practice it means that to apply filter to an image it is enough to filter it in horizontal direction with 1D filter and then filter the result with the same filter in vertical direction.
Which direction first really makes no difference — our operation is commutative. Thus,

2D separable Gaussian filter, or Gaussian blur, algorithm:

Calculate 1D window weights G'n;

Filter every image line as 1D signal;

Filter every filtered image column as 1D signal.

2D Gaussian filtering with [2N+1]×[2N+1] window is reduced to a couple of 1D filterings with 2N+1 window. That means significant speed-up
especially for large images because of jump from O(N2) to O(N) number of operations.

Now, when we have the algorithm, it is time to write some code — let us come down to programming.

4. 2D Gaussian filter, or 2D Gaussian blur programming

We are starting with 2D filter because 1D one could be easily got just by treating signal as one-line image and canceling vertical filtering.

CWindow structure designed to keep window size and set of weights, as well it has two methods to calculate weights and to apply 1D Gaussian filter starting from
a given image element: Create and Apply. As you can see Apply code is trivial and code for Create is a little bit more complicated:

Now, the last problem to be solved before we can start filtering the image is its extension.

5. Extension

There is a problem every windowed filter deals with. Being placed at the edge filter window lacks for data elements to be processed. There are two ways to solve the problem:
first is not to process edges and second, cleverer one, to extend data across edges. We are taking the second approach and extending our data like depicted in fig. 4.

Which means pointer to image should not be NULL, image and filter window sizes should be some positive numbers, window size 2N+1≡_W
should be odd number and N should be less than image size in any direction.

7. Color Gaussian blur

We have created an application to see Gaussian filter in action. The sample package includes 3 files — the applications, sample image and description:

gaussianblur.exe — Gaussian filter,

sample.bmp — 24-bit sample image,

readme.txt — description.

Be aware of the fact, that this sample uses OpenGL, so it should be supported by your system (usually that is the case).

8. How to use

Start up gaussianblur.exe application. Load the image.

Fig. 5. Original image.

Set filter window size: Set >> Window size... or click w-button in toolbar, in dialog key in size, for instance 13.
Blur image with Gaussian filter by choosing Set >> Filter or clicking F-button in toolbar. See the result.