C # image grayscale, grayscale inversion, binary

Source: Internet
Author: User

grayscale of images :
The process of transforming color images into grayscale images becomes the grayscale processing of images. The color of each pixel in a color image is determined by the R, G, and b three components, and each component has a value of 255, so that a pixel can have a range of more than 16 million (255*255*255) color variations. and gray image is R, G, b three components of a special color image, the range of one pixel change is 255, so in the digital image processing species generally first convert the image of various formats into grayscale image so that the subsequent image of the calculation of less. The description of grayscale image still reflects the distribution and characteristics of the whole and local chroma and luminance levels of the whole image as well as the color image. Grayscale processing of images can be achieved in two ways.
The first method causes the average of the R, G, and b three components of each pixel to be averaged, and then assigns the average value to the three components of the pixel.
The second method is based on the YUV color space, the physical meaning of Y component is the brightness of the point, reflected by this value brightness level, according to the RGB and YUV color space change relationship can be established brightness Y and R, G, b three color components corresponding: Y=0.3R+0.59G+0.11B, The grayscale value of the image is expressed with this luminance value.

/// <summary>      /// 图像灰度化      /// </summary>      /// <param name="bmp"></param>      /// <returns></returns>      publicstaticBitmap ToGray(Bitmap bmp)      {          for(int i = 0; i < bmp.Width; i++)          {              for(intj = 0; j < bmp.Height; j++)              {                  //获取该点的像素的RGB的颜色                  Color color = bmp.GetPixel(i, j);                  //利用公式计算灰度值                  intgray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);                  Color newColor = Color.FromArgb(gray, gray, gray);                  bmp.SetPixel(i, j, newColor);              }          }          return bmp;      }

Grayscale inversion :
Set the value 0 of the R, G, and b three components of each pixel to 255,255 to 0.

/// <summary>      /// 图像灰度反转      /// </summary>      /// <param name="bmp"></param>      /// <returns></returns>      publicstaticBitmap GrayReverse(Bitmap bmp)      {          for(inti = 0; i < bmp.Width; i++)          {              for(intj = 0; j < bmp.Height; j++)              {                  //获取该点的像素的RGB的颜色                  Color color = bmp.GetPixel(i, j);                  Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);                  bmp.SetPixel(i, j, newColor);              }          }          returnbmp;      }

grayscale image binary :
After grayscale processing, each pixel in the image has only one value, which is the grayscale value of the pixel. Its size determines how bright the pixels are. In order to carry out the image processing operation more conveniently, we also need to do a binary processing of the gray image that has been obtained. The two value of the image is to differentiate the pixels in the image into two colors according to certain criteria. In the system, it is processed into black and white color according to the gray value of the pixel. Similar to grayscale, the two value of the image also has many mature algorithms. It can adopt the adaptive threshold method or the given threshold method.

  /// <summary>        /// 图像二值化1:取图片的平均灰度作为阈值,低于该值的全都为0,高于该值的全都为255        /// </summary>        /// <param name="bmp"></param>        /// <returns></returns>        publicstaticBitmap ConvertTo1Bpp1(Bitmap bmp)        {            intaverage = 0;            for(inti = 0; i < bmp.Width; i++)            {                for(intj = 0; j < bmp.Height; j++)                {                    Color color = bmp.GetPixel(i, j);                    average += color.B;                                    }            }            average = (int)average / (bmp.Width * bmp.Height);            for(inti = 0; i < bmp.Width; i++)            {                for(intj = 0; j < bmp.Height; j++)                {                    //获取该点的像素的RGB的颜色                    Color color = bmp.GetPixel(i, j);                    intvalue = 255 - color.B;                    Color newColor = value > average ? Color.FromArgb(0, 0, 0): Color.FromArgb(255,255, 255);                                       bmp.SetPixel(i, j, newColor);                }            }            returnbmp;        }                /// <summary>        /// 图像二值化2        /// </summary>        /// <param name="img"></param>        /// <returns></returns>        publicstaticBitmap ConvertTo1Bpp2(Bitmap img)        {            intw = img.Width;            inth = img.Height;            Bitmap bmp = newBitmap(w, h, PixelFormat.Format1bppIndexed);            BitmapData data = bmp.LockBits(newRectangle(0, 0, w, h), ImageLockMode.ReadWrite,PixelFormat.Format1bppIndexed);            for(inty = 0; y < h; y++)            {                byte[] scan = newbyte[(w + 7) / 8];                for(intx = 0; x < w; x++)                {                    Color c = img.GetPixel(x, y);                    if(c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));                }                Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length);            }            returnbmp;        }

C # image grayscale, grayscale inversion, binary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.