for(int x = -fi; x <=fi; x++)
for(int y = -fi; y <= fi; y++) {
int sqrtFi = fi * fi;
double ex = Math.Pow(Math.E, (-(x * x + y * y)/(2 * sqrtFi)));
double result = ex / (2 * Math.PI * sqrtFi);
GAUSS[x + fi, y + fi] = result;
}
}
//事先计算好每个像素在卷积中要用到的值,避免重复运算, WeightedColor是自定义struct, 只有3个字段,
//分别是double R; double G; double B;
private WeightedColor[,,] GetWeightedColor(Bitmap source, int fi) {
WeightedColor[,,] retVal = new WeightedColor[fi + 1 + fi, source.Width, source.Height];
double r = 0, g = 0, b = 0;
for(int i = 0; i < source.Width; i++)
for(int j = 0; j <source.Height; j++) {
//Calculate weighted color
Color c = source.GetPixel(i, j);
for(int k = 0; k < fi + 1 + fi; k++) {
//From center to edge
double weight;
if( k < fi + 1)
weight = GAUSS[fi, fi + k];
else
//对角
weight = GAUSS[k, k];
r = c.R * weight;
g = c.G * weight;
b = c.B * weight;
r = r > 255 ? 255 : r;
g = g > 255 ? 255 : g;
b = b > 255 ? 255 : b;
retVal[k, i, j] = WeightedColor.FromRGB(r, g, b);
}
}
//计算一个点的卷积值
private Color GetFilteredColor(Bitmap source, int x, int y, int fi, WeightedColor[,,] colorMap) {
int w = source.Width, h = source.Height;
double r = 0, g = 0, b = 0;
for(int u = x - fi; u <= x + fi; u++)
for(int v = y - fi; v <= y + fi; v++) {
if(u >= 0 && u < w && v >= 0 && v < h) {
//wx, wy is the distance between x and u, y and v
int wx = Math.Abs(u - x), wy = Math.Abs(v - y);
//Use which version of colorMap
int wVersion;
if(wx == wy && wx != 0)//对角
wVersion = fi + wx;
else
wVersion = wx > wy ? wx : wy;
WeightedColor tmpC = colorMap[wVersion, u, v];
r += tmpC.R;
g += tmpC.G;
b += tmpC.B;
}
}
r = r > 255 ? 255 : r;
g = g > 255 ? 255 : g;
b = b > 255 ? 255 : b;
return Color.FromArgb((int)r, (int)g, (int)b);
}
return retVal;
}
//使用这个函数对图像进行过滤
private Bitmap GaussianFilt(Bitmap source, int fi) {
Bitmap retVal = new Bitmap(source.Width, source.Height);
WeightedColor[,,] colorMap = GetWeightedColor(source, fi);
for(int i = 0; i < source.Width; i++)
for(int j = 0; j <source.Height; j++) {
Color fColor = GetFilteredColor(source, i, j, fi, colorMap);
retVal.SetPixel(i, j, fColor);
}
return retVal;
}