# Image Filtering
Image filtering: compute the function value of local neighborhood at each pixel.
## Linear filters
For a linear system, each output is a linear combination of all the input values:
$
f[m, n]=\sum_{k, l} h[m, n, k, l] g[k, l]
$
In matrix form,
$
f=H g
$
where $g$ is the input image and $f$ is the output image, $H$ is the filtering operation.
Say, we want to replace each pixel with a weighted average of its neighborhood. The neighborhood mark is called a filter or a kernel. For example for weighted averaging of neighbors (box filtering):
$
f[m, n]=\sum_{k, l} g[k, l] h[m+k, n+l]
$
![[FILTER-g.jpg]]
![[output-boxfiltering.jpg]]
## Combining filters
Filtering operations can be combined linearly.
![[sharpening.jpg]]
## Handling borders
Lots of ways to handling boundary. Not an issue for high-res images.
![[border-filtering.jpg]]
## Edge filters
Horizontal edge filter: $[-1 1]$
$
\frac{\partial \mathrm{I}}{\partial x} \simeq \mathrm{I}(x, y)-\mathrm{I}(x-1, y)
$
![[horizontal-edge.jpg]]
Vertical edge filter: $[-1 1]^\top$
![[vertical-edge.jpg]]
## Sobel filters
Takes gradient of the image in horizontal or vertical direction.
![[sobel1.jpg]]
![[sobel2.jpg]]
## Gaussian filter
Weight contribution of neighboring pixels by nearness, controllable with the "sigma" parameter.
![[gaussianfilter.jpg]]
### Properties of Gaussian filter
1. Rotational symmetry treats features of all orientations equally (isotropy).
2. Efficient
Rule of thumb is kernel width $\geq 5 \sigma$
- Separable
- Cascadable: Approach to large $\sigma$ comes from identity
$
G_{\sigma_{1}} * G_{\sigma_{2}}=G_{\sqrt{\sigma_{1}^{2}+\sigma_{2}^{2}}}
$
3. Separability
4. The $2 \mathrm{D}$ Gaussian can be expressed as the product of two functions, one a function of $x$ and the other a function of $y$. In this case, the two functions are the (identical) 1D Gaussian
$
\begin{aligned}
G_{\sigma}(x, y) &=\frac{1}{2 \pi \sigma^{2}} \exp ^{-\frac{x^{2}+y^{2}}{2 \sigma^{2}}} \\
&=\left(\frac{1}{\sqrt{2 \pi} \sigma} \exp ^{-\frac{x^{2}}{2 \sigma^{2}}}\right) \quad\left(\frac{1}{\sqrt{2 \pi} \sigma} \exp ^{-\frac{y^{2}}{2 \sigma^{2}}}\right)
\end{aligned}
$
## Median filter
These are non-linear filters. If we have strong outliers, we prefer non-linear filters.
![[median-filtering.jpg]]
Problem - It can diminish the structure with large filter size.
## Bilateral filter
Non-linear filter. Smoothes image without causing excessive edge-blur. Uses gaussian on patches which is smooth, uses half/partial gaussian where patch is not smooth. Has two parameters.
$
B F[I]_{\mathrm{p}}=\frac{1}{W_{\mathrm{p}}} \sum_{\mathbf{q} \in S} G_{\sigma_{\mathrm{s}}}(\|\mathbf{p}-\mathbf{q}\|) \quad G_{\sigma_{\mathrm{r}}}\left(\left|I_{\mathrm{p}}-I_{\mathrm{q}}\right|\right) \quad I_{\mathbf{q}}
$
![[bilateral filtering.jpg]]
---
## References