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.
// 1D gaussian kernel
// 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;
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment