getGaussianKernel
Getgaussiankernel
Kordustrükk: https://blog.csdn.net/u012633319/article/details/80921023
Põhimõtteline :
Digitaalsel pilditöötlusel võetakse kahemõõtmelist Gaussi funktsiooni tavaliselt nii
Punktist (1) võib teada, et kahemõõtmelist Gaussi funktsiooni võib pidada kahe ühemõõtmelise Gaussi funktsiooni korrutiseks. Seetõttu arvutatakse kõigepealt ühemõõtmeline Gaussi mall ja seejärel arvutatakse vajalik kahemõõtmeline Gaussi mall.
Kahe mõõtmelise Gaussi mall, mis saadakse kahe normaliseeritud ühemõõtmelise malli korrutamisel, on ka normaliseeritud tulemus, näiteks:
Nagu näidatud1Nagu näidatud,
(kuni) Kas kaks normaliseeritud ühemõõtmelist Gaussi malli, sta + b + c = 1, d + e + f + g + h = 1
(b) On kahemõõtmeline Gaussi mall, mis saadakse kahe ühemõõtmelise Gaussi koefitsiendi korrutamisel,
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。
Lähtekoodi analüüs :
aastalOpenCVFunktsioon ühemõõtmelise normaliseeritud Gaussi malli saamiseks on
// n -Malli suurus,sigma ( -Standardhälve
cv :: Mat cv :: getGaussianKernel (intn,topeltsigma,intktype)
Peamine protsess on:
Lähtekoodi analüüs on järgmine:
- [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
- }
Märge:
Kui n = 1, 3, 5, 7 ja sigma<= 0, the preset template is not used, sigma =((n-1) * 0,5-1) * 0,3 + 0,8Arvutamisel, nagu on näidatud järgmises tabelis:
On näha, et koefitsiendid ei ole praegusel ajal täielikult kooskõlas eelseadistatud mallikoefitsientidega.