C++: void **bilateralFilte**r(InputArray src, OutputArray dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT )¶
表示对图像进行双边滤波,它能很好地对图像进行滤波,去除噪声的能力很好,并且增强边缘,但是运行慢相对于其他的滤波器
参数的详解:
src:表示输入的图像
OutputArray dst:表示输出的图像
int类型的d,表示在过滤过程中每个像素邻域的直径。如果这个值我们设其为非正数,那么OpenCV会从第五个参数sigmaSpace来计算出它来。
double类型的sigmaColor,颜色空间滤波器的sigma值。这个参数的值越大,就表明该像素邻域内有更宽广的颜色会被混合到一起,产生较大的半相等颜色区域。
double类型的sigmaSpace坐标空间中滤波器的sigma值,坐标空间的标注方差。他的数值越大,意味着越远的像素会相互影响,从而使更大的区域足够相似的颜色获取相同的颜色。当d>0,d指定了邻域大小且与sigmaSpace无关。否则,d正比于sigmaSpace。
int类型的borderType,用于推断图像外部像素的某种边界模式。注意它有默认值BORDER_DEFAULT。
简单地说它是以被处理像素与其周围像素的距离作为权重而进行的一种加权平均过程。
高斯距离权值为:
其中,d(ξ,x)=d(ξ-x)=||ξ-x||表示的是两个像素ξ和x之间的距离。
但该权值仅仅考虑的是距离,而对像素本身的亮度信息没有考虑,因此高斯滤波的结果是使整幅图像都模糊了,即图像的边缘信息(高频部分)被严重削弱了。我们知道当图像中邻域像素亮度与被处理像素的亮度差异很大时,邻域像素与该像素的关系很小的,即两者相似性很差。因此双边滤波还考虑了领域像素的亮度信息,通过计算相似度来赋予领域像素一定的权重,下面就是高斯相似度的权值:
其中,σ(f(ξ),f(x))=||f(ξ)-f(x)||表示两个像素亮度之差。最终的双边滤波的公式为:
其中,,Σ表示的是邻域范围,及滤波内核大小,f(ξ)表示原图。
代码详解:
<ol start="1" class="dp-cpp" style="padding: 0px; border: none; color: rgb(92, 92, 92); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; line-height: 26px; margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> cv::adaptiveBilateralFilter( InputArray _src, OutputArray _dst, Size ksize, </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">double</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> sigmaSpace, </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">double</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> maxSigmaColor, Point anchor, </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> borderType ) </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">{ </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//得到输入图像矩阵和与其尺寸类型一致的输出图像矩阵</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Mat src = _src.getMat(); </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> _dst.create(src.size(), src.type()); </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Mat dst = _dst.getMat(); </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//该算法只能处理8位二进制的灰度图像和三通道的彩色图像</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3); </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//得到滤波内核的锚点</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> anchor = normalizeAnchor(anchor,ksize); </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">( src.depth() == CV_8U ) </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> adaptiveBilateralFilter_8u( src, dst, ksize, sigmaSpace, maxSigmaColor, anchor, borderType ); </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">else</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> CV_Error( CV_StsUnsupportedFormat, </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"Adaptive Bilateral filtering is only implemented for 8u images"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span></li></ol>在这里我们以处理 8 为二进制图像为例,因此它调用了 bilateralFilter_8u 函数:
Opencv中的双边滤波的源码分析就到这里,另外根据Opencv的文档中介绍,两个高斯公式的σ可以相同,而且如果σ小于10,则滤波效果不明显,如果大于150,则会有强烈的卡通效果。当实时处理时,内核尺寸d推荐为5;如果在非实时处理情况下,而且有较强的噪声时,d为9效果会较好。
MATLAB代码:
clear all; close all; clc; img=imread('lena.jpg'); img=mat2gray(img); [m n]=size(img); imshow(img); r=10; %模板半径 imgn=zeros(m+2*r+1,n+2*r+1); imgn(r+1:m+r,r+1:n+r)=img; imgn(1:r,r+1:n+r)=img(1:r,1:n); %扩展上边界 imgn(1:m+r,n+r+1:n+2*r+1)=imgn(1:m+r,n:n+r); %扩展右边界 imgn(m+r+1:m+2*r+1,r+1:n+2*r+1)=imgn(m:m+r,r+1:n+2*r+1); %扩展下边界 imgn(1:m+2*r+1,1:r)=imgn(1:m+2*r+1,r+1:2*r); %扩展左边界 sigma_d=2; sigma_r=0.1; [x,y] = meshgrid(-r:r,-r:r); w1=exp(-(x.^2+y.^2)/(2*sigma_d^2)); %以距离作为自变量高斯滤波器 h=waitbar(0,'wait...'); for i=r+1:m+r for j=r+1:n+r w2=exp(-(imgn(i-r:i+r,j-r:j+r)-imgn(i,j)).^2/(2*sigma_r^2)); %以周围和当前像素灰度差值作为自变量的高斯滤波器 w=w1.*w2; s=imgn(i-r:i+r,j-r:j+r).*w; imgn(i,j)=sum(sum(s))/sum(sum(w)); end waitbar(i/m); end close(h) figure; imshow(mat2gray(imgn(r+1:m+r,r+1:n+r)));
#include "cv.h" #include "highgui.h" #include <iostream> using namespace std; using namespace cv; int main(int argc, char* argv[]) { Mat src = imread("misaka.jpg"); Mat dst; //参数是按顺序写的 //高斯滤波 //src:输入图像 //dst:输出图像 //Size(5,5)模板大小,为奇数 //x方向方差 //Y方向方差 GaussianBlur(src,dst,Size(5,5),0,0); imwrite("gauss.jpg",dst); //中值滤波 //src:输入图像 //dst::输出图像 //模板宽度,为奇数 medianBlur(src,dst,3); imwrite("med.jpg",dst); //均值滤波 //src:输入图像 //dst:输出图像 //模板大小 //Point(-1,-1):被平滑点位置,为负值取核中心 blur(src,dst,Size(3,3),Point(-1,-1)); imwrite("mean.jpg",dst); //双边滤波 //src:输入图像 //dst:输入图像 //滤波模板半径 //颜色空间标准差 //坐标空间标准差 bilateralFilter(src,dst,5,10.0,2.0); imwrite("bil.jpg",dst); waitKey(); return 0; }