Image-Grayscale, grayscale inversion, binary

Source: Internet
Author: User

Original address: http://www.cnblogs.com/gdjlc/archive/2013/03/05/2943801.html

Image grayscale: The process of transforming color image into grayscale image is a grayscale processing. 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 a pixel can have more than 16 million (255*255*255) The range of color changes. 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, which expresses the grayscale value of the image with this luminance value. /// <summary>      ///Grayscale of images/// </summary>      /// <param name= "BMP" ></param>      /// <returns></returns>       Public StaticBitmap Togray (Bitmap bmp) { for(inti =0; I < BMP. Width; i++)          {               for(intj =0; J < BMP. Height; J + +)              {                  //gets the RGB color of the pixel of the pointColor color =bmp.                  GetPixel (i, j); //calculate grayscale values using formulas                  intGray = (int) (color. R0.3+ Color. G0.59+ Color. B0.11); Color Newcolor=Color.FromArgb (Gray, Gray, gray); Bmp.              SetPixel (i, J, Newcolor); }          }          returnbmp; Gray inversion: Set the value of the R, G, and b three components of each pixel to be set to 255,255 for 0. /// <summary>      ///Image Grayscale Inversion/// </summary>      /// <param name= "BMP" ></param>      /// <returns></returns>       Public StaticBitmap grayreverse (Bitmap bmp) { for(inti =0; I < BMP. Width; i++)          {               for(intj =0; J < BMP. Height; J + +)              {                  //gets the RGB color of the pixel of the pointColor color =bmp.                  GetPixel (i, j); Color Newcolor= Color.FromArgb (255-Color. R255-Color. G255-color.                  B); Bmp.              SetPixel (i, J, Newcolor); }          }          returnbmp; } grayscale image binary: After grayscale processing, each pixel in the image has only one value, that is, the gray 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>        ///Image binary 1: The average grayscale of the image is taken as a threshold, all is 0 below this value, and all above this value is 255/// </summary>        /// <param name= "BMP" ></param>        /// <returns></returns>         Public StaticBitmap 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 + +)                {                    //gets the RGB color of the pixel of the pointColor 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>        ///Image binary 2/// </summary>        /// <param name= "img" ></param>        /// <returns></returns>         Public StaticBitmap 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 =New byte[(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; }

Image-Grayscale, grayscale inversion, binary

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.