public static Image<Gray,byte> Otsu2D(Image<Gray, byte> img_in)
{
float[] histogram = new float[256];
int w = img_in.Width;
int h = img_in.Height;
Bitmap bm = img_in.Bitmap;
Bitmap bm_fix = bm.Clone(new Rectangle(0, 0, w, h), System.Drawing.Imaging.PixelFormat.Format24bppRgb);//解决“具有索引像素格式的图像不支持 SetPixel”的错误
Bitmap bm_th = bm;
int temp;
double td = 0;
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
Color c = bm.GetPixel(i, j);
temp = ((int)c.R + (int)c.G + (int)c.B) / 3;
histogram[temp]++;
}
}
//计算各像素频率和平均灰度
float avgValue = 0;
int numPixel = w * h;
for (int i = 0; i < 256; i++)
{
histogram[i] = histogram[i] / numPixel;
avgValue += i * histogram[i];
}
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余2页未读,立即下载