Win8 Metro(C#)数字图像处理--3.4图像信息熵计算

时间:2021-05-22 05:16:13

原文: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;
}
}
最后,分享一个专业的图像处理网站(微像素),里面有很多源代码下载: