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

来源:互联网 发布:苏州爱知电机 编辑:程序博客网 时间:2024/06/08 21:08


[函数代码]

        /// <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;            }           }

原创粉丝点击