Detailed description of implementation methods based on c # grayscale image, grayscale inversion, and binarization

Source: Internet
Author: User
Tags scale image

Grayscale Images:
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 the R, G, and B components, and each component has a 255 median value, so that a pixel can have more than 16 million (255*255*255) the color change range. A gray scale image is a special color image with the same components of R, G, and B. The change range of a pixel is 255, therefore, in digital image processing, images in various formats are first converted into grayscale images to reduce the computing workload of subsequent images. Like a color image, the gray scale image still reflects the overall and local color and brightness level distribution and features of the entire image. Grayscale image processing can be implemented in two ways.
The first method is to obtain the average values of the R, G, and B components of each pixel, and then assign the average value to the three components of the pixel.
The second method is based on the YUV color space, the physical meaning of the Y component is the brightness of the vertex, which reflects the brightness level, based on the relationship between RGB and YUV color spaces, we can establish the correspondence between brightness Y and brightness R, G, and B: Y = 0.3R + 0.59G + 0.11B, the gray value of the image is expressed by the brightness value. Copy codeThe Code is as follows: // <summary>
/// Grayscale Images
/// </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 ++)
{
// Obtain the RGB color of the pixel of the point
Color color = bmp. GetPixel (I, j );
// Calculate the gray value using the formula
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 value 0 of the R, G, and B components of each pixel to 255,255 to 0.Copy codeThe Code is as follows: // <summary>
/// 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 ++)
{
// Obtain the RGB color of the pixel of the point
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;
}

Grayscale image binarization:
After grayscale processing, each pixel in the image has only one value, that is, the gray value of the pixel. Its size determines the brightness and darkness of a pixel. To facilitate the following image processing operations, we also need to perform a binarization operation on the obtained grayscale images. The binarization of an image is to divide the pixels in the image into two colors according to certain standards. In the system, the gray values of pixels are processed into black and white colors. Similar to grayscale, image binarization also has many mature algorithms. It can adopt the adaptive threshold method or the given threshold method.Copy codeThe Code is as follows: // <summary>
/// Image binarization 1: Take the average gray scale of the image as the threshold value. All values lower than this value are 0, and all values higher than this value are 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 ++)
{
// Obtain the RGB color of the pixel of the point
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 binarization 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 {
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 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.