Guided filter

From HandWiki
Short description: Edge-preserving smoothing image filter

}}

A guided filter is an edge-preserving smoothing image filter. As with a bilateral filter, it can filter out noise or texture while retaining sharp edges.[1][2]

Comparison

Compared to the bilateral filter, the guided image filter has two advantages: bilateral filters have high computational complexity, while the guided image filter uses simpler calculations with linear computational complexity. Bilateral filters sometimes include unwanted gradient reversal artifacts and cause image distortion. The guided image filter is based on linear combination, making the output image consistent with the gradient direction of the guidance image, preventing gradient reversal.

Definition

One key assumption of the guided filter is that the relation between guidance [math]\displaystyle{ I }[/math] and the filtering output [math]\displaystyle{ q }[/math] is linear. Suppose that [math]\displaystyle{ q }[/math] is a linear transformation of [math]\displaystyle{ I }[/math] in a window [math]\displaystyle{ \omega_k }[/math] centered at the pixel [math]\displaystyle{ k }[/math].

In order to determine the linear coefficient [math]\displaystyle{ (a_k, b_k) }[/math], constraints from the filtering input [math]\displaystyle{ p }[/math] are required. The output [math]\displaystyle{ q }[/math] is modeled as the input [math]\displaystyle{ p }[/math] with unwanted components [math]\displaystyle{ n }[/math], such as noise/textures subtracted.

The basic model:

(1)  [math]\displaystyle{ q_i = a_k I_i + b_k, \forall i \in \omega_k }[/math]

(2)  [math]\displaystyle{ q_{i} = p_{i} - n_{i} }[/math]

in which:

[math]\displaystyle{ q_{i} }[/math] is the [math]\displaystyle{ i_{th} }[/math] output pixel;
[math]\displaystyle{ p_i }[/math] is the [math]\displaystyle{ i_{th} }[/math] input pixel;
[math]\displaystyle{ n_{i} }[/math] is the [math]\displaystyle{ i_{th} }[/math] pixel of noise components;
[math]\displaystyle{ I_i }[/math] is the [math]\displaystyle{ i_{th} }[/math] guidance image pixel;
[math]\displaystyle{ (a_k, b_k) }[/math] are some linear coefficients assumed to be constant in [math]\displaystyle{ \omega_k }[/math].

The reason to use a linear combination is that the boundary of an object is related to its gradient. The local linear model ensures that [math]\displaystyle{ q }[/math] has an edge only if [math]\displaystyle{ I }[/math] has an edge, since [math]\displaystyle{ \nabla q = a \nabla I }[/math].

Subtract (1) and (2) to get formula (3);At the same time, define a cost function (4):

(3)  [math]\displaystyle{ n_{i} = p_{i} - a_k I_{i} - b_k }[/math]

(4)  [math]\displaystyle{ E(a_{k},b_{k})=\sum_{i{\epsilon}{\omega}_{k}}^{}((a_{k}I_{i} + b_{k} - p_{i})^{2} + {\epsilon}a_{k}^{2}) }[/math]

in which

[math]\displaystyle{ \epsilon }[/math] is a regularization parameter penalizing large [math]\displaystyle{ a_{k} }[/math];
[math]\displaystyle{ \omega_{k} }[/math] is a window centered at the pixel [math]\displaystyle{ k }[/math].

And the cost function's solution is:

(5)  [math]\displaystyle{ a_{k} = \frac{\frac{1}{\left|\omega\right|}\sum_{i\epsilon\omega_{k}}I_{i}p_{i} - \mu_{k}\bar{p_{k}}}{\sigma^{2}_{k}+\epsilon} }[/math]

(6)  [math]\displaystyle{ b_{k} = \bar{p_{k}} - a_{k}\mu_{k} }[/math]

in which

