0
$\begingroup$

I'm having trouble calculating the same values for a Gaussian filter kernel as those derived in the Canny edge detector Wikipedia page

It states:

The equation for a Gaussian filter kernel of size (2k+1)x(2k+1) is given by:

$$ H_{i,j} = \frac {1} {2\pi\sigma^2} \exp\left(- \frac {(i - (k + 1))^2 + (j - (k + 1)^2} {2\sigma^2}\right) \\ ; 1 \le i, j \le (2k + 1) $$

Here is an example of a 5×5 Gaussian filter, used to create the adjacent image, with $\sigma = 1$. (The asterisk denotes a convolution operation.)

$$ \textbf{B} = \frac{1}{159} \begin{bmatrix} 2 & 4 & 5 & 4 & 2 \\ 4 & 9 & 12 & 9 & 4 \\ 5 & 12 & 15 & 12 & 5 \\ 4 & 9 & 12 & 9 & 4 \\ 2 & 4 & 5 & 4 & 2 \\ \end{bmatrix} * \textbf{A} $$

My work for just the first term:

For a 5x5 filter, I calculate k using the equation $2k+1 = 5$: $$ k = \frac{5 - 1}{2} \rightarrow k = 2 $$

The range for $i, j$ now becomes $1 \le i, j \le 5$

For $i, j = (1, 1)$ & $\sigma = 1$:

$$ \begin{align*} H_{1,1} &= \frac {1} {2\pi\cdot1^2} \exp\left(- \frac {(1 - (2 + 1))^2 + (1 - (2 + 1)^2} {2\cdot1^2}\right)\\ &= \frac {1} {2\pi} \exp\left(- \frac {(-2)^2 + (-2)^2} {2}\right)\\ &= \frac {1} {2\pi} \exp\left(-4\right)\\ &= (0.1592...) \cdot (0.0183...)\\ &\approx 0.0029\\ \end{align*} $$

Looking at the result from the Wiki page for the top left-most term, it's equal to $\frac{1}{159}\cdot2 \approx 0.0126$

Applying my method to calculate the entire kernel I get: $$ \begin{bmatrix} 0.0029 & 0.0131 & 0.0215 & 0.0131 & 0.0029 \\ 0.0131 & 0.0585 & 0.0965 & 0.0585 & 0.0131 \\ 0.0215 & 0.0965 & 0.1592 & 0.0965 & 0.0215 \\ 0.0131 & 0.0585 & 0.0965 & 0.0585 & 0.0131 \\ 0.0029 & 0.0131 & 0.0215 & 0.0131 & 0.0029 \\ \end{bmatrix} $$

The kernel on the Wiki page resolves to: $$ \begin{bmatrix} 0.0126 & 0.0252 & 0.0314 & 0.0252 & 0.0126 \\ 0.0252 & 0.0566 & 0.0755 & 0.0566 & 0.0252 \\ 0.0314 & 0.0755 & 0.0943 & 0.0755 & 0.0314 \\ 0.0252 & 0.0566 & 0.0755 & 0.0566 & 0.0252 \\ 0.0126 & 0.0252 & 0.0314 & 0.0252 & 0.0126 \\ \end{bmatrix} $$

Both results are rounded to 4 decimal places. Am I doing something wrong in my calculations? Looking at the more general equation:

$$ G(x,y) = \frac{1}{2\pi\sigma^2}e^{-\frac{x^2 + y^2}{2\sigma^2}} $$ where $x, y$ are the horizontal and vertical distances from the center of the kernel, respectively. In the example above, this is accounted for by the $(i - (k + 1))$ and $(j - (k + 1))$ terms. Yet, all other terms are fixed or constants, so there must be something wrong with my understanding of the $i, j, k$ variables.

$\endgroup$
6
  • $\begingroup$ Check these out: dsp.stackexchange.com/questions/69619/… dsp.stackexchange.com/questions/54375/… $\endgroup$ Aug 20, 2020 at 21:42
  • $\begingroup$ @CedronDawg Thank you! Very informative stuff there. I stumbled on your dsprelated blog post but most of it went over my head, for now :P So if I understand correctly, I am actually computing the "true" values of the Gaussian kernel. What I am comparing it to (on the wiki page) is just an approximate integer representation? $\endgroup$
    – Nevermore
    Aug 20, 2020 at 22:34
  • $\begingroup$ "Just"??!! It's proud to be a quantized normalized sampling of the continuous Gaussian. Notice that the coefficients should add up to the divisor on the outside. Thus applying it to a level surface has no effect. Integer calculations tend to be much faster. My latest article is about the discrete vs continuous Gaussian, that undoubtedly has a 2D analog, but I haven't gotten there yet. $\endgroup$ Aug 20, 2020 at 22:45
  • $\begingroup$ Point taken! I don't think I'm quite at the level where I can fully appreciate what's going on with it, but I'll probably be digging deeper into your articles and your code to learn more. $\endgroup$
    – Nevermore
    Aug 21, 2020 at 0:02
  • $\begingroup$ My articles are mostly about the 1D DFT and parameter estimation. Image processing is a separate interest area that I haven't really written about yet. I do have a few in my queue, the first is about gradient estimation on a noisy surface. $\endgroup$ Aug 21, 2020 at 0:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.