2014-04-22 61 views

回答

0

下面的代碼說明了如何計算高斯核與任何過濾器的大小和高斯加權參數。

enter code here public static double[,] CalculateGaussianKernel(int length, double weight) 
    { 
     // define an array of two dimensions based on the length value that pass it by the user from the text box. 
     double[,] Kernel = new double[length, length]; 
     double sumTotal = 0; 

     int kernelRadius = length/2; 
     double distance = 0; 

     double calculatedEuler = 1.0/(2.0 * Math.PI * Math.Pow(weight, 2)); // Gaussian Function first part 

     for (int filterY = -kernelRadius; filterY <= kernelRadius; filterY++) 
     { 
      for (int filterX = -kernelRadius; filterX <= kernelRadius; filterX++) 
      { 
       distance = ((filterX * filterX) + (filterY * filterY)) /(2 * (weight * weight)); // Gaussian Function Second part 

       Kernel[filterY + kernelRadius,filterX + kernelRadius] = calculatedEuler * Math.Exp(-distance); 

       sumTotal += Kernel[filterY + kernelRadius, filterX + kernelRadius]; 
      } 
     } 

     for (int y = 0; y < length; y++) 
     { 
      for (int x = 0; x < length; x++) 
      { 
       Kernel[y, x] = Kernel[y, x] * 
           (1.0/sumTotal); 
      } 
     } 

     return Kernel; 
    }