getGaussianKernel
Getgaussiankernel
転載: https://blog.csdn.net/u012633319/article/details/80921023
ファンダメンタル :
デジタル画像処理では、2次元ガウス関数は一般的に次のように解釈されます。
(1)から、2次元ガウス関数は2つの1次元ガウス関数の積と見なすことができます。したがって、最初に1次元のガウステンプレートが計算され、次に必要な2次元のガウステンプレートが計算されます。
2つの正規化された1次元テンプレートを乗算して得られた2次元ガウステンプレートも、正規化された結果です。次に例を示します。
示されているように1示されているように、
((に)2つの正規化された1次元ガウステンプレートです。a + b + c = 1、d + e + f + g + h = 1
((b)は、2つの1次元ガウス係数を乗算して得られる2次元ガウステンプレートです。、
ad + ae + af + ag + ah + bd + be + bf + bg + bh + cd + ce + cf + cg + ch
= a(d + e + f + g + h)+ b(b + e + f + g + h)+ c(b + e + f + g + h)
=(a + b + c)(d + e + f + g + h)= 1 * 1 = 1。
ソースコード分析 :
にOpenCV1次元の正規化されたガウステンプレートを取得する関数は次のとおりです。
// n –テンプレートサイズ、シグマ( -標準偏差
cv :: Mat cv :: getGaussianKernel(intn、ダブルシグマ、intktype)
主なプロセスは次のとおりです。
ソースコードの分析は次のとおりです。
- [Function name]
- cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
- [Function description] Template coefficients for calculating the one-dimensional Gaussian function
- [Parameter description] n — template size, n must be greater than0Positive integer of, generally take n as an odd number, but pass
- Through source code analysis, we can know that if n is an even number, the same can be calculated.
- Should result.
- sigma — Gaussian standard deviation (σ in the formula), if sigma is less than or equal to0The number of
- The function calculates the corresponding sigma value based on the current n value,
- sigma = ((n-1)*0.5 – 1)*0.3 + 0.8。
- ktype — data type, CV_32F or CV_64F
- [Return value] Return n rows1Column normalized one-dimensional Gaussian coefficient
- cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
- {
- // The preset Gaussian coefficient array, only in
- // (1) 0
- // (2) sigma is an invalid parameter, ie sigma <= 0
- // The preset Gaussian coefficient array will be used when both are satisfied
- const int SMALL_GAUSSIAN_SIZE = 7 // The size of the preset largest template
- 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}
- }
- // Determine whether the condition of using preset Gaussian coefficient is satisfied
- const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
- small_gaussian_tab[n>>1] : 0
- // To determine the legality of the ktype parameter, only go to CV_32F or CV_64F
- CV_Assert( ktype == CV_32F || ktype == CV_64F )
- Mat kernel(n, 1, ktype) // Create n rows and one column coefficient matrix
- float* cf = (float*)kernel.data // cf is assigned to the data address
- double* cd = (double*)kernel.data // cd is assigned to the data address
- // If sigma <0, use the formula to calculate sigma according to the template size n
- double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8
- double scale2X = -0.5/(sigmaX*sigmaX) // Calculate -1 / 2σ ^ 2 in e ^ (-x ^ 2 / 2σ ^ 2)
- double sum = 0 // The sum of each element is used as the divisor when normalized
- int i
- for( i = 0 i
- {
- double x = i - (n-1)*0.5 // Calculate the x coordinate of the element with index 0
- double t = fixed_kernel ? (double)fixed_kernel[i] : std::exp(scale2X*x*x) // Take the corresponding element in the preset array or calculate by Gauss formula, 1 / √2π σ_x when normalized
- // Will make an appointment, so no calculation is required.
- // Assign values to CV_32F or CV_64F according to the element type, and sum
- if( ktype == CV_32F )
- {
- cf[i] = (float)t
- sum += cf[i]
- }
- else
- {
- cd[i] = t
- sum += cd[i]
- }
- }
- // Do normalized calculation
- sum = 1./sum
- for( i = 0 i
- {
- if( ktype == CV_32F )
- cf[i] = (float)(cf[i]*sum)
- else
- cd[i] *= sum
- }
- return kernel
- }
注意:
n = 1、3、5、7およびシグマの場合<= 0, the preset template is not used, sigma =((n-1)* 0.5-1)* 0.3 + 0.8次の表に示すように、計算する場合:
この時点での係数は、事前設定されたテンプレート係数と完全には一致していないことがわかります。