OpenCV 双边滤波器算法详解

OpenCV中双边滤波器函数

void cv::bilateralFilter( InputArray _src, OutputArray _dst, int d,
                      double sigmaColor, double sigmaSpace,
                      int borderType )

函数各输入参数解释:

参数1  _src  为输入图像;参数2  _dst  为输出图像;

参数3 d 为核的大小,

d\left\{\begin{matrix} d , d>0 and d=2*n+1\\ d+1 ,d>0 and d=2*n\\ cvRound(sigma_space*1.5) , d<=0 \end{matrix}\right.

参数4  sigmaColor为值域(像素)的标准偏差,表示为\sigma _r

参数5  sigmaSpace为定义域的标准偏差,即高斯滤波中的偏差,表示为\sigma _d

参数6  borderType为边界类型;

 

算法详解

1. 填充图像边界

图像的上下左右边界各填充d/2行或列,下图为d=3为例进行说明。

\begin{pmatrix} a_{00} & a_{01} & ... &a_{0n-1} \\ a_{10}& a_{11} & ... &a_{1n-1} \\ ...& ... & ... &... \\ a_{n-10} & a_{n-11} & ... & a_{n-1n-1} \end{pmatrix}填充后为\begin{pmatrix} {\color{Red} a_{00}} &{\color{Red} a_{00}} & {\color{Red} a_{01}} & {\color{Red} ...} &{\color{Red} a_{0n-1}} &{\color{Red} a_{0n-1}} \\ {\color{Red} a_{00}} &a_{00} & a_{01} & ... &a_{0n-1} &{\color{Red} a_{0n-1}}\\{\color{Red} a_{10}} & a_{10}& a_{11} & ... &a_{1n-1} &{\color{Red} a_{1n-1}}\\ {\color{Red} ...}&...& ... & ... &... &{\color{Red} ...}\\ {\color{Red} a_{n-10}} &a_{n-10} & a_{n-11} & ... & a_{n-1n-1}& {\color{Red} a_{n-1n-1}} \\ {\color{Red} a_{n-10}} & {\color{Red} a_{n-11}} & {\color{Red} ...} & {\color{Red} a_{n-1n-1}}& {\color{Red} a_{n-1n-1}} & {\color{Red} a_{n-1n-1}}\end{pmatrix}

若d=5时,则填充2行,填充数据均为最外围数据,即红色像素在复制一行。

2. 值域(像素)的滤波器系数

color weight =\exp \left ( - \frac{(r_{k,l}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right )

当核为size(3,3)时,r为核内某像素的值,r0为核心中心像素的值。

\begin{matrix} r{_{-1,-1}} & r{_{-1,0}} & r{_{-1,1}}\\ r{_{0,-1}}& r{_{0,0}} &r{_{0,1}} \\ r{_{1,-1}} & r{_{1,0}} & r{_{1,1}} \end{matrix}

核内的值域滤波器系数为

\begin{matrix} \exp \left ( - \frac{(r_{-1,-1}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right ) & \exp \left ( - \frac{(r_{-1,0}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right ) & \exp \left ( - \frac{(r_{-1,1}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right )\\ \exp \left ( - \frac{(r_{0,-1}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right ) & \exp \left ( - \frac{(r_{0,0}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right ) &\exp \left ( - \frac{(r_{0,1}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right ) \\ \exp \left ( - \frac{(r_{1,-1}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right )& \exp \left ( - \frac{(r_{1,0}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right ) & \exp \left ( - \frac{(r_{1,1}-r_{0,0})^{2}}{2 \sigma _r^{2}} \right ) \end{matrix}

3. 定义域的滤波器系数

space weight =\exp \left ( - \frac{(i-0)^{2}+(j-0)^{2}}{2 \sigma _d^{2}} \right )

当核为size(d,d)时,行和列取值为-d/2...0...d/2,仍以d = 3为例说明

\begin{matrix} \left ( -1,-1 \right ) & \left ( -1,0 \right ) &\left ( -1,1 \right ) \\ \left ( 0,-1 \right ) & \left (0,0 \right ) &\left ( 0,1 \right ) \\ \left ( 1,-1 \right ) & \left (1,0 \right ) &\left ( 1,1 \right ) \end{matrix},核内的定义域滤波器系数为\begin{matrix} \exp \left ( - \frac{(-1-0)^{2}+(-1-0)^{2}}{2 \sigma _d^{2}} \right ) & \exp \left ( - \frac{(-1-0)^{2}+(0-0)^{2}}{2 \sigma _d^{2}} \right )&\exp \left ( - \frac{(-1-0)^{2}+(1-0)^{2}}{2 \sigma _d^{2}} \right ) \\ \exp \left ( - \frac{(0-0)^{2}+(-1-0)^{2}}{2 \sigma _d^{2}} \right ) & \exp \left ( - \frac{(0-0)^{2}+(0-0)^{2}}{2 \sigma _d^{2}} \right )&\exp \left ( - \frac{(0-0)^{2}+(1-0)^{2}}{2 \sigma _d^{2}} \right ) \\ \exp \left ( - \frac{(1-0)^{2}+(-1-0)^{2}}{2 \sigma _d^{2}} \right ) & \exp \left ( - \frac{(1-0)^{2}+(0-0)^{2}}{2 \sigma _d^{2}} \right )&\exp \left ( - \frac{(1-0)^{2}+(1-0)^{2}}{2 \sigma _d^{2}} \right ) \end{matrix}

3. 核内双边滤波权重为

\begin{matrix} r{_{\left ( -1,-1 \right )}}\times d{_{\left ( -1,-1 \right )}}& r{_{\left ( -1,0 \right )}}\times d{_{\left ( -1,0 \right )}} & r{_{\left ( -1,1 \right )}}\times d{_{\left ( -1,1 \right )}}\\ r{_{\left ( 0,-1 \right )}} \times d{_{\left ( 0,-1 \right )}}& r{_{\left ( 0,0 \right )}}\times d{_{\left ( 0,0 \right )}} & r{_{\left ( 0,1 \right )}}\times d{_{\left ( 0,1 \right )}}\\ r{_{\left ( 1,-1 \right )}} \times d{_{\left ( 1,-1 \right )}}& r{_{\left ( 1,0 \right )}}\times d{_{\left ( 1,0 \right )}} & r{_{\left ( 1,1 \right )}}\times d{_{\left ( 1,1 \right )}} \end{matrix},简化为\begin{matrix} w\left ( -1,-1 \right ) & w\left ( -1,0 \right ) &w\left ( -1,1 \right ) \\w \left ( 0,-1 \right ) & w\left (0,0 \right ) &w\left ( 0,1 \right ) \\ w\left ( 1,-1 \right ) &w \left (1,0 \right ) &w\left ( 1,1 \right ) \end{matrix}

4. a00处的像素值r(0,0),经过双边滤波后的值为g(0,0)。

a00处的邻域为OpenCV 双边滤波器算法详解_第1张图片

 g(0,0) = \frac{f_{\left ( -1,-1 \right )}w_{\left ( -1,-1 \right )}+f_{\left ( -1,0 \right )}w_{\left ( -1,0 \right )}+...+f_{\left ( 1,1 \right )}w_{\left (1,1 \right )}}{w_{\left ( -1,-1 \right )}+w_{\left ( -1,0 \right )}+...+w_{\left (1,1 \right )}}

 

具体的代码实现,请见下一条博文OpenCV 双边滤波代码实现。

 

参考:https://www.cnblogs.com/magic-428/p/9172406.html

你可能感兴趣的:(OpenCV)