Today's Question:  What weekend projects have you created?        GIVE A SHOUT

Technical Article => Programming =>  Algorithm

Gaussian Blur Algorithm

  sonic0002      2012-11-24 08:42:13      51,575    1

Usually, image processing software will provide blur filter to make images blur.

There are many algorithms to implement blur, one of them is called Gaussian Blur algorithm. It utilizes Gaussian distribution to process images.

This article is to introduce Gaussian Blur algorithm, you will find this a simple algorithm. In fact, it is a kind of data smoothing which can be used in many situations.

1. Gaussian Blur theory

The so called blur can be understood as taking a pixel as the average value of its surrounding pixels.

On the above graph, 2 is the center point, the surrounding points are 1.

The center point will take the average value of its surrounding points, it will be 1. From value perspective, it's a smoothing. On graphic, it's a blur effect. The center point will lose its detail.


Obviously if the value range is very large, the blur effect is very strong.

The above are graphs of original, 3 pixels blur radius and 10 pixels blur radius. The bigger the blur radius, the more blur the picture is.

The question now is if every point will get the average value of surrounding points, then how should we allocate weight?

If we just use simple average, it's unreasonable, because images are continuous, the closer the points in distance, the closer the relationship between points. Hence the weighted average is more reasonable than simple average, the close the points in distance, the larger the weight.

2. Weight of normal distribution

Normal distribution is an acceptable weight distribution model.

On graphic, normal distribution is a Bell-shaped curve, the closer to the center, the bigger the value.

3. Gaussian function

The normal distribution above is one dimensional, the graph is two dimensional. We need two dimensional normal distribution.

The density function of normal distribution is called Gaussian function. The one dimension format is :

Here μ is the average of x, Because center point is the origin point when calculating average value, so μ equals to 0.

Based on the one dimension function , we can derive the two dimensional Gaussian function.

With this function, we can calculate the weight of each point.

4. Weight matrix

Assume the coordinate of the center point is (0,0), then the coordinates of 8 points which are nearest to it are:

To calculate the weight matrix, we need to set the value of σ, σ=1.5, then the weight matrix of blur radius 1 is

The sum of the weights of these 9 points is 0.4787147. If only calculate the Weighted average of these 9 points, then the sum should be 1, hence the above 9 values should divide 0.4787147.

5. Calculate Gaussian Blur

With weight matrix, we can calculate the value of Gaussian Blur.

Assume we have 0 pixels now, the gray value(0-255):

Each point multiplies its weight value:

Now we have:

Add these 9 values up, we will get the Gaussian Blur value of the center point.

Repeat this process for all other points, then we will get graph after Gaussian blur.

6. The process for the points at borders

If a point is at the border, there are not enough points, what should we do?

One solution is to copy all the existing points to respective places to form a new matrix.

Author : 阮一峰 Source :



Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious



tox [Reply]@ 2012-12-13 09:32:58
Using Pascal's triangle one can compute the necessary coefficients for practically any blur size. Because it is separable one can use a 1D window and work with pre-computed coefficients to speed things up, so a blur covering 3x1 pixels can be done with this function: blur3x1(m,a,b) = 0.25*m + 0.5*a + 0.25*b; // a = sample @ position, b = sample @ position + 1 and m = sample @ position - 1 I've just calculated all the coefficients for blurs of up to 21x1 pixels, so if they are of help to anyone else, here they are. Keep in mind that these coefficients are rounded and will not perfectly reproduce the real gaussian blur, but they should be precise enough for most uses. a = sample @ position, b = sample @ position + 1, c = sample @ position + 2, ..., k = sample @ position + 10 m = sample @ position - 1, n = sample @ position - 2, ..., v = sample @ position - 10 blur03x1 = 0.250000*m + 0.500000*a + 0.250000*b blur05x1 = 0.062500*n + 0.250000*m + 0.375000*a + 0.250000*b + 0.062500*c blur07x1 = 0.015625*o + 0.093750*n + 0.234575*m + 0.312500*a + 0.234575*b + 0.093750*c + 0.015625*d blur09x1 = 0.003906*p + 0.031250*o + 0.109375*n + 0.218750*m + 0.273438*a + 0.218750*b + 0.109375*c + 0.031250*d + 0.003906*e blur11x1 = 0.000977*q + 0.009766*p + 0.043945*o + 0.117188*n + 0.205078*m + 0.512695*a + 0.205078*b + 0.117188*c + 0.043945*d + 0.009766*e + 0.000977*f blur13x1 = 0.000244*r + 0.002930*q + 0.016113*p + 0.053711*o + 0.120850*n + 0.193359*m + 0.225506*a + 0.193359*b + 0.120850*c + 0.053711*d + 0.016113*e + 0.002930*f + 0.000244*g blur15x1 = 0.000061*s + 0.000854*r + 0.005554*q + 0.022217*p + 0.061096*o + 0.122192*n + 0.183289*m + 0.209473*a + 0.183289*b + 0.122192*c + 0.061096*d + 0.022217*e + 0.005554*f + 0.000854*g + 0.000061*h blur17x1 = 0.000015*t + 0.000244*s + 0.001831*r + 0.008545*q + 0.027771*p + 0.066650*o + 0.122192*n + 0.174561*m + 0.196301*a + 0.174561*b + 0.122192*c + 0.066650*d + 0.027771*e + 0.008545*f + 0.001831*g + 0.000244*h + 0.000015*i blur19x1 = 0.000004*u + 0.000069*t + 0.000584*s + 0.003113*r + 0.011673*q + 0.032684*p + 0.070816*o + 0.121399*n + 0.166924*m + 0.185471*a + 0.166924*b + 0.121399*c + 0.070816*d + 0.032684*e + 0.011673*f + 0.003113*g + 0.000584*h + 0.000069*i + 0.000004*j blur21x1 = 0.000001*v + 0.000019*u + 0.000181*t + 0.001087*s + 0.004621*r + 0.014786*q + 0.036964*p + 0.073929*o + 0.120134*n + 0.160179*m + 0.176197*a + 0.160179*b + 0.120134*c + 0.073929*d + 0.036964*e + 0.014786*f + 0.004621*g + 0.001087*h+ 0.000181*i + 0.000019*j + 0.000001*k greetz, tox


Yet another TCP protocol handshake

By sonic0002