Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Gaussian Blur - How to calculate convolution matrix (kernel)

My question is very close to this question: How do I gaussian blur an image without using any in-built gaussian functions?

The answer to this question is very good, but it doesn't give an example of actually calculating a real Gaussian filter kernel. The answer gives an arbitrary kernel and shows how to apply the filter using that kernel but not how to calculate a real kernel itself. I am trying to implement a Gaussian blur in C++ or Matlab from scratch, so I need to know how to calculate the kernel from scratch.

I'd appreciate it if someone could calculate a real Gaussian filter kernel using any small example image matrix.

like image 458
gsingh2011 Avatar asked Nov 20 '11 20:11

gsingh2011


People also ask

What is convolution kernel Gaussian blur?

A gaussian blur is a convolution function that uses a really ugly (you've seen the wikipedia page) function to compute a convolution kernel to pass over the image. You'll find an example kernel for a gaussian in that wikipedia page.

What is kernel size in Gaussian blur?

The Gaussian function shown has a standard deviation of 10x10 and a kernel size of 35x35 pixels.

How is Gaussian blur calculated?

In a Gaussian blur, the pixels nearest the centre of the kernel are given more weight than those far away from the centre. This averaging is done on a channel-by-channel basis, and the average channel values become the new value for the pixel in the filtered image.


1 Answers

You can create a Gaussian kernel from scratch as noted in MATLAB documentation of fspecial. Please read the Gaussian kernel creation formula in the algorithms part in that page and follow the code below. The code is to create an m-by-n matrix with sigma = 1.

m = 5; n = 5; sigma = 1; [h1, h2] = meshgrid(-(m-1)/2:(m-1)/2, -(n-1)/2:(n-1)/2); hg = exp(- (h1.^2+h2.^2) / (2*sigma^2)); h = hg ./ sum(hg(:));  h =      0.0030    0.0133    0.0219    0.0133    0.0030     0.0133    0.0596    0.0983    0.0596    0.0133     0.0219    0.0983    0.1621    0.0983    0.0219     0.0133    0.0596    0.0983    0.0596    0.0133     0.0030    0.0133    0.0219    0.0133    0.0030 

Observe that this can be done by the built-in fspecial as follows:

fspecial('gaussian', [m n], sigma) ans =      0.0030    0.0133    0.0219    0.0133    0.0030     0.0133    0.0596    0.0983    0.0596    0.0133     0.0219    0.0983    0.1621    0.0983    0.0219     0.0133    0.0596    0.0983    0.0596    0.0133     0.0030    0.0133    0.0219    0.0133    0.0030 

I think it is straightforward to implement this in any language you like.

EDIT: Let me also add the values of h1 and h2 for the given case, since you may be unfamiliar with meshgrid if you code in C++.

h1 =      -2    -1     0     1     2     -2    -1     0     1     2     -2    -1     0     1     2     -2    -1     0     1     2     -2    -1     0     1     2  h2 =      -2    -2    -2    -2    -2     -1    -1    -1    -1    -1      0     0     0     0     0      1     1     1     1     1      2     2     2     2     2 
like image 51
petrichor Avatar answered Oct 02 '22 15:10

petrichor