


The Art of Interface 
Article 9Gaussian filter, or Gaussian blurCategory. Digital signal and image processing (DSP and DIP) software development. Abstract. The article is a practical tutorial for Gaussian filter, or Gaussian blur understanding and implementation of its separable version. Article contains theory, C++ source code, programming instructions and a sample application.
1. Introduction to Gaussian filter, or Gaussian blurGaussian 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. To get acquainted with filter window idea in signal and image processing read our “Filter window, or filter mask” article. 2. Understanding Gaussian filter, or Gaussian blurFirst 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 bellfunction because of its shape. The most general function formula is:
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:
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:
Now we can start up MatLab and type lines like:
Which tells MatLab to calculate our integral (3). The result is
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. Back to our discrete normal distribution: we are interested in points {x_{−N}, x_{−N+1}, ... , x_{−1}, x_{0}, x_{1}, ... , x_{N−1}, x_{N}}, where x_{−n}=−x_{n} and x_{N}=3σ, and respectively in values of normal distribution at these points: {G(x_{−N}), G(x_{−N+1}), ... , G(x_{−1}), G(x_{0}), G(x_{1}), ... , G(x_{N−1}), G(x_{N})}. So, we have 2N+1 value set {G_{n}  n=−N, −N+1, ... , N} where G_{n}=G(x_{n}). 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 G_{n}: G'_{n}=G_{n}/k so that ∑G'_{n}=1 — sum of all of them to be one: G'_{n}=G_{n}/∑G_{n} — which means k=∑G_{n}. Thus, we have our window weights {G'_{n}  n=−N, −N+1, ... , N} where G'_{n}=G_{n}/k, G_{n}=G(x_{n}), k=∑G_{n} and x_{N}=3σ which means as well x_{n}=3σn/N. To get expression for G'_{n} for practical use let us write down the detailed formula:
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={s_{i}} then for every signal element s_{i} the new modified value s'_{i} will be s'_{i}=∑G'_{n}s_{i+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 stepbystep instructions for processing by 1D Gaussian filter or blur. 1D Gaussian filter, or Gaussian blur algorithm:
Let us proceed with 2D case. 3. 2D caseExpression for 2D Gaussian distribution is:
And its plot is depicted below — fig. 3. Fig. 3. 2D Gaussian or normal distribution.Gaussian distribution has surprising property. Look, its expression could be rewritten as:
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:
2D Gaussian filtering with [2N+1]×[2N+1] window is reduced to a couple of 1D filterings with 2N+1 window. That means significant speedup especially for large images because of jump from O(N^{2}) 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 programmingWe are starting with 2D filter because 1D one could be easily got just by treating signal as oneline image and canceling vertical filtering. First of all a couple of simple auxiliary structures.
The method implements steps 1–4 of 1D Gaussian filter algorithm to calculate weights according to expression (4) and utilizes window symmetry G_{−n}=G_{n}. Now, the last problem to be solved before we can start filtering the image is its extension. 5. ExtensionThere 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. Fig. 4. Data extension.And here is
The job is done inside methods
Method
Pasting has two modes — horizontal and vertical. In horizontal mode line is copied into the extension and in vertical mode column is copied element by element.
Method
Now we have everything to program filtering method.
Structure of the method is quite straightforward: input parameters check, memory allocation, window weights calculation, applying 1D filter in horizontal direction (first loop) and applying it in vertical direction (second loop). Parameters check is a short function below.
Which means pointer to image should not be 6. 1D Gaussian filter, or Gaussian blur programming1D filter is truncated version of 2D filter:
You can see familiar steps here just now filter makes one pass along signal array. Our separable Gaussian filter library is ready! You can download full source code here:
Full file listings are available online as well:
6. How to usePay attention that our solution is universal. First, our filter has window of arbitrary size. Second, we have developed class template that suits any type of input data —
— all these declarations are valid and will process your data graciously. To use the filter you should include header file gaussianblur.h and place in your code lines like:
Here image is stored as double array of 512×512 size and it is filtered inplace with Gaussian window of 13×13 pixels width. And now — an application to play around! 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:
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 useStart up gaussianblur.exe application. Load the image. Fig. 5. Original image.Set filter window size: Set >> Window size... or click wbutton in toolbar, in dialog key in size, for instance 13. Blur image with Gaussian filter by choosing Set >> Filter or clicking Fbutton in toolbar. See the result. Fig. 6. Image blurred by Gaussian filter.


