Difference between revisions of "Spatially Varying Blur"

From Inkscape Wiki
Jump to navigation Jump to search
(Start)
 
Line 19: Line 19:
0&=2\left[G(\sigma,\sigma_1)-\,G(\sigma,\sigma_2)\right]-2(1-a)G(\sigma_1,\sigma_1)+2(1-2a)G(\sigma_1,\sigma_2)+2a\,G(\sigma_2,\sigma_2)
0&=2\left[G(\sigma,\sigma_1)-\,G(\sigma,\sigma_2)\right]-2(1-a)G(\sigma_1,\sigma_1)+2(1-2a)G(\sigma_1,\sigma_2)+2a\,G(\sigma_2,\sigma_2)
\\0&=G(\sigma,\sigma_1)-\,G(\sigma,\sigma_2)-G(\sigma_1,\sigma_1)+a\,G(\sigma_1,\sigma_1)+G(\sigma_1,\sigma_2)-2a\,G(\sigma_1,\sigma_2)+a\,G(\sigma_2,\sigma_2)
\\0&=G(\sigma,\sigma_1)-\,G(\sigma,\sigma_2)-G(\sigma_1,\sigma_1)+a\,G(\sigma_1,\sigma_1)+G(\sigma_1,\sigma_2)-2a\,G(\sigma_1,\sigma_2)+a\,G(\sigma_2,\sigma_2)
\\a&=\frac{G(\sigma_1,\sigma_1)-G(\sigma_1,\sigma_2)-(G(\sigma,\sigma_1)-G(\sigma,\sigma_2))}{G(\sigma_1,\sigma_1)-2\,G(\sigma_1,\sigma_2)+G(\sigma_2,\sigma_2)}
\\a&=\frac{G(\sigma,\sigma_2)-G(\sigma,\sigma_1)+G(\sigma_1,\sigma_1)-G(\sigma_1,\sigma_2)}{G(\sigma_1,\sigma_1)-2\,G(\sigma_1,\sigma_2)+G(\sigma_2,\sigma_2)}
.\end{align}</math><!-- Verified numerically for s,s1,s2=2,1,3 -->
.\end{align}</math><!-- Verified numerically for s,s1,s2=2,1,3 -->


For reference:
This uses


<math>g(\sigma)=\frac{1}{\sqrt{2\pi\sigma^2}}e^{-x^2/(2\sigma^2)}</math>
<math>\begin{align}
g(\sigma)&=\frac{1}{\sqrt{2\pi\sigma^2}}e^{-x^2/(2\sigma^2)}
\\G(\sigma_a,\sigma_b)&=\frac{1}{\sqrt{2\pi(\sigma_a^2+\sigma_b^2)}}
.\end{align}</math>


<math>G(\sigma_a,\sigma_b)=\frac{1}{\sqrt{2\pi(\sigma_a^2+\sigma_b^2)}}</math>
<!-- I think it's probably just best to use feComponentTransfer with type=table
 
Filling in and simplifying we get
 
<math>\begin{align}
a&=\frac{G(\sigma,\sigma_2)-G(\sigma,\sigma_1)+G(\sigma_1,\sigma_1)-G(\sigma_1,\sigma_2)}{G(\sigma_1,\sigma_1)-2\,G(\sigma_1,\sigma_2)+G(\sigma_2,\sigma_2)}
\\&=\frac{\frac{1}{\sqrt{2\pi(\sigma^2+\sigma_2^2)}}-\frac{1}{\sqrt{2\pi(\sigma^2+\sigma_1^2)}}+\frac{1}{\sqrt{2\pi(\sigma_1^2+\sigma_1^2)}}-\frac{1}{\sqrt{2\pi(\sigma_1^2+\sigma_2^2)}}}{\frac{1}{\sqrt{2\pi(\sigma_1^2+\sigma_1^2)}}-2\,\frac{1}{\sqrt{2\pi(\sigma_1^2+\sigma_2^2)}}+\frac{1}{\sqrt{2\pi(\sigma_2^2+\sigma_2^2)}}}
\\&=\frac{\frac{1}{\sqrt{\sigma^2+\sigma_2^2}}-\frac{1}{\sqrt{\sigma^2+\sigma_1^2}}+\frac{1}{\sqrt{\sigma_1^2+\sigma_1^2}}-\frac{1}{\sqrt{\sigma_1^2+\sigma_2^2}}}{\frac{1}{\sqrt{\sigma_1^2+\sigma_1^2}}-2\,\frac{1}{\sqrt{\sigma_1^2+\sigma_2^2}}+\frac{1}{\sqrt{\sigma_2^2+\sigma_2^2}}}
.\end{align}</math>
-->

Revision as of 11:32, 26 April 2010

Spatially varying blur can be important for a number of effects, not the least of which is depth-of-field. Unfortunately SVG does not support this directly and we need to make some approximation of the effect.

One possibility is to do a simple linear interpolation between two blurs, but this tends to give an effective kernel which is only a very poor fit to the true kernel desired, it tends to give a much too sharp result.

This leads to wanting to create a better fitting interpolation between the two blurs.

L2-minimized fit

In general the easiest global error metric to minimize is the l2-norm. This is simply (the root of) the integral of the squared error. Which in this case can be formulated as ([math]\displaystyle{ g(\sigma) }[/math] is the kernel of a Gaussian blur with standard deviation [math]\displaystyle{ \sigma }[/math], [math]\displaystyle{ G(\sigma_a,\sigma_b) }[/math] is the integral of the product of two kernels, [math]\displaystyle{ \sigma_1\lt \sigma\lt \sigma_2 }[/math] and [math]\displaystyle{ 0\lt a\lt 1 }[/math])

[math]\displaystyle{ \begin{align} e(\sigma,\sigma_1,\sigma_2,a)&=\int_{-\infty}^{\infty}\left[g(\sigma)-\left((1-a)g(\sigma_1)+a\,g(\sigma_2)\right)\right]^2dx \\&=\int_{-\infty}^{\infty}g(\sigma)^2-2g(\sigma)\left((1-a)g(\sigma_1)+a\,g(\sigma_2)\right)+\left((1-a)g(\sigma_1)+a\,g(\sigma_2)\right)^2dx \\&=G(\sigma,\sigma)-2\left[(1-a)G(\sigma,\sigma_1)+a\,G(\sigma,\sigma_2)\right]+(1-a)^2G(\sigma_1,\sigma_1)+2a(1-a)G(\sigma_1,\sigma_2)+a^2\,G(\sigma_2,\sigma_2) .\end{align} }[/math]

Equating the derivative to zero (in order to minimize the quadratic equation in [math]\displaystyle{ a }[/math]) we get

[math]\displaystyle{ \begin{align} 0&=2\left[G(\sigma,\sigma_1)-\,G(\sigma,\sigma_2)\right]-2(1-a)G(\sigma_1,\sigma_1)+2(1-2a)G(\sigma_1,\sigma_2)+2a\,G(\sigma_2,\sigma_2) \\0&=G(\sigma,\sigma_1)-\,G(\sigma,\sigma_2)-G(\sigma_1,\sigma_1)+a\,G(\sigma_1,\sigma_1)+G(\sigma_1,\sigma_2)-2a\,G(\sigma_1,\sigma_2)+a\,G(\sigma_2,\sigma_2) \\a&=\frac{G(\sigma,\sigma_2)-G(\sigma,\sigma_1)+G(\sigma_1,\sigma_1)-G(\sigma_1,\sigma_2)}{G(\sigma_1,\sigma_1)-2\,G(\sigma_1,\sigma_2)+G(\sigma_2,\sigma_2)} .\end{align} }[/math]

This uses

[math]\displaystyle{ \begin{align} g(\sigma)&=\frac{1}{\sqrt{2\pi\sigma^2}}e^{-x^2/(2\sigma^2)} \\G(\sigma_a,\sigma_b)&=\frac{1}{\sqrt{2\pi(\sigma_a^2+\sigma_b^2)}} .\end{align} }[/math]