C#高斯平滑算法 :二维高斯卷积代码实例
生活随笔
收集整理的這篇文章主要介紹了
C#高斯平滑算法 :二维高斯卷积代码实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//高斯平滑處理方法//inputImage 輸入圖像// outputImage 輸出圖像//sigema 均方差private void gaussSmooth(double[]inputImage,out double[] outputImage,double sigema){//方差double std2 = 2 * sigema * sigema;//半徑=3sigemaint radius = Convert.ToInt16(Math.Ceiling(3 * sigema));int filterWidth = 2 * radius + 1;double[] filter = new double[filterWidth];outputImage = new double[inputImage.Length];//限定輸入的情況為方陣的情況下得到的圖像的寬度和高度int length = Convert.ToInt16(Math.Sqrt(inputImage.Length));double[] tempImage = new double[inputImage.Length];double sum = 0;//產生一維高斯函數for (int i = 0; i < filterWidth; i++){int xx = (i - radius) * (i - radius);filter[i] = Math.Exp(-xx / std2);sum += filter[i];}//歸一化for (int i = 0; i < filterWidth; i++){filter[i] = filter[i] / sum;}//水平方向濾波for (int i = 0; i < length; i++){for (int j = 0; j < length; j++){double temp = 0;for (int k = -radius; k <= radius; k++){//循環拓展int rem = (Math.Abs(j + k) % length);//計算卷積和temp += inputImage[i * length + rem] * filter[k + radius];}tempImage[i + length + j] = temp;}}//垂直方向濾波for (int j = 0; j < length; j++){for (int i = 0; i < length; i++){double temp = 0;for (int k = -radius ; k <=radius; k++){//循環拓展int rem = (Math.Abs(i + k) )% length;//計算卷積和temp += tempImage[rem * length + j] * filter[k + radius];}outputImage[i * length + j] = temp;}}}
總結
以上是生活随笔為你收集整理的C#高斯平滑算法 :二维高斯卷积代码实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: log(二)——MDC实现之Thread
- 下一篇: 设计模式【5】-- 原型模式