快樂蝦@[http://blog.csdn.net/lights_joy/](http://blog.csdn.net/lights_joy/)
歡迎轉載,但請保留作者信息
本文適用于opencv3.0.0, vs2013
Opencv中提供了高斯濾波函數:
~~~
/**@brief Blurs an image using a Gaussian filter.
The function convolves the source image with the specified Gaussian kernel. In-place filtering is
supported.
@param src input image; the image can have any number of channels, which are processed
independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
@param dst output image of the same size and type as src.
@param ksize Gaussian kernel size. ksize.width and ksize.height can differ but they both must be
positive and odd. Or, they can be zero's and then they are computed from sigma.
@param sigmaX Gaussian kernel standard deviation in X direction.
@param sigmaY Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set to be
equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width and ksize.height,
respectively (see cv::getGaussianKernel for details); to fully control the result regardless of
possible future modifications of all this semantics, it is recommended to specify all of ksize,
sigmaX, and sigmaY.
@param borderType pixel extrapolation method, see cv::BorderTypes
@sa sepFilter2D, filter2D, blur, boxFilter, bilateralFilter, medianBlur
*/
CV_EXPORTS_W void GaussianBlur( InputArray src, OutputArray dst, Size ksize,
double sigmaX, double sigmaY = 0,
int borderType = BORDER_DEFAULT );
~~~
本節學習一下它的實現和使用。
### [1.????高斯函數的定義]()
高斯函數的形式為:

其中 a、b 與 c 為實數常數,且a > 0.
當a=1, b = 0, c = 1時,此函數圖形如下:

在上面三個參數中,a控制尖峰的值,b控制中心點偏離0點的值,c控制上升速度。
當a=2, b=1, c=0.5時圖形如下,可以明顯看出這種影響。

### 2.平滑處理中的高斯函數
由于高斯函數的可分離性,Opencv將二維高斯函數卷積分兩步來進行,首先將圖像與一維高斯函數進行卷積,然后將卷積結果與方向垂直的相同一維高斯函數卷積。在每個方向上都是一維的卷積,且高斯函數的形式變為了:

這里的ksize為選擇的核大小,i為要計算核函數中點的序號。
這里的alpha為歸一化系數,用于保證計算出的ksize個數之和為1。
當sigma<=0,則計算公式為:sigma =0.3*((ksize-1)*0.5 - 1) + 0.8 .
sigma>0,則就用該輸入參數sigma。?
Opencv中高斯核的生成由函數getGaussianKernel完成。
~~~
cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
{
const int SMALL_GAUSSIAN_SIZE = 7;
static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
{
{1.f},
{0.25f, 0.5f, 0.25f},
{0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
{0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
};
const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
small_gaussian_tab[n>>1] : 0;
CV_Assert( ktype == CV_32F || ktype == CV_64F );
Mat kernel(n, 1, ktype);
float* cf = kernel.ptr<float>();
double* cd = kernel.ptr<double>();
double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8;
double scale2X = -0.5/(sigmaX*sigmaX);
double sum = 0;
int i;
for( i = 0; i < n; i++ )
{
double x = i - (n-1)*0.5;
double t = fixed_kernel ? (double)fixed_kernel[i] : std::exp(scale2X*x*x);
if( ktype == CV_32F )
{
cf[i] = (float)t;
sum += cf[i];
}
else
{
cd[i] = t;
sum += cd[i];
}
}
sum = 1./sum;
for( i = 0; i < n; i++ )
{
if( ktype == CV_32F )
cf[i] = (float)(cf[i]*sum);
else
cd[i] *= sum;
}
return kernel;
}
~~~
這個函數其實比較簡單,只是有一點需要注意:
當sigma<=0,則sigma =0.3*((ksize-1)*0.5 - 1) + 0.8 .
當ksize確定了之后,其實它就是一個常數,因而公式

的計算結果也是一個常數。Opencv為了加快計算速度,在ksize較小時直接將這些常數值寫在代碼中,即small_gaussian_tab這個數組的值(注意,這個數組僅當輸入的sigma參數<=0時才有效)。
###3.sigma對濾波結果的影響
從上面的分析可以看出,高斯濾波器寬度(決定著平滑程度)是由參數σ表征的,而且σ和平滑程度的關系是非常簡單的。σ越大,高斯濾波器的頻帶就越寬,平滑程度就越好,圖像也將越模糊。通過調節平滑程度參數σ,可在圖像特征過分模糊(過平滑)與平滑圖像中由于噪聲和細紋理所引起的過多的不希望突變量(欠平滑)之間取得折衷。
同樣取核大小為5,比較一下:
當sigma為1時:

而當sigma為3時:

顯然后者的模糊程度更高。
??