Based on c image grayscale grayscale inversion binarization realization method

  • 2020-05-12 03:06:20
  • OfStack

Image graying:
The process of converting a color image into a grayscale image becomes the grayscale processing of the image. The color of each pixel in a color image is determined by three components: R, G, and B. Each component has a median value of 255, so that a pixel can have a color range of more than 16 million (255*255*255) colors. The grayscale image is a special color image with the same components of R, G and B, and the variation range of one pixel is 255. Therefore, in digital image processing, images in various formats are first converted into grayscale images to reduce the calculation quantity of subsequent images by 1. The description of gray image and color image 1 still reflect the distribution and characteristics of the overall and local chromaticity and brightness levels of the whole image. Image graying can be achieved by two methods.
In the first method, the average of the three components of R, G and B of each pixel is calculated, and then the average value is assigned to the three components of this pixel.
The second method is according to the color space of YUV, the physical meaning of the component of Y is the brightness of the point, which reflects the brightness level. According to the changing relationship of the color space of RGB and YUV, the corresponding values of the three color components of Y and R, G and B can be established: Y=0.3R+0.59G+0.11B can be used to express the grayscale value of the image.

/// <summary> 
      ///  Image graying  
      /// </summary> 
      /// <param name="bmp"></param> 
      /// <returns></returns> 
      public static Bitmap ToGray(Bitmap bmp) 
      { 
          for (int i = 0; i < bmp.Width; i++) 
          { 
              for (int j = 0; j < bmp.Height; j++) 
              { 
                  // Gets the pixel of the point RGB The color of the  
                  Color color = bmp.GetPixel(i, j); 
                  // The formula is used to calculate the gray value  
                  int gray = (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 values of R, G and B of each pixel to 255,255 and 0.

/// <summary> 
      ///  Image grayscale inversion  
      /// </summary> 
      /// <param name="bmp"></param> 
      /// <returns></returns> 
      public static Bitmap GrayReverse(Bitmap bmp) 
      { 
          for (int i = 0; i < bmp.Width; i++) 
          { 
              for (int j = 0; j < bmp.Height; j++) 
              { 
                  // Gets the pixel of the point RGB The color of the  
                  Color color = bmp.GetPixel(i, j); 
                  Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B); 
                  bmp.SetPixel(i, j, newColor); 
              } 
          } 
          return bmp; 
      }

Gray image 2 valuation:
After the graying process, each pixel in the image has only one value, that is, the gray value of the pixel. Its size determines the brightness of the pixels. In order to carry out the following image processing operation more conveniently, it is also necessary to do one 2-valued processing for the gray image that has been obtained. The 2-valued image is to divide the pixels in the image into two colors according to the standard of 1. In the system, the gray value of pixels is processed into black and white colors. Similar to graying, there are a lot of mature algorithms for 2-valued images. It can use the adaptive threshold method, also can use the given threshold method.

  /// <summary> 
        ///  image 2 threshold 1 : take the average grayscale of the picture as the threshold value, and all below it are 0 , are all higher than the value 255 
        /// </summary> 
        /// <param name="bmp"></param> 
        /// <returns></returns> 
        public static Bitmap ConvertTo1Bpp1(Bitmap bmp) 
        { 
            int average = 0; 
            for (int i = 0; i < bmp.Width; i++) 
            { 
                for (int j = 0; j < bmp.Height; j++) 
                { 
                    Color color = bmp.GetPixel(i, j); 
                    average += color.B;                     
                } 
            } 
            average = (int)average / (bmp.Width * bmp.Height); 

            for (int i = 0; i < bmp.Width; i++) 
            { 
                for (int j = 0; j < bmp.Height; j++) 
                { 
                    // Gets the pixel of the point RGB The color of the  
                    Color color = bmp.GetPixel(i, j); 
                    int value = 255 - color.B; 
                    Color newColor = value > average ? Color.FromArgb(0, 0, 0): Color.FromArgb(255, 

255, 255);                    
                    bmp.SetPixel(i, j, newColor); 
                } 
            } 
            return bmp; 
        } 

        /// <summary> 
        ///  image 2 threshold 2 
        /// </summary> 
        /// <param name="img"></param> 
        /// <returns></returns> 
        public static Bitmap ConvertTo1Bpp2(Bitmap img) 
        { 
            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); 
            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() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8)); 
                } 
                Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length); 
            } 
            return bmp; 
        }


Related articles: