标签:
一,噪声的介绍和卷积
二、各个滤波函数的解读,定义与源代码
三、综合所有的滤波,加滑动条控制核大小来blur
四、Matlab 辅助表达
一,噪声的介绍
图像噪声是图像在摄取或传输时所受的随机信号干扰,是图像中各种妨碍人们对其信息接受的因素。很多时候将图像噪声看成是多维随机过程,因而描述噪声的方法完全可以借用随机过程的描述,即用其概率分布函数和概率密度分布函数。我们常看到的就是淑艳噪声 salt&pepper,这里对噪声有理论的介绍http://blog.csdn.net/qq_20823641/article/details/51513567,可以学习一下。
二、各个滤波函数的解读,定义与源代码
这里通说噪声分为线性和非线性,官方中给了BoxBlur,Blur,GaussianBlur,medianBlur,bilateralBlur,其中方框滤波和均值滤波有相同有不同,准备说均值滤波是方框滤波的特殊,从函数BoxBlur与Blur也可以看出,下面的图也是一种解释
滤波器:可以说滤波器是一个包含加权系数的窗口,当使用这个滤波器平滑处理图像时,就把这个窗口放到图像上,透过这个窗口来看图像,有时候也叫做核函数 kernel,相信经常看见这个词语。
其中在看到这个窗口的时候我建议大家看看卷积的概念,http://baike.baidu.com/link?url=lQxRSr-C41IoYNqYyOwXE1FfsufcuptxFhBE2AJexoL9bdhSVOaRppD7f4r8T8C5DAR8ggGgQVXrgMmHr-PfJq
<span style="font-size:18px;">C++: void boxFilter(InputArray src,OutputArray dst, int ddepth, Size ksize, Point anchor=Point(-1,-1), boolnormalize=true, int borderType=BORDER_DEFAULT ) </span>
<span style="font-size:18px;">void cv::blur( InputArray src, OutputArray dst, Size ksize, Point anchor, int borderType ) { boxFilter( src, dst, -1, ksize, anchor, true, borderType ); } </span>
<span style="font-size:18px;">cv::Ptr<cv::FilterEngine> cv::createBoxFilter( int srcType, int dstType, Size ksize, Point anchor, bool normalize, int borderType ) { int sdepth = CV_MAT_DEPTH(srcType); int cn = CV_MAT_CN(srcType), sumType = CV_64F; if( sdepth <= CV_32S && (!normalize || ksize.width*ksize.height <= (sdepth == CV_8U ? (1<<23) : sdepth == CV_16U ? (1 << 15) : (1 << 16))) ) sumType = CV_32S; sumType = CV_MAKETYPE( sumType, cn ); Ptr<BaseRowFilter> rowFilter = getRowSumFilter(srcType, sumType, ksize.width, anchor.x ); Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType, dstType, ksize.height, anchor.y, normalize ? 1./(ksize.width*ksize.height) : 1); return Ptr<FilterEngine>(new FilterEngine(Ptr<BaseFilter>(0), rowFilter, columnFilter, srcType, dstType, sumType, borderType )); }</span>
<span style="font-size:18px;">C++: void GaussianBlur(InputArray src,OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, intborderType=BORDER_DEFAULT ) </span>
<span style="font-size:18px;">C++: void medianBlur(InputArray src,OutputArray dst, int ksize) </span>
<span style="font-size:18px;">C++: void bilateralFilter(InputArray src, OutputArraydst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT) </span>
<span style="font-size:18px;">#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> using namespace std; using namespace cv; Mat g_srcImage,g_dstImage1,g_dstImage2,g_dstImage3,g_dstImage4,g_dstImage5; int g_nBoxFilterValue=6; //方框滤波内核值 int g_nMeanBlurValue=10; //均值滤波内核值 int g_nGaussianBlurValue=6; //高斯滤波内核值 int g_nMedianBlurValue=10; //中值滤波参数值 int g_nBilateralFilterValue=10; //双边滤波参数值 static void on_BoxFilter(int, void *); //方框滤波 static void on_MeanBlur(int, void *); //均值块滤波器 static void on_GaussianBlur(int, void *); //高斯滤波器 static void on_MedianBlur(int, void *); //中值滤波器 static void on_BilateralFilter(int, void *); //双边滤波器 int main( ) { g_srcImage = imread( "lena.jpg", 1 ); g_dstImage1 = g_srcImage.clone( ); g_dstImage2 = g_srcImage.clone( ); g_dstImage3 = g_srcImage.clone( ); g_dstImage4 = g_srcImage.clone( ); g_dstImage5 = g_srcImage.clone( ); namedWindow("【<0>原图窗口】", 1); imshow("【<0>原图窗口】",g_srcImage); namedWindow("【<1>方框滤波】", 1); createTrackbar("内核值:", "【<1>方框滤波】",&g_nBoxFilterValue, 50,on_BoxFilter ); on_MeanBlur(g_nBoxFilterValue,0); imshow("【<1>方框滤波】", g_dstImage1); namedWindow("【<2>均值滤波】", 1); createTrackbar("内核值:", "【<2>均值滤波】",&g_nMeanBlurValue, 50,on_MeanBlur ); on_MeanBlur(g_nMeanBlurValue,0);</span>
<span style="font-size:18px;"> namedWindow("【<3>高斯滤波】", 1); createTrackbar("内核值:", "【<3>高斯滤波】",&g_nGaussianBlurValue, 50,on_GaussianBlur ); on_GaussianBlur(g_nGaussianBlurValue,0); namedWindow("【<4>中值滤波】", 1); createTrackbar("参数值:", "【<4>中值滤波】",&g_nMedianBlurValue, 50,on_MedianBlur ); on_MedianBlur(g_nMedianBlurValue,0); namedWindow("【<5>双边滤波】", 1); createTrackbar("参数值:", "【<5>双边滤波】",&g_nBilateralFilterValue, 50,on_BilateralFilter); on_BilateralFilter(g_nBilateralFilterValue,0); return 0; } static void on_BoxFilter(int, void *) { boxFilter( g_srcImage, g_dstImage1, -1,Size( g_nBoxFilterValue+1, g_nBoxFilterValue+1)); imshow("【<1>方框滤波】", g_dstImage1); } static void on_MeanBlur(int, void *) { blur( g_srcImage, g_dstImage2, Size( g_nMeanBlurValue+1, g_nMeanBlurValue+1), Point(-1,-1)); imshow("【<2>均值滤波】", g_dstImage2); } static void on_GaussianBlur(int, void *) { GaussianBlur( g_srcImage, g_dstImage3, Size( g_nGaussianBlurValue*2+1, g_nGaussianBlurValue*2+1 ), 0, 0); imshow("【<3>高斯滤波】", g_dstImage3); } static void on_MedianBlur(int, void *) { medianBlur ( g_srcImage, g_dstImage4, g_nMedianBlurValue*2+1 ); imshow("【<4>中值滤波】", g_dstImage4); } static void on_BilateralFilter(int, void *) { bilateralFilter ( g_srcImage, g_dstImage5, g_nBilateralFilterValue, g_nBilateralFilterValue*2, g_nBilateralFilterValue/2 ); imshow("【<5>双边滤波】", g_dstImage5); } </span>
<span style="font-size:18px;">h=imread('d:\lena.jpg'); A=fspecial('average',3); output1 = imfilter(h, A, 'conv', 'replicate'); A=fspecial('gaussian',3); output2 = imfilter(h, A, 'conv', 'replicate'); output3 = medfilt2(h, [3, 3]);</span>
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)); 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 end figure; imshow(mat2gray(imgn(r+1:m+r,r+1:n+r)));
Opencv图像识别从零到精通(14)-----线性滤波和非线性滤波
标签:
原文地址:http://blog.csdn.net/qq_20823641/article/details/52004501