0
$\begingroup$

I am seeking to compute a kernel radius to use with my gaussian convolution filter, and inspired by https://stackoverflow.com/a/68050503/, I came up with:

$$r=\sqrt{-2\sigma^2\ln\left( \epsilon\sigma\sqrt{2\pi} \right)}$$

This is basically inverting the normalized gaussian, setting $g(x,\sigma)=\epsilon$ and solving for $x$ given $\sigma$ and $\epsilon$.

However, I noticed that with $\epsilon=0.01$ at about $\sigma>22$ my kernel radius computation started get smaller instead of bigger, and at $\sigma>40$ it went completely undefined (NaN).

So I re-derived this little formula (derivation at bottom) and graphed it in desmos, and mysteriously, it indeed showed a graph that peaked at about $20$ and fell to $0$ at about $40$ and was thereafter was undefined!

Screenshot of graph in desmos; peaks at about 25ish, falls to zero at about 40ish, and goes undefined after that!

Here is the desmos graph, with multiple steps through the derivation, all showing the same graph (only the first is activated though): https://www.desmos.com/calculator/ucrqzrahxs

Where is my math wrong?


Derivation:

$$\begin{eqnarray} g(x,\sigma)&=&\frac{1}{\sigma\sqrt{2\pi}} e^{\frac{-x^2}{2\sigma^2}} &\hspace{5em}\text{Normalized Guassian function.}\\ g(r,\sigma)&=&\frac{1}{\sigma\sqrt{2\pi}} e^{\frac{-r^2}{2\sigma^2}} &\hspace{5em}\text{Set radius to x.} \\ \epsilon&=&g(r,\sigma) &\hspace{5em}\text{Set }\epsilon\text{ to output} \\ \epsilon&=&\frac{1}{\sigma\sqrt{2\pi}} e^{\frac{-r^2}{2\sigma^2}} &\hspace{5em}\text{Replace }g(r,\sigma). \\ \ln \epsilon&=& \ln \left[\frac{1}{\sigma\sqrt{2\pi}} e^{\frac{-r^2}{2\sigma^2}}\right] &\hspace{5em}\text{Log both sides.} \\ \ln \epsilon&=& \ln \left[\frac{1}{\sigma\sqrt{2\pi}} \right]-\frac{r^2}{2\sigma^2} &\hspace{5em}\text{Get rid of }e. \\ \frac{r^2}{2\sigma^2} + \ln \epsilon&=& \ln \left[\frac{1}{\sigma\sqrt{2\pi}} \right] &\hspace{5em}\text{Move x to the left hand side.} \\ \frac{r^2}{2\sigma^2}&=& \ln \left[\frac{1}{\sigma\sqrt{2\pi}} \right] - \ln \epsilon &\hspace{5em}\text{Move }\epsilon\text{ to the right hand side.} \\ \frac{r^2}{2\sigma^2}&=& \ln \left[\frac{1}{\epsilon\sigma\sqrt{2\pi}} \right] &\hspace{5em}\text{Move }\epsilon\text{ into the log function.} \\ r^2&=& 2\sigma^2\ln \left[\frac{1}{\epsilon\sigma\sqrt{2\pi}} \right] &\hspace{5em}\text{Move the denominator to the right.} \\ r &=& \sqrt{2\sigma^2\ln \left[\frac{1}{\epsilon\sigma\sqrt{2\pi}} \right]} &\hspace{5em}\text{Square root both sides.} \\ r &=& \sqrt{-2\sigma^2\ln \left[\epsilon\sigma\sqrt{2\pi} \right]} &\hspace{5em}\text{Flip the fraction inside the logarithm.} \\ \end{eqnarray} $$

$\endgroup$

1 Answer 1

0
$\begingroup$

I think this should be correct, because when $\sigma$ increases, the peak of the Gaussian curve becomes smaller and eventually less than 0.01.

Edit: Indeed, this is the curve for $\sigma=39$ (red) and for $\sigma=41$ (blue). The latter stays below 0.01

enter image description here

$\endgroup$
2
  • $\begingroup$ ~~Any better idea on how to approximate a kernel radius?~~ I'll just use 3x sigma as suggested in the other answer. $\endgroup$
    – multiscale
    Commented Nov 13, 2022 at 21:09
  • $\begingroup$ 3$\sigma$ is usually a good guess, but it really depends what you need it for. Otherwise you can retry your approach using $g(x,\sigma)/g(0,\sigma)=\epsilon$ $\endgroup$
    – Vito
    Commented Nov 13, 2022 at 21:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.