图像灰度化

  1. /// <summary>  
  2. /// 内存转换法  
  3. /// </summary>  
  4. /// <param name="img"></param>  
  5. public static Bitmap GrayByMemory(Bitmap bmp)  
  6. {  
  7.     if (bmp == null)  
  8.         return bmp;  
  9.       
  10.     //位图矩形  
  11.     Rectangle bmpRect = new Rectangle(0, 0, bmp.Width, bmp.Height);  
  12.     //以可读写的方式锁定全部位图像素  
  13.     BitmapData bmpData = bmp.LockBits(bmpRect, ImageLockMode.ReadWrite, bmp.PixelFormat);  
  14.     //得到首地址  
  15.     IntPtr ptr = bmpData.Scan0;  
  16.     //获取位图的总字节数  
  17.     int bytes = bmpData.Stride * bmp.Height;  
  18.     //定义出位图数组  
  19.     byte[] rgbValues = new byte[bytes];  
  20.     //把锁定的像素复制到数组内  
  21.     Marshal.Copy(ptr, rgbValues, 0, bytes);  
  22.   
  23.     //灰度化  
  24.     double grayColor = 0;  
  25.     //逐行扫描  
  26.     for (int y = 0; y < bmpData.Height; y++)  
  27.     {  
  28.         //只处理每行中图像像素的数据,舍弃未用空间  
  29.         for (int x = 0; x < bmpData.Width * 3; x += 3)  
  30.         {  
  31.             grayColor = rgbValues[y * bmpData.Stride + x + 2] * 0.299  
  32.                         + rgbValues[y * bmpData.Stride + x + 1] * 0.587  
  33.                         + rgbValues[y * bmpData.Stride + x] * 0.114;  
  34.             rgbValues[y * bmpData.Stride + x] =   
  35.             rgbValues[y * bmpData.Stride + x + 1] =   
  36.             rgbValues[y * bmpData.Stride + x + 2] = (byte)grayColor;  
  37.         }  
  38.     }  
  39.   
  40.     //把数据复制会位图  
  41.     Marshal.Copy(rgbValues, 0, ptr, bytes);  
  42.   
  43.     //解锁位图像素  
  44.     bmp.UnlockBits(bmpData);  
  45.   
  46.     return bmp;  

图片灰度反转

  1. /// <summary>  
  2. /// 图片灰度反转  
  3. /// </summary>  
  4. /// <param name="bmp"></param>  
  5. /// <returns></returns>  
  6. public static Bitmap Reverse(Bitmap bmp)  
  7. {  
  8.     if (bmp == null)  
  9.         return bmp;  
  10.   
  11.     Rectangle bmpRect = new Rectangle(0, 0, bmp.Width, bmp.Height);  
  12.     BitmapData bmpData = bmp.LockBits(bmpRect, ImageLockMode.ReadWrite, bmp.PixelFormat);  
  13.     int bytes = bmpData.Stride * bmp.Height;  
  14.     byte[] rgbValues = new byte[bytes];  
  15.     IntPtr ptr = bmpData.Scan0;  
  16.     Marshal.Copy(ptr, rgbValues, 0, bytes);  
  17.   
  18.     for (int y = 0; y < bmp.Height; y++)  
  19.         for (int x = 0; x < bmp.Width * 3; x ++)  
  20.             rgbValues[y * bmpData.Stride + x] = (byte)(255 - rgbValues[y * bmpData.Stride + x]);  
  21.   
  22.     Marshal.Copy(rgbValues, 0, ptr, bytes);  
  23.     bmp.UnlockBits(bmpData);  
  24.   
  25.     return bmp;  


  26. 平移图像 
            /// <summary>
            /// 平移图像
            /// </summary>
            /// <param name="bmp"></param>
            /// <param name="tx"></param>
            /// <param name="ty"></param>
            /// <returns></returns>
            public static Bitmap Translation(Bitmap bmp, int tx, int ty)
            {
                if (bmp == null || (tx == 0 && ty == 0))
                    return bmp;

                Rectangle bmpRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData bmpData = bmp.LockBits(bmpRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
                int bytes = bmpData.Stride * bmp.Height;
                byte[] rgbValues = new byte[bytes];
                IntPtr ptr = bmpData.Scan0;
                Marshal.Copy(ptr, rgbValues, 0, bytes);

                byte[] newRgbValues = new byte[bytes];
                for (int i = 0; i < bytes; i++)
                    newRgbValues[i] = 255;

                for (int y = 0; y < bmp.Height; y++)
                {
                    if (y + ty >= bmp.Height || y + ty < 0)
                        continue;

                    for (int x = 0; x < bmp.Width * 3; x++)
                    {
                        if (x + tx * 3 >= bmp.Width * 3 || x + tx * 3 < 0)
                            continue;

                        newRgbValues[(y + ty) * bmpData.Stride + x + tx * 3] = rgbValues[y * bmpData.Stride + x];
                    }
                }

                Marshal.Copy(newRgbValues, 0, ptr, bytes);
                bmp.UnlockBits(bmpData);

                return bmp;
            }
  27. 去杂色,转换为黑白图片
    1. /// <summary>  
    2. /// 去杂色,转换为黑白图片  
    3. /// </summary>  
    4. /// <param name="img"></param>  
    5. public static void Binarization(Bitmap img, int thresholdValue)  
    6. {  
    7.     for (int x = 0; x < img.Width; x++)  
    8.     {  
    9.         for (int y = 0; y < img.Height; y++)  
    10.         {  
    11.             Color c = img.GetPixel(x, y);  
    12.             if (c.R >= thresholdValue)  
    13.                 img.SetPixel(x, y, Color.FromArgb(255, 255, 255));  
    14.             else  
    15.                 img.SetPixel(x, y, Color.FromArgb(0, 0, 0));  
    16.         }  
    17.     }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值