c#图像处理-二值化

来源:互联网 发布:sql语句 编辑:程序博客网 时间:2024/06/14 10:42
  public static Bitmap BitmapToBlack(Bitmap img, Double hsb)        {            int w = img.Width;            int h = img.Height;            Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);            BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);//将 Bitmap 锁定到系统内存中            for (int y = 0; y < h; y++)            {                byte[] scan = new byte[(w + 7) / 8];                for (int x = 0; x < w; x++)                {                    Color c = img.GetPixel(x, y);                    if (c.GetBrightness() >= hsb)                        scan[x / 8] |= (byte)(0x80 >> (x % 8));//亮度值和原来比较,二值化处理                }                Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length);            }            bmp.UnlockBits(data);//将 Bitmap 锁定到系统内存中            return bmp;        }

处理前:

处理后:

原创粉丝点击