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

时间:2022-01-18 04:17:44

C#图片灰度处理(位深度24→位深度8)

#region 灰度处理
/// <summary>
/// 将源图像灰度化,并转化为8位灰度图像。
/// </summary>
/// <param name="original"> 源图像。 </param>
/// <returns> 8位灰度图像。 </returns>
public static Bitmap RgbToGrayScale(Bitmap original)
{
if (original != null)
{
// 将源图像内存区域锁定
Rectangle rect = new Rectangle(, , original.Width, original.Height);
BitmapData bmpData = original.LockBits(rect, ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb); // 获取图像参数
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宽度,表示这是内存区域的大小 // 分别设置两个位置指针,指向源数组和目标数组
int posScan = , posDst = ;
byte[] rgbValues = new byte[scanBytesLength]; // 为目标数组分配内存
Marshal.Copy(ptr, rgbValues, , scanBytesLength); // 将图像数据拷贝到rgbValues中
// 分配灰度数组
byte[] grayValues = new byte[width * height]; // 不含未用空间。
// 计算灰度数组 byte blue, green, red, YUI; for (int i = ; i < height; i++)
{
for (int j = ; j < width; j++)
{ blue = rgbValues[posScan];
green = rgbValues[posScan + ];
red = rgbValues[posScan + ];
YUI = (byte)(0.229 * red + 0.587 * green + 0.144 * blue);
//grayValues[posDst] = (byte)((blue + green + red) / 3);
grayValues[posDst] = YUI;
posScan += ;
posDst++; }
// 跳过图像数据每行未用空间的字节,length = stride - width * bytePerPixel
posScan += offset;
} // 内存解锁
Marshal.Copy(rgbValues, , ptr, scanBytesLength);
original.UnlockBits(bmpData); // 解锁内存区域 // 构建8位灰度位图
Bitmap retBitmap = BuiltGrayBitmap(grayValues, width, height);
return retBitmap;
}
else
{
return null;
}
} /// <summary>
/// 用灰度数组新建一个8位灰度图像。
/// </summary>
/// <param name="rawValues"> 灰度数组(length = width * height)。 </param>
/// <param name="width"> 图像宽度。 </param>
/// <param name="height"> 图像高度。 </param>
/// <returns> 新建的8位灰度位图。 </returns>
private static Bitmap BuiltGrayBitmap(byte[] rawValues, int width, int height)
{
// 新建一个8位灰度位图,并锁定内存区域操作
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpData = bitmap.LockBits(new Rectangle(, , width, height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); // 计算图像参数
int offset = bmpData.Stride - bmpData.Width; // 计算每行未用空间字节数
IntPtr ptr = bmpData.Scan0; // 获取首地址
int scanBytes = bmpData.Stride * bmpData.Height; // 图像字节数 = 扫描字节数 * 高度
byte[] grayValues = new byte[scanBytes]; // 为图像数据分配内存 // 为图像数据赋值
int posSrc = , posScan = ; // rawValues和grayValues的索引
for (int i = ; i < height; i++)
{
for (int j = ; j < width; j++)
{
grayValues[posScan++] = rawValues[posSrc++];
}
// 跳过图像数据每行未用空间的字节,length = stride - width * bytePerPixel
posScan += offset;
} // 内存解锁
Marshal.Copy(grayValues, , ptr, scanBytes);
bitmap.UnlockBits(bmpData); // 解锁内存区域 // 修改生成位图的索引表,从伪彩修改为灰度
ColorPalette palette;
// 获取一个Format8bppIndexed格式图像的Palette对象
using (Bitmap bmp = new Bitmap(, , PixelFormat.Format8bppIndexed))
{
palette = bmp.Palette;
}
for (int i = ; i < ; i++)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
// 修改生成位图的索引表
bitmap.Palette = palette; return bitmap;
}
#endregion

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

#region 二值化

        #region Otsu阈值法二值化模块   

        /// <summary>
/// Otsu阈值
/// </summary>
/// <param name="b">位图流</param>
/// <returns></returns>
public Bitmap OtsuThreshold()
{
// 图像灰度化
// b = Gray(b);
int width = bitmap.Width;
int height = bitmap.Height;
byte threshold = ;
int[] hist = new int[]; int AllPixelNumber = , PixelNumberSmall = , PixelNumberBig = ; double MaxValue, AllSum = , SumSmall = , SumBig, ProbabilitySmall, ProbabilityBig, Probability;
BitmapData bmpData = bitmap.LockBits(new Rectangle(, , width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); unsafe
{
byte* p = (byte*)bmpData.Scan0;
int offset = bmpData.Stride - width * ;
for (int j = ; j < height; j++)
{
for (int i = ; i < width; i++)
{
hist[p[]]++;
p += ;
}
p += offset;
}
bitmap.UnlockBits(bmpData);
}
//计算灰度为I的像素出现的概率
for (int i = ; i < ; i++)
{
AllSum += i * hist[i]; // 质量矩
AllPixelNumber += hist[i]; // 质量
}
MaxValue = -1.0;
for (int i = ; i < ; i++)
{
PixelNumberSmall += hist[i];
PixelNumberBig = AllPixelNumber - PixelNumberSmall;
if (PixelNumberBig == )
{
break;
} SumSmall += i * hist[i];
SumBig = AllSum - SumSmall;
ProbabilitySmall = SumSmall / PixelNumberSmall;
ProbabilityBig = SumBig / PixelNumberBig;
Probability = PixelNumberSmall * ProbabilitySmall * ProbabilitySmall + PixelNumberBig * ProbabilityBig * ProbabilityBig;
if (Probability > MaxValue)
{
MaxValue = Probability;
threshold = (byte)i;
}
}
this.Threshoding(bitmap, threshold);
bitmap = twoBit(bitmap);
return bitmap; ;
}
#endregion #region 固定阈值法二值化模块
public Bitmap Threshoding(Bitmap b, byte threshold)
{
int width = b.Width;
int height = b.Height;
BitmapData data = b.LockBits(new Rectangle(, , width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
byte* p = (byte*)data.Scan0;
int offset = data.Stride - width * ;
byte R, G, B, gray;
for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
R = p[];
G = p[];
B = p[];
gray = (byte)((R * + G * + B * ) >> );
if (gray >= threshold)
{
p[] = p[] = p[] = ;
}
else
{
p[] = p[] = p[] = ;
}
p += ;
}
p += offset;
}
b.UnlockBits(data);
return b; } }
#endregion #region 创建1位图像 /// <summary>
/// 创建1位图像
/// </summary>
/// <param name="srcBitmap"></param>
/// <returns></returns>
public Bitmap twoBit(Bitmap srcBitmap)
{
int midrgb = Color.FromArgb(, , ).ToArgb();
int stride;//简单公式((width/8)+3)&(~3)
stride = (srcBitmap.Width % ) == ? (srcBitmap.Width / ) : (srcBitmap.Width / ) + ;
stride = (stride % ) == ? stride : ((stride / ) + ) * ;
int k = srcBitmap.Height * stride;
byte[] buf = new byte[k];
int x = , ab = ;
for (int j = ; j < srcBitmap.Height; j++)
{
k = j * stride;//因图像宽度不同、有的可能有填充字节需要跳越
x = ;
ab = ;
for (int i = ; i < srcBitmap.Width; i++)
{
//从灰度变单色(下法如果直接从彩色变单色效果不太好,不过反相也可以在这里控制)
if ((srcBitmap.GetPixel(i, j)).ToArgb() > midrgb)
{
ab = ab * + ;
}
else
{
ab = ab * ;
}
x++;
if (x == )
{
buf[k++] = (byte)ab;//每字节赋值一次,数组buf中存储的是十进制。
ab = ;
x = ;
}
}
if (x > )
{
//循环实现:剩余有效数据不满1字节的情况下须把它们移往字节的高位部分
for (int t = x; t < ; t++) ab = ab * ;
buf[k++] = (byte)ab;
}
}
int width = srcBitmap.Width;
int height = srcBitmap.Height;
Bitmap dstBitmap = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
BitmapData dt = dstBitmap.LockBits(new Rectangle(, , dstBitmap.Width, dstBitmap.Height), ImageLockMode.ReadWrite, dstBitmap.PixelFormat);
Marshal.Copy(buf, , dt.Scan0, buf.Length);
dstBitmap.UnlockBits(dt);
return dstBitmap;
} #endregion #endregion