Win8MetroC#数字图像处理--2.2图像二值化函数

时间:2022-07-11 08:29:01

原文:Win8MetroC#数字图像处理--2.2图像二值化函数

Win8MetroC#数字图像处理--2.2图像二值化函数

[函数代码]

        /// <summary>
/// Binary process.
/// </summary>
/// <param name="src">Source image.</param>
/// <param name="threshould">Define a threshould value for binary processing, from 0 to 255.</param>
/// <returns></returns>
public static WriteableBitmap BinaryProcess(WriteableBitmap src, int threshould)////2 二值化处理
{
if(src!=null )
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap binaryImage = new WriteableBitmap(w,h);
byte[] temp = src.PixelBuffer.ToArray();
for (int i = 0; i < temp.Length; i += 4)
{
byte tempByte = (byte)(((temp[i] + temp[i + 1] + temp[i + 2]) / 3) < threshould ? 0 : 255);
temp[i] = tempByte;
temp[i + 1] = tempByte;
temp[i + 2] = tempByte;
}
Stream sTemp = binaryImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return binaryImage;
}
else
{
return null;
}
}

Win8MetroC#数字图像处理--2.2图像二值化函数