zoukankan      html  css  js  c++  java
  • 【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;
            }

    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;
            }

    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;
            }

    3. 结果

  • 相关阅读:
    ps中套索工具怎么使用的方法
    动态加载css方法实现和深入解析
    深入React事件系统(React点击空白部分隐藏弹出层;React阻止事件冒泡失效)
    近期项目中用到的一些自己写的或者整理而成的前端效果干货(********************************)
    Vue.js自定义指令的用法与实例
    vue初探
    React Router基础使用
    javascript常用积累
    js动画(三)
    基于jQuery的ajax系列之用FormData实现页面无刷新上传
  • 原文地址:https://www.cnblogs.com/cnsec/p/13286761.html
Copyright © 2011-2022 走看看