赞
踩
private double[,] GaussianKernel(int N)
{
int i, j;
double sigma = 1;
double[,] gussian = new double[N, N];
double sum = 0.0;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
gussian[i, j] = Math.Exp(-((i - N / 2) * (i - N / 2) + (j - N / 2) * (j - N / 2)) / (2.0 * sigma * sigma));
sum += gussian[i, j];
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
gussian[i, j] = gussian[i, j] / sum;
}
}
return gussian;
}
private Image<Bgr, byte> ExtendImg(Image<Bgr, byte> img, int size)
{
int w = img.Width;
int h = img.Height;
int nw = w + size * 2;
int nh = h + size * 2;
Image<Bgr, byte> nimg = new Image<Bgr, byte>(nw, nh);
for (int i = 0; i < size; i++)
for (int j = size; j < nh - size; j++)
nimg[j, i] = img[j - size, i];
for (int i = nw - size; i < nw; i++)
for (int j = 0; j < h; j++)
nimg[j, i] = img[j, i - size * 2];
for (int i = size; i < nw - size; i++)
for (int j = 0; j < size; j++)
nimg[j, i] = img[j, i - size];
for (int i = size; i < nw - size; i++)
for (int j = nh - size; j < nh; j++)
nimg[j, i] = img[j - size * 2, i - size];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
nimg[j, i] = img[j, i];
for (int i = nw - size; i < nw; i++)
for (int j = 0; j < size; j++)
nimg[j, i] = img[j, i - size * 2];
for (int i = nw - size; i < nw; i++)
for (int j = nh - size; j < nh; j++)
nimg[j, i] = img[j - size * 2, i - size * 2];
for (int i = 0; i < size; i++)
for (int j = nh - size; j < nh; j++)
nimg[j, i] = img[j - size * 2, i];
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
nimg[j + size, i + size] = img[j, i];
return nimg;
}
private Image<Bgr, byte> Convolution(Image<Bgr, byte> pic, double[,] k)
{
int d = k.GetLength(0) / 2;
Image<Bgr, byte> temp = ExtendImg(pic, d);
Image<Bgr, byte> output = new Image<Bgr, byte>(pic.Size);
double b = 0, g = 0, r = 0;
for (int i = d; i < temp.Width - d; i++)
{
for (int j = d; j < temp.Height - d; j++)
{
b = 0; g = 0; r = 0;
for (int m = -d; m < d; m++)
{
for (int n = -d; n < d; n++)
{
b += temp[j + m, i + n].Blue * k[m + d, n + d];
g += temp[j + m, i + n].Green * k[m + d, n + d];
r += temp[j + m, i + n].Red * k[m + d, n + d];
}
}
output[j - d, i - d] = new Bgr(b, g, r);
}
}
return output;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。