当前位置:   article > 正文

【C#】图像滤镜(一):图像平滑_c# 图片处理滤镜

c# 图片处理滤镜

一、高斯平滑

1. 高斯核计算

    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;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

2. 图像扩展

    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;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

3. 卷积计算

 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;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

3. 结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/257628
推荐阅读
相关标签
  

闽ICP备14008679号