Gaussian blurring using separable kernel in C++

Gaussian blurring is used to reduce the noise and details of the image. It reduces the image’s high frequency components and thus it is type of low pass filter. Gaussian blurring is obtained by convolving the image with Gaussian function. For more information about Gaussian function see the Wikipedia page.  Since 2D Gaussian function can be obtained by multiplying two 1D Gaussian functions, the blurring can be obtained by using separable kernel.

Use of Separable Kernel

Convolution is very expensive computationally. For an n x n kernel requires n2 multiplication and the same number of additions per pixel, and there are typically 105 – 106 pixels per image. So a much more efficient algorithm can be used for convolution in the small number of cases where a kernel is separable. A separable n x n kernel can be represented as a vector product of two orthogonal, one-dimensional kernels, each of width n. Convolution with the kernel can be carried out using there one-dimensional components. One of them is applied down the columns of an image, generating intermediate result. The other kernel is then applied along the rows of the intermediate image, producing the final result.

Complexity of standard kernel O(n2)
Complexity of separable kernel O(n)

It is estimated that convolution with a separable 15 x 15 kernel requires just 13 percent of the computation necessary when a non-separable kernel is used.

How to determine if a Kernel is separable?
The easiest way to test if a Kernel is separable is to calculate the rank of the matrix. If the rank is 1, then it is separable (i.e. you can break down the matrix in two vectors 1D vectors:). The rank of the Gaussian Kernel is therefore 1.

In the code below I have used 1D Gaussian function. The discrete value of 1D Gaussian function is calculated using this method and is given by

Here sigma = 1 and centre of distribution is assumed to be at x = 0

Source Code : C++

SHARE Gaussian blurring using separable kernel in C++

You may also like...

11 Responses

  1. Hello!
    Nice tutorial, can you please reffer to some literature that explains the principle behind a separable kernel?, I've been trying to find it myself, but I haven't been able to.

    For any help you can provide, Thankyou very much!

  2. @Roberto, the book Real-Time Rendering is a great book all kind of rendering, and has information on gaussian blur, both "regular" and with separable kernel.

    @Bibek, your coefficients are wrong! It adds up to 0.96. Your two 0.224 values should be 0.2442.

  3. @Eldon nice catch up. Thank you very much. I think I had written this post in sleepy mind 😛

  4. owl says:

    in reflect you need add return x at the end

  5. Thank you so much. It is corrected.

  6. Good morning Mr. Bibek.
    I want to ask, why would you use a loop from -2 to 2?
    thank you

  7. Hello Indra
    I have used the Gaussian Kernel of size 5×5. So starting from -2 to 2 will give the expected result. If your query is why not to use from 1 to 5, you must look at the convolution operation first.

  8. Hello Bibek,
    I just tested this codes, but it did not show the src and dst image. Although the program built success, not any error.
    Can you talk the causal ?
    Thanks !

  9. Hello Bibek,
    I tested this codes, but not show source and dst image.
    The built did not include any errors.
    Thank you !

  10. Anonymous says:

    Wrong code. You'll need a change loops in the vertical – along y block. Also it must be combined. Very basic poor code. Shukra jazilan. Inwala

Leave a Reply

Your email address will not be published. Required fields are marked *