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)

主なプロセスは次のとおりです。

ソースコードの分析は次のとおりです。

  1. [Function name]
  2. cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
  3. [Function description] Template coefficients for calculating the one-dimensional Gaussian function
  4. [Parameter description] n — template size, n must be greater than0Positive integer of, generally take n as an odd number, but pass
  5. Through source code analysis, we can know that if n is an even number, the same can be calculated.
  6. Should result.
  7. sigma — Gaussian standard deviation (σ in the formula), if sigma is less than or equal to0The number of
  8. The function calculates the corresponding sigma value based on the current n value,
  9. sigma = ((n-1)*0.51)*0.3 + 0.8
  10. ktype — data type, CV_32F or CV_64F
  11. [Return value] Return n rows1Column normalized one-dimensional Gaussian coefficient
  12. cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
  13. {
  14. // The preset Gaussian coefficient array, only in
  15. // (1) 0
  16. // (2) sigma is an invalid parameter, ie sigma <= 0
  17. // The preset Gaussian coefficient array will be used when both are satisfied
  18. const int SMALL_GAUSSIAN_SIZE = 7 // The size of the preset largest template
  19. static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
  20. {
  21. {1.f},
  22. {0.25f, 0.5f, 0.25f},
  23. {0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
  24. {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
  25. }
  26. // Determine whether the condition of using preset Gaussian coefficient is satisfied
  27. const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
  28. small_gaussian_tab[n>>1] : 0
  29. // To determine the legality of the ktype parameter, only go to CV_32F or CV_64F
  30. CV_Assert( ktype == CV_32F || ktype == CV_64F )
  31. Mat kernel(n, 1, ktype) // Create n rows and one column coefficient matrix
  32. float* cf = (float*)kernel.data // cf is assigned to the data address
  33. double* cd = (double*)kernel.data // cd is assigned to the data address
  34. // If sigma <0, use the formula to calculate sigma according to the template size n
  35. double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8
  36. double scale2X = -0.5/(sigmaX*sigmaX) // Calculate -1 / 2σ ^ 2 in e ^ (-x ^ 2 / 2σ ^ 2)
  37. double sum = 0 // The sum of each element is used as the divisor when normalized
  38. int i
  39. for( i = 0 i
  40. {
  41. double x = i - (n-1)*0.5 // Calculate the x coordinate of the element with index 0
  42. 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
  43. // Will make an appointment, so no calculation is required.
  44. // Assign values ​​to CV_32F or CV_64F according to the element type, and sum
  45. if( ktype == CV_32F )
  46. {
  47. cf[i] = (float)t
  48. sum += cf[i]
  49. }
  50. else
  51. {
  52. cd[i] = t
  53. sum += cd[i]
  54. }
  55. }
  56. // Do normalized calculation
  57. sum = 1./sum
  58. for( i = 0 i
  59. {
  60. if( ktype == CV_32F )
  61. cf[i] = (float)(cf[i]*sum)
  62. else
  63. cd[i] *= sum
  64. }
  65. return kernel
  66. }

注意:

n = 1、3、5、7およびシグマの場合<= 0, the preset template is not used, sigma =((n-1)* 0.5-1)* 0.3 + 0.8次の表に示すように、計算する場合:


この時点での係数は、事前設定されたテンプレート係数と完全には一致していないことがわかります。