Win8 Metro(C#)數字圖像處理--2.72圖像噪聲

原文: Win8 Metro(C#)數字圖像處理--2.72圖像噪聲



[函數名稱]php

  噪聲函數WriteableBitmap NoiseProcess(WriteableBitmap src, byte flagNoise, double meanValue, double varianceValue)html

/// <summary>
        /// Noise process.
        /// </summary>
        /// <param name="src">The source image.</param>
         /// <param name="flagNoise">One parameter to choose noise mode,0--Gauss noise,1--Rayleigh noise,2--Noise index,3--alt and pepper noise.</param>
        /// <param name="meanValue">Mean value or alt value.</param>
        /// <param name="varianceValue">Variance value or pepper value.</param>
        /// <returns></returns>
         public static WriteableBitmap NoiseProcess(WriteableBitmap src, byte flagNoise, double meanValue, double varianceValue)////噪聲
         {
             if (src != null)
             {
                 int w = src.PixelWidth;
                 int h = src.PixelHeight;
                 WriteableBitmap srcImage = new WriteableBitmap(w, h);
                 byte[] temp = src.PixelBuffer.ToArray();
                 double tempValue = 0;
                 Random r1, r2;
                 double v1 = 0, v2 = 0;
                 r1 = new Random(unchecked((int)DateTime.Now.Ticks));
                 r2 = new Random(~unchecked((int)DateTime.Now.Ticks));
                 for (int i = 0; i < temp.Length; i++)
                 {
                     switch (flagNoise)
                     {
                         case 0:
                             //Gauss noise 0 20
                             do
                             {
                                 v1 = r1.NextDouble();
                             }
                             while (v1 <= 0.00000000001);
                             v2 = r2.NextDouble();
                             tempValue = Math.Sqrt(-2 * Math.Log(v1)) * Math.Cos(2 * Math.PI * v2) * varianceValue + meanValue;
                             break;
                         case 1:
                             do
                             //Rayleigh noise 0 200
                             {
                                 v1 = r1.NextDouble();
                             }
                             while (v1 >= 0.9999999999);
                             tempValue = meanValue + Math.Sqrt(-1 * varianceValue * Math.Log(1 - v1));
                             break;
                         case 2:
                             //Noise index  0.1
                             do
                             {
                                 v1 = r1.NextDouble();
                             }
                             while (v1 >= 0.9999999999);
                             tempValue = -1 * Math.Log(1 - v1) / meanValue;
                             break;
                         case 3:
                             //Salt and pepper noise 0.02 0.02
                             v1 = r1.NextDouble();
                             if (v1 <= meanValue)
                                 tempValue = -500;
                             else if (v1 >= (1 - varianceValue))
                                 tempValue = 500;
                             else
                                 tempValue = 0;
                             break;
                         default:
                             break;
                     }
                     tempValue = tempValue + temp[i];

                     if (tempValue > 255)
                     {
                         temp[i] = 255;
                     }
                     else if (tempValue < 0)
                     {
                         temp[i] = 0;
                     }
                     else
                         temp[i] = Convert.ToByte(tempValue);
                 }                
                 Stream sTemp = srcImage.PixelBuffer.AsStream();
                 sTemp.Seek(0, SeekOrigin.Begin);
                 sTemp.Write(temp, 0, w * 4 * h);
                 return srcImage;
             }
             else
             {
                 return null;
             }
         }


最後,分享一個專業的圖像處理網站(微像素),裏面有不少源代碼下載:
相關文章
相關標籤/搜索