Guided Filter
输入图p,导向图I, 输出图q,\(\Rightarrow q_i = \sum_jW_{ij}(I)p_j\),其中i,j为pixel index,\(W_{ij}\)是导向图I的一个函数,与输入图p独立。
假设输出图q在一个窗口\(w_i\)中是导向图I的相信变换,即:\(q_i = a_kI_i + b_k, \forall i \in w_k\)
这种模型保证了仅有在导向图I有边缘的时候,输出图也有边缘,\(\nabla q = a\nabla I\),原理可以按照https://sci-hubtw.hkvisa.net/10.1109/acv.2002.1182150
为了确定\(a_k,b_k\)系数,有如下的优化函数。
\(E(a_k, b_k) = \sum_{i \in \omega_k}((a_kI_i + b_k - p_i)^2 + \epsilon a_k^2)\)
最终得到\(a_k,b_k\)如下:
\(a_k = \frac{\frac{1}{\lvert\epsilon\rvert}\sum{i\in\omega_k(I_ip_i - \mu_k\overline{p}_k)}}{\sigma_k^2 + \omega}\)
\(b_k = \overline{p_k} - a_k\mu_k\)
以偏导的方式求解\(a_k,b_k\)
对于输出图q,可以认为是输入图p减去其噪声,\(q_i = p_i - n_i\),\(n_i\)表示噪声
对于输出图q与导向图I,有如下公式:\(\nabla q_i = a \nabla I_i\), \(q_i = aI_i + b\)
最小化输出图q与输入图p的差异,求解系数:
\(\frac{\partial E}{\partial a_k} = 2\sum_{i}^{N}((a_kI_i + b_k - p_i)I_i + \epsilon a_k)\)
\(\frac{\partial E}{\partial b_k} = -2\sum_{i}^{N}(p_i - a_kI_i - b_k)\)
求解\(b_k\)
\[\begin{aligned} &\ \frac{\partial E}{\partial b_k} = -2\sum_{i}^{N}(p_i - a_kI_i - b_k) = 0 \\ &\ \Rightarrow Nb_k = \sum_{i}^n(p_i - a_kI_i) \\ &\ \Rightarrow b_k = \frac{1}{N} \sum_{i}^n(p_i - a_kI_i) \\ &\ \Rightarrow b_k = \frac{1}{N} \sum_{i}^np_i - a_k\frac{1}{N} \sum_{i}^nI_i \\ &\ \Rightarrow b_k = p_k - a_k u_k \\ \end{aligned}\]
\(p_k\)和\(u_k\)是窗口\(w_i\)内,输入图\(p_i\)和导向图\(I_i\)的均值。
求解\(a_k\)
\[\begin{aligned} &\ \frac{\partial E}{\partial a_k} = 2\sum_{i}^{N}((a_kI_i + b_k - p_i)I_i + \epsilon a_k) = 0 \\ &\ \Rightarrow -\sum_{i}^Np_iI_i + \sum_{i}^Nb_kI_i + \sum_{i}^Na_kI_i^2 + \sum_{i}^N \epsilon a_k = 0 \\ &\ \Rightarrow \sum_{i}^Np_iI_i - \sum_{i}^Nb_kI_i = \sum_{i}^Na_kI_i^2 + \sum_{i}^2\epsilon a_k \\ &\ \Rightarrow \sum_{i}^Np_iI_i - \sum_{i}^N(p_k-a_ku_k)I_i = a_k\sum_{i}^N(I_i^2 + \epsilon) \\ &\ \Rightarrow \sum_{i}^Np_iI_i - \sum_{i}^Np_kI_i + \sum_{i}^Na_ku_kI_i = a_k\sum_{i}^N(I_i^2 + \epsilon) \\ &\ \Rightarrow \sum_{i}^Np_iI_i - p_k\sum_{i}^NI_i = a_k\sum_{i}^N(I_i^2 + \epsilon - u_kI_i) \\ &\ \Rightarrow a_k = \frac{\sum_{i}^Np_iI_i - p_k\sum_{i}^NI_i}{\sum_{i}^N(I_i^2 + \epsilon - u_kI_i)} \\ &\ \Rightarrow a_k = \frac{\sum_{i}^Np_iI_i - \frac{1}{N}\sum_{i}^Np_i\sum_{i}^NI_i}{\sum_{i}^NI_i^2 - \frac{1}{N}\sum_{i}^NI_i\sum_{i}^NI_i + \epsilon} \\ &\ \Rightarrow a_k = \frac{\frac{1}{N}\sum_{i}^Np_iI_i - \frac{1}{N}\sum_{i}^Np_i\frac{1}{N}\sum_{i}^NI_i}{\frac{1}{N}\sum_{i}^NI_i^2 - \frac{1}{N}\sum_{i}^NI_i\frac{1}{N}\sum_{i}^NI_i + \epsilon} \\ \end{aligned}\]
根据协方差公式
\(var(X) = \frac{\sum_{i}^{N}(X_i-\overline X)(X_i - \overline X)}{n-1}\)
\(cov(X, Y) = E[(X - E[X])(Y - E[Y])]\)
\(cov(X, Y) = E[XY] - E[X][Y]\)
\(a_k = \frac{cov(p_i, I_i)}{\sigma_k^2 + \epsilon}\)
1 | cv::Mat guidedFilter(cv::Mat I, cv::Mat p, int r, double eps) |