How do I gaussian blur an image without using any in-built gaussian functions?

前端 未结 5 1509
一生所求
一生所求 2020-11-28 02:07

I want to blur my image using the native Gaussian blur formula. I read the Wikipedia article, but I am not sure how to implement this.

How do I use the formula to de

5条回答
  •  感动是毒
    2020-11-28 02:16

    Here's the pseudo-code for the code I used in C# to calculate the kernel. I do not dare say that I treat the end-conditions correctly, though:

    double[] kernel = new double[radius * 2 + 1];
    double twoRadiusSquaredRecip = 1.0 / (2.0 * radius * radius);
    double sqrtTwoPiTimesRadiusRecip = 1.0 / (sqrt(2.0 * Math.PI) * radius);
    double radiusModifier = 1.0;
    
    int r = -radius;
    for (int i = 0; i < kernel.Length; i++)
    {
        double x = r * radiusModifier;
        x *= x;
        kernel[i] = sqrtTwoPiTimesRadiusRecip * Exp(-x * twoRadiusSquaredRecip);
        r++;
    }
    
    double div = Sum(kernel);
    for (int i = 0; i < kernel.Length; i++)
    {
        kernel[i] /= div;
    }
    

    Hope this helps.

提交回复
热议问题