如何计算高斯滤波器内核

时间:2014-04-22 19:11:37

标签: filter kernel gaussian

我正在研究图像处理项目。我需要实现高斯滤波器。如何计算3x3,5x5,7x7内核?请帮我。

http://s14.postimg.org/rwpyq8k5d/image.jpg

1 个答案:

答案 0 :(得分: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;
    }