图像的熵、灰度平均值、灰度中值、方差
//图像的熵=========================================================================================
private void Menu_Entropy_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
//计算熵
double entropy = GetEntropy(curBitmap, curBitmap.Width, curBitmap.Height);
MessageBox.Show("图像:"+curFileName +"nn"+"图像熵为 H = " + entropy);
}
}
//计算熵的函数-----------------------------------------------
public double GetEntropy(Bitmap bmp, int w, int h)
{
int g;
double H = 0.0;
int[] pix = new int[256];
Array.Clear(pix, 0, 256);
//获取各灰度值的像素个数
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
Color c = bmp.GetPixel(j, i);
g = (int)(0.299*c.R + 0.587*c.B + 0.114*c.G);
pix[g]++;
}
}
//计算熵
for (int i = 0; i < 256; i++)
if (pix[i] > 0)
H = H + pix[i] * Math.Log10(pix[i]);
H = Math.Log10(w * h) / Math.Log10(2) - H / (w * h * Math.Log10(2));
return H;
}
//灰度平均值=======================================================================================
private void Menu_GrayAverage_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
double aver = GetGrayAverage(curBitmap, curBitmap.Width, curBitmap.Height);
MessageBox.Show("图像:" + curFileName +"nn"+ "灰度平均值为 A = " + aver);
}
}
//计算灰度平均值的函数---------------------------------------
public double GetGrayAverage(Bitmap bmp, int w, int h)
{
double sum = 0;
//计算平均值
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
{
Color c = bmp.GetPixel(i, j);
sum = sum + (int)(0.299 * c.R + 0.587 * c.B + 0.114 * c.G);
}
double aver = sum / (w * h);
return aver;
}
//灰度中值=========================================================================================
private void Menu_GrayMid_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
Bitmap bmp = curBitmap;
//由5 X 5的矩形获取值
int[] pix = new int[25];
for (int j = 0; j < 5; j++)
{
for (int i = 0; i < 5; i++)
{
pix[i + j * 5] = bmp.GetPixel(100 + i, 100 + j).B;
}
}
pix = MedianSorter(pix,25);
MessageBox.Show("图像:" + curFileName + "n" +
"左上角坐标为(100,100)的5X5矩形块n灰度中值为" +
" M = " + pix[12]);
}
}
//排序-------------------------------------------------------
public int[] MedianSorter(int[] data, int m)
{
int tem;
for (int i = m - 1; i >= 1; i--)
for (int j = 1; j <= i; j++)
if (data[j - 1] > data[j])
{
tem = data[j];
data[j] = data[j - 1];
data[j - 1] = tem;
}
return data;
}
//方差=============================================================================================
private void Menu_Variance_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
double vari = GetVariance(curBitmap, curBitmap.Width, curBitmap.Height);
MessageBox.Show("图像:" + curFileName + "nn" + "方差值 S = " + vari);
}
}
//计算方差值的函数-------------------------------------------
public double GetVariance(Bitmap bmp, int w, int h)
{
double aver, vari =0;
int data;
//求平均值
aver = GetGrayAverage(bmp ,w , h);
//求方差
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
Color c = bmp.GetPixel(i, j);
data = (int)(0.299 * c.R + 0.587 * c.B + 0.114 * c.G);
vari = vari + (data - aver) * (data - aver);
}
}
vari = vari / (w * h);
return vari;
}