1. 程式人生 > >Win8 Metro(C#)數字影象處理--3.2影象方差計算

Win8 Metro(C#)數字影象處理--3.2影象方差計算

       /// <summary>
        /// 
        /// </summary>Variance computing.
        /// <param name="src">The source image.</param>
        /// <returns></returns>
        public static double GetVarianceProcess(WriteableBitmap src) ////33 影象方差計算
        {
            if (src != null)
            {
                double mean = GetMeanProcess(src);
                double sum = 0;
                double variance = 0;
                int gray = 0, number = 0;
                byte[] temp = src.PixelBuffer.ToArray();
                for (int i = 0; i < temp.Length; i += 4)
                {
                    gray = (int)(temp[i] * 0.114 + temp[i + 1] * 0.587 + temp[i + 2] * 0.299);
                    sum += (gray-mean)*(gray-mean);
                    number++;
                }
                variance = (double)(sum / number);
                return variance;
            }
            else
            {
                return 0;
            }
        }
最後,分享一個專業的影象處理網站(微畫素),裡面有很多原始碼下載: