c# 中图像的简单二值化处理
图像二值化,是很多图像处理中都会用到的基础操作之一,在c#我们可以使用GDI+对图像进行二值化的处理:
public void Binarization(string filePath,string newFilePath)
{
Bitmap btmp = new Bitmap(filePath);
BitmapData btmpd = btmp.LockBits(new Rectangle(0, 0, btmp.Width, btmp.Height), ImageLockMode.ReadWrite, btmp.PixelFormat);
//32位深度为4,24位为3
int PixelSize = 3;
unsafe
{
for (int i = 0; i < btmp.Height; i++)
{
for (int j = 0; j < btmp.Width; j++)
{
byte* row = (byte*)btmpd.Scan0 + PixelSize * j + (i * btmpd.Stride);
if (row[0] + row[1] + row[2] > 382)
{
row[0] = row[1] = row[2] = 255;
continue;
}
row[0] = row[1] = row[2] = 0;
}
}
}
btmp.Save(newFilePath);
}