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!
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} $$