Welcome

首页 / 软件开发 / C# / 如何使用C#绘制数字图像灰度直方图

如何使用C#绘制数字图像灰度直方图2014-04-14 cnblogs 初行灰度直方图是灰度的函数,描述的是图像中具有该灰度级的像素的个数。如果用直角坐标系来表示 ,则它的横坐标是灰度级,纵坐标是该灰度出现的概率(像素的个数)。

灰度直方图的分布函数:

其中,K是指第k个灰度级,如果是8位灰度图像,k=0、1、 ……、255。

处理图像生成直方图数据

//将图像数据复制到byte中Rectangle rect = new Rectangle(0, 0, bmpHist.Width, bmpHist.Height);System.Drawing.Imaging.BitmapData bmpdata = bmpHist.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmpHist.PixelFormat);IntPtr ptr = bmpdata.Scan0; int bytes = bmpHist.Width * bmpHist.Height * 3;byte[] grayValues = new byte[bytes];System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes); //统计直方图信息byte temp = 0;maxPixel = 0;Array.Clear(countPixel, 0, 256);for (int i = 0; i < bytes; i++){temp = grayValues[i];countPixel[temp]++;if (countPixel[temp] > maxPixel){maxPixel = countPixel[temp];}} System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);bmpHist.UnlockBits(bmpdata);