Win8 Metro(C#)數字圖像處理--3.4圖像信息熵計算

原文: Win8 Metro(C#)數字圖像處理--3.4圖像信息熵計算

[函數代碼]
        /// <summary>
        /// Entropy of one image.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <returns></returns>
        public static double GetEntropy(WriteableBitmap src)
        {
            double entropy = 0;
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                byte[] temp = src.PixelBuffer.ToArray();
                int[] countTemp=new int[256];
                int gray = 0;
                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);
                    countTemp[gray]++;
                }
                for (int i = 0; i < 256;i++)
                {
                    if (countTemp[i] != 0)
                    {
                        entropy += (double)(-((countTemp[i] / (w * h)) * Math.Log10(countTemp[i] / (w * h))));
                    }
                    else
                        continue;
                }
                    return entropy;
            }
            else
            {
                return 0;
            }   
        }
最後,分享一個專業的圖像處理網站(微像素),裏面有不少源代碼下載:
相關文章
相關標籤/搜索