bilareralFilter双边滤波函数
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+rfor 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));endwaitbar(i/m); end close(h)figure; imshow(mat2gray(imgn(r+1:m+r,r+1:n+r)));opencv代碼: #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; }
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的bilareralFilter双边滤波函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv中imgproc库中函数详解
- 下一篇: blur 简单滤波