Skip to content

Instantly share code, notes, and snippets.

@esibirtseva
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esibirtseva/7052c07a71df7a3a1c64 to your computer and use it in GitHub Desktop.
Save esibirtseva/7052c07a71df7a3a1c64 to your computer and use it in GitHub Desktop.
not so fair gaussian blur with box filter
public byte[] blurGauss_2(byte[] originalImage, int w, int h, double r)
{
byte[] blurImage = new byte[originalImage.Length];
var bxs = boxesForGauss(r, 3);
boxBlur_2(originalImage, blurImage, w, h, (bxs[0] - 1) / 2);
boxBlur_2(blurImage, originalImage, w, h, (bxs[1] - 1) / 2);
boxBlur_2(originalImage, blurImage, w, h, (bxs[2] - 1) / 2);
return blurImage;
}
// standard deviation, number of boxes
private int[] boxesForGauss(double sigma, int n){
double wIdeal = Math.Sqrt((12 * sigma * sigma / n) + 1);// Ideal averaging filter width
int wl = (int)Math.Floor(wIdeal); if (wl % 2 == 0) wl--;
int wu = wl+2;
double mIdeal = (12 * sigma * sigma - n * wl * wl - 4 * n * wl - 3 * n) / (-4 * wl - 4);
int m = (int)Math.Round(mIdeal);
int[] sizes = new int[n];
for (int i = 0; i < n; i++ )
{
sizes[i] = (i< m ? wl : wu);
}
return sizes;
}
private void boxBlur_2(byte[] originalImage, byte[] blurImage, int w, int h, int r){
for (int i = 0; i < h; i++ )
{
for (int j = 0; j < w; j++) {
double val = 0;
for (int iy = i - r; iy < i + r + 1; iy++ )
{
for (int ix = j - r; ix < j + r + 1; ix++ )
{
int x = Math.Min(w-1, Math.Max(0,ix));
int y = Math.Min(h - 1, Math.Max(0, iy));
val += originalImage[y * w + x];
}
}
blurImage[i * w + j] = (byte)(val / ((r + r + 1) * (r + r + 1)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment