Serge's World

Blogging about software development, astronomy, genealogy and more.

Image Processing in C#: Part 7 - Applying a Gaussian Blur

Gaussian blur also uses convolution to create the effect. If you have not yet read my blog post on smoothing using convolution, I would advise you to read that post first, as in that post I explain how the convolution function works.

Gaussian blur works very similarly to the smoothing function, in that both are a type of blur, but the Gaussian blur gives a more natural looking blur. It achieves this by not have a flat weighting, but instead has a weighting based on a Gaussian curve, thus for our 3x3 grid, the values we want to supply the convolution function is as follows:

121
242
121

The factor parameter is set to the sum of each pixel in the grid, which in this case, is again, 16, and the offset is set to 0.

Now we will get a nice blur effect.

Gaussian blur

public void ApplyGaussianBlur(double peakValue)
{
    ConvolutionMatrix matrix = new ConvolutionMatrix(3);
    matrix.SetAll(1);
    matrix.Matrix[0, 0] = peakValue / 4;
    matrix.Matrix[1, 0] = peakValue / 2;
    matrix.Matrix[2, 0] = peakValue / 4;
    matrix.Matrix[0, 1] = peakValue / 2;
    matrix.Matrix[1, 1] = peakValue;
    matrix.Matrix[2, 1] = peakValue / 2;
    matrix.Matrix[0, 2] = peakValue / 4;
    matrix.Matrix[1, 2] = peakValue / 2;
    matrix.Matrix[2, 2] = peakValue / 4;
    matrix.Factor = peakValue * 4;
    bitmapImage = Convolution3x3(bitmapImage, matrix);

}

The full source used in the series is available from https://github.com/sjmeunier/image-processor.

Originally posted on my old blog, Smoky Cogs, on 19 Nov 2009

Tag Cloud

Algorithms (3) Android (10) Astronomy (25) Audio (1) Audiobooks (1) Barcodes (9) C# (69) Css (1) Deep sky (6) Esoteric languages (3) Family (3) Fractals (10) Gaming (1) Genealogy (14) General (2) Geodesy (3) Google (1) Graphics (3) Hubble (2) Humour (1) Image processing (23) Java (8) Javascript (5) jQuery (3) Jupiter (3) Maths (22) Moon (5) Music (4) Pets (5) Programming (88) Saturn (1) Science (1) Spitzer (4) Sun (4) Tutorials (68) Unity (3) Web (9) Whisky (13) Windows (1) Xamarin (2)