up to Schedule & Notes

Separable Filters

Large Filters are Expensive

In 1D

Recall the zero-mean Gaussian: $$G(x) = {1 \over \sigma \sqrt{2 \pi}} e^{-{x^2 \over 2 \sigma^2}}$$

In 1D, this is (from dev.theomader.com/gaussian-kernel-calculator): $$\begin{array}{rlllllll} \textrm{width 3:} & & &0.279010 &0.441980 &0.279010 \\ \textrm{width 5:} & &0.061360 &0.244770 &0.387740 &0.244770 &0.061360 \\ \textrm{width 7:} &0.005980 &0.060626 &0.241843 &0.383103 &0.241843 &0.060626 &0.005980 \\ \end{array}$$

In 2D

In 2D, the zero-mean Gaussian is $$G(x,y) = {1 \over 2 \pi \sigma^2} e^{-{x^2+y^2 \over 2 \sigma^2}}$$

To approximate $G(x,y)$, we could use a Gaussian filter like this: $${1 \over 16} \left[\begin{array}{ccc} 1 & 2 & 1 \\ 2 & 4 & 2 \\ 1 & 2 & 1 \\ \end{array}\right]$$

Or we could use a bigger, better approximation: $${1 \over 273} \left[\begin{array}{ccc} 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{array}\right]$$

But a 5x5 filter is very expensive to apply: $25 M N$ operations for an $M \times N$ image.

Separability of Spatial Filters

A filter, $H$, is separable if it can be written as the convolution of two lower-dimensional filters: $H = H_1 ∗ H_2$.

Recall that convolution is associative: $I ∗ (H_1 ∗ H_2) = (I ∗ H_1) ∗ H_2$.

That means that we can apply $H_1$ to the image, then apply $H_2$.

The 2D Gaussian is separable: $$\begin{array}{rl} G(x,y) = & {1 \over 2 \pi \sigma^2} e^{-{ x²+y² \over 2 \sigma^2 }} \\ = & {1 \over \sigma \sqrt{2\pi}} e^{-{x^2 \over 2 \sigma^2}} \times {1 \over \sigma \sqrt{2\pi}} e^{-{y^2 \over 2 \sigma^2}} \\ = & G(x) \times G(y) \\ \end{array}$$

To apply the 2D Gaussian: $$\begin{array}{rl} I ∗ G(x,y) = & I \;\; ∗ \;\; (G(x) ∗ G(y)) \\ = & (I ∗ G(x)) \;\; ∗ \;\; G(y) \\ \end{array}$$

Execution cost:

Conclusion: Use separable filters where possible!

Run the "convolve" code to experiment with this.

up to Schedule & Notes