图像处理方法:1.提取像素法 2内存法 3指针法
第一种速度慢,2 3速度很快,建议用内存法。
//将图像数据直接复制到内存中,提高程序运行速度
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;
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);