首页 > 学院 > 开发设计 > 正文

图像二值化 黑白处理

2019-11-08 03:27:19
字体:
来源:转载
供稿:网友
//定义图像二值化函数PRivatestatic Bitmap PBinary(Bitmap src,intv){ intw = src.Width; inth = src.Height; Bitmap dstBitmap = newBitmap(src.Width ,src.Height ,System .Drawing .Imaging .PixelFormat .Format24bppRgb ); System.Drawing.Imaging.BitmapData srcData = src.LockBits(newRectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(newRectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe {  byte* pIn = (byte*)srcData.Scan0.ToPointer();  byte* pOut = (byte*)dstData.Scan0.ToPointer();  byte* p;  intstride = srcData.Stride;  intr, g, b;  for(inty = 0; y < h; y++)  {  for(intx = 0; x < w; x++)  {   p = pIn;   r = p[2];   g = p[1];   b = p[0];   pOut [0]=pOut [1]=pOut [2]=(byte)(((byte)(0.2125 * r + 0.7154 * g + 0.0721 * b) >= v)   ? 255 : 0);   pIn += 3;   pOut += 3;  }  pIn += srcData.Stride - w * 3;  pOut += srcData.Stride - w * 3;  }  src.UnlockBits(srcData );  dstBitmap.UnlockBits(dstData );  returndstBitmap; }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表