[math]\displaystyle{ \mu_{k} }[/math] and [math]\displaystyle{ \sigma^{2}_{k} }[/math] are the mean and variance of [math]\displaystyle{ I }[/math] in [math]\displaystyle{ \omega_{k} }[/math];
[math]\displaystyle{ \left|\omega\right| }[/math] is the number of pixels in [math]\displaystyle{ \omega_{k} }[/math];
[math]\displaystyle{ \bar{p}_{k} = \frac{1}{\left|\omega\right|}\sum_{i\epsilon\omega_{k}}p_{i} }[/math] is the mean of [math]\displaystyle{ p }[/math] in [math]\displaystyle{ \omega_{k} }[/math].

After obtaining the linear coefficients [math]\displaystyle{ (a_k, b_k) }[/math], the filtering output [math]\displaystyle{ q_i }[/math] is provided by the following algorithm:

Algorithm

By definition, the algorithm can be written as:

Algorithm 1. Guided Filter

input: filtering input image [math]\displaystyle{ p }[/math] ,guidance image [math]\displaystyle{ I }[/math] ,window radius [math]\displaystyle{ r }[/math] ,regularization [math]\displaystyle{ \epsilon }[/math]

output: filtering output [math]\displaystyle{ q }[/math]

1.

[math]\displaystyle{ mean_{I} }[/math] = [math]\displaystyle{ f_{mean}(I) }[/math]
[math]\displaystyle{ mean_{p} }[/math] = [math]\displaystyle{ f_{mean}(p) }[/math]
[math]\displaystyle{ corr_{I} }[/math] = [math]\displaystyle{ f_{mean}(I.*I) }[/math]
[math]\displaystyle{ corr_{Ip} }[/math] = [math]\displaystyle{ f_{mean}(I.*p) }[/math]

2.

[math]\displaystyle{ var_{I} }[/math] = [math]\displaystyle{ corr_{I} - mean_{I.} * mean_{I} }[/math]
[math]\displaystyle{ cov_{Ip} }[/math] = [math]\displaystyle{ corr_{Ip} - mean_{I.} * mean_{p} }[/math]

3.

[math]\displaystyle{ a }[/math] = [math]\displaystyle{ cov_{Ip}./(var_{I} + \epsilon) }[/math]
[math]\displaystyle{ b }[/math] = [math]\displaystyle{ mean_{p} - a. * mean_{I} }[/math]

4.

[math]\displaystyle{ mean_{a} }[/math] = [math]\displaystyle{ f_{mean}(a) }[/math]
[math]\displaystyle{ mean_{b} }[/math] = [math]\displaystyle{ f_{mean}(b) }[/math]

5.

[math]\displaystyle{ q }[/math] = [math]\displaystyle{ mean_{a.} * I + mean_{b} }[/math]

[math]\displaystyle{ f_{mean} }[/math] is a mean filter with a wide variety of O(N) time methods.

Properties

Edge-preserving filtering

When the guidance image [math]\displaystyle{ I }[/math] is the same as the filtering input [math]\displaystyle{ p }[/math]. The guided filter removes noise in the input image while preserving clear edges.

Specifically, a “flat patch” or a “high variance patch” can be specified by the parameter [math]\displaystyle{ \epsilon }[/math] of the guided filter. Patches with variance much lower than the parameter [math]\displaystyle{ \epsilon }[/math] will be smoothed, and those with variances much higher than [math]\displaystyle{ \epsilon }[/math] will be preserved. The role of the range variance [math]\displaystyle{ \sigma_r^2 }[/math] in the bilateral filter is similar to [math]\displaystyle{ \epsilon }[/math] in the guided filter. Both of them define the edge/high variance patches that should be kept and noise/flat patches that should be smoothed.”

Gradient-preserving filtering

When using the bilateral filter to filter an image, artifacts may appear on the edges. This is because of the pixel value's abrupt change on the edge. These artifacts are inherent and hard to avoid, because edges appear in all kinds of pictures.

The guided filter performs better in avoiding gradient reversal. Moreover, in some cases, it can be ensured that gradient reversal does not occur.

Structure-transferring filtering

Due to the local linear model of [math]\displaystyle{ q = aI + b }[/math], it is possible to transfer the structure from the guidance [math]\displaystyle{ I }[/math] to the output [math]\displaystyle{ q }[/math]. This property enables some special filtering-based applications, such as feathering, matting and dehazing.

Implementations

See also

References