Skip to content

Instantly share code, notes, and snippets.

@larsberg
Last active August 29, 2015 14:13
Show Gist options
  • Save larsberg/32dd5562fb0474bb7bd3 to your computer and use it in GitHub Desktop.
Save larsberg/32dd5562fb0474bb7bd3 to your computer and use it in GitHub Desktop.

Revisions

  1. larsberg revised this gist Feb 7, 2015. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gaussian1D
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@

    // 1D gaussian kernel
    // modified from here: http://www.apileofgrains.nl/blur-filters-c/
    // pretty much ripped from here: http://www.apileofgrains.nl/blur-filters-c/
    vector<float> gaussianKernel( int radius, float weight = 1.)
    {
    int mem_amount = (radius*2)+1;
  2. larsberg created this gist Jan 14, 2015.
    28 changes: 28 additions & 0 deletions gaussian1D
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@

    // 1D gaussian kernel
    // modified from here: http://www.apileofgrains.nl/blur-filters-c/
    vector<float> gaussianKernel( int radius, float weight = 1.)
    {
    int mem_amount = (radius*2)+1;
    vector<float> kernel( (radius*2) + 1 );

    float twoRadiusSquaredRecip = 1.0 / (2.0 * radius * radius);
    float sqrtTwoPiTimesRadiusRecip = 1.0 / (sqrt(2.0 * PI) * radius);

    // Create Gaussian Kernel
    int r = -radius;
    float sum = 0, x, v;
    for (int i = 0; i < kernel.size(); i++)
    {
    v = sqrtTwoPiTimesRadiusRecip * exp(-pow(r * weight, 2) * twoRadiusSquaredRecip);
    kernel[i] = v;

    sum+=v;
    r++;
    }

    // normalize
    for (auto& k: kernel) k /= sum;

    return kernel;
    }