[函數代碼] /// <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; } }