Skip to content

Instantly share code, notes, and snippets.

@smoogipoo
Created September 22, 2015 09:34
Show Gist options
  • Save smoogipoo/1eca47854924641ede47 to your computer and use it in GitHub Desktop.
Save smoogipoo/1eca47854924641ede47 to your computer and use it in GitHub Desktop.
Linearly-sampled Gaussian blur kernel
class Kernel
{
public double[] Weights;
public double[] Offsets;
public Kernel(int taps)
{
Debug.Assert(taps >= 0);
double[] row = genPascalRow(taps);
double sum = row.Sum();
List<double> weights = new List<double>();
List<double> offsets = new List<double>();
if (row.Length % 2 == 0)
{
float o = -0.5f;
for (int i = row.Length / 2 - 1, count = 0; i <= row.Length - 2; i += 2, o++, count++)
{
weights.Add((row[i] + row[i + 1]) / sum);
offsets.Add((o * row[i] + (o + 1) * row[i + 1]) / sum / weights[count]);
}
}
else
{
weights.Add(row[row.Length / 2] / sum);
offsets.Add(0.0);
for (int i = row.Length / 2 + 1, o = 1, count = 1; i < row.Length - 2; i += 2, o++, count++)
{
weights.Add((row[i] + row[i + 1]) / sum);
offsets.Add((o * row[i] + (o + 1) * row[i + 1]) / sum / weights[count]);
}
}
Weights = weights.ToArray();
Offsets = offsets.ToArray();
}
private static double[] genPascalRow(int n)
{
double[] row = new double[n + 1];
row[0] = 1;
for (int i = 0; i < n; i++)
row[i + 1] = row[i] * (n - i) / (i + 1);
return row;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment