文档库 最新最全的文档下载
当前位置:文档库 › C#图片二值化处理(位深度8→位深度1)

C#图片二值化处理(位深度8→位深度1)

#region 二值化
/*
1位深度图像 颜色表数组255个元素 只有用前两个 0对应0 1对应255
1位深度图像每个像素占一位
8位深度图像每个像素占一个字节 是1位的8倍
*/
///


/// 将源灰度图像二值化,并转化为1位二值图像。
///

/// 源灰度图像。
/// 1位二值图像。
public static Bitmap GTo2Bit(Bitmap bmp)
{
if (bmp != null)
{
// 将源图像内存区域锁定
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly,
PixelFormat.Format8bppIndexed);

// 获取图像参数
int leng, offset_1bit = 0;
int width = bmpData.Width;
int height = bmpData.Height;
int stride = bmpData.Stride; // 扫描线的宽度,比实际图片要大
int offset = stride - width; // 显示宽度与扫描线宽度的间隙
IntPtr ptr = bmpData.Scan0; // 获取bmpData的内存起始位置的指针
int scanBytesLength = stride * height; // 用stride宽度,表示这是内存区域的大小
if (width % 32 == 0)
{
leng = width / 8;
}
else
{
leng = width / 8 + (4 - (width / 8 % 4));
if (width % 8 != 0)
{
offset_1bit = leng - width / 8;
}
else
{
offset_1bit = leng - width / 8;
}
}

// 分别设置两个位置指针,指向源数组和目标数组
int posScan = 0, posDst = 0;
byte[] rgbValues = new byte[scanBytesLength]; // 为目标数组分配内存
Marshal.Copy(ptr, rgbValues, 0, scanBytesLength); // 将图像数据拷贝到rgbValues中
// 分配二值数组
byte[] grayValues = new byte[leng * height]; // 不含未用空间。
// 计算二值数组
int x, v, t = 0;
for (int i = 0; i < height; i++)
{
for (x = 0; x < width; x++)
{
v = rgbValues[posScan];
t = (t << 1) | (v > 100 ? 1 : 0);


if (x % 8 == 7)
{
grayValues[posDst] = (byte)t;
posDst++;
t = 0;
}
posScan++;
}

if ((x

%= 8) != 7)
{
t <<= 8 - x;
grayValues[posDst] = (byte)t;
}
// 跳过图像数据每行未用空间的字节,length = stride - width * bytePerPixel
posScan += offset;
posDst += offset_1bit;
}

// 内存解锁
Marshal.Copy(rgbValues, 0, ptr, scanBytesLength);
bmp.UnlockBits(bmpData); // 解锁内存区域

// 构建1位二值位图
Bitmap retBitmap = twoBit(grayValues, width, height);
return retBitmap;
}
else
{
return null;
}
}

///


/// 用二值数组新建一个1位二值图像。
///

/// 二值数组(length = width * height)。
/// 图像宽度。
/// 图像高度。
/// 新建的1位二值位图。
private static Bitmap twoBit(byte[] rawValues, int width, int height)
{
// 新建一个1位二值位图,并锁定内存区域操作
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);

// 计算图像参数
int offset = bmpData.Stride - bmpData.Width / 8; // 计算每行未用空间字节数
IntPtr ptr = bmpData.Scan0; // 获取首地址
int scanBytes = bmpData.Stride * bmpData.Height; // 图像字节数 = 扫描字节数 * 高度
byte[] grayValues = new byte[scanBytes]; // 为图像数据分配内存

// 为图像数据赋值
int posScan = 0; // rawValues和grayValues的索引
for (int i = 0; i < height; i++)
{
for (int j = 0; j < bmpData.Width / 8; j++)
{
grayValues[posScan] = rawValues[posScan];
posScan++;
}
// 跳过图像数据每行未用空间的字节,length = stride - width * bytePerPixel
posScan += offset;
}

// 内存解锁
Marshal.Copy(grayValues, 0, ptr, scanBytes);
bitmap.UnlockBits(bmpData); // 解锁内存区域

// 修改生成位图的索引表
ColorPalette palette;
// 获取一个Format8bppIndexed格式图像的Palette对象
using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format1bppIndexed))
{
palette = bmp.Palette

;
}
for (int i = 0; i < 2; i = +254)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
// 修改生成位图的索引表
bitmap.Palette = palette;

return bitmap;
}
#endregion

相关文档