I need help normalizing a Gaussian kernel matrix to integer values

4.8k Views Asked by At

I am trying to understand the mathematics behind Canny edge detection, and the first step is to apply a Gaussian blur to the image you are working with. To do a Gaussian blur, you must obtain a Gaussian kernel matrix, whose values are obtained from the 2-d equation for a Gaussian distribution:

$$G(x,y) = \frac{1}{2\pi\sigma^2}e^{\frac{-x^2+y^2}{2\sigma^2}}$$

So say you are using a 5x5 matrix for your Gaussian kernel, then the center of the matrix would represent x = 0, y = 0, and the x and y values would change as you expect as you move away from the center of the matrix.

If I calculate this 5x5 kernel with $\sigma$ = 1, then I obtain a matrix that looks something like this:

\begin{bmatrix} 0.00292 & 0.01306 & 0.02154 & 0.01306 & 0.00292 \\ 0.01306 & 0.05855 & 0.09653 & 0.05855 & 0.01306 \\ 0.02154 & 0.09653 & 0.15915 & 0.09653 & 0.02154 \\ 0.01306 & 0.05855 & 0.09653 & 0.05855 & 0.01306 \\ 0.00292 & 0.01306 & 0.02154 & 0.01306 & 0.00292 \end{bmatrix}

where the matrix values get smaller as you move away from the center.

Looking at multiple resources for this online, http://homepages.inf.ed.ac.uk/rbf/HIPR2/gsmooth.htm https://www.cs.auckland.ac.nz/courses/compsci373s1c/PatricesLectures/Gaussian%20Filtering_1up.pdf

they obtain a normalized matrix with integer values that looks like this:

\begin{bmatrix} 1 & 4 & 7 & 4 & 1 \\ 4 & 16 & 26 & 16 & 4 \\ 7 & 26 & 41 & 26 & 7 \\ 4 & 16 & 26 & 16 & 4 \\ 1 & 4 & 7 & 4 & 1 \end{bmatrix}

I do not know how they went from the first matrix to the second. I am not really sure what to search for to look up what was done myself. Can anyone explain this to me, and assist me in finding further reading on this subject? I have always been intimidated in image processing when my instructors would talk about things like a Gaussian filter or Gaussian noise. If I could understand the underlying mathematics behind this, then I feel I would understand it a lot better.