C # two methods for obtaining Grayscale Images

Source: Internet
Author: User

 

In the development of image processing programs, I often encounter the situation of converting a color image into a gray image. I have encountered this in a recent project. After trying to solve the problem, I thought it was necessary to share it, so I wrote this article. In this article, we will introduce you to the two methods to implement this transformation, which are two methods that I have used successively. This example is written in C # And the integrated development environment is Visual Studio 2005.

 

First, call the getpixel/setpixel method directly.

We all know that the form of an image in a computer is a bitmap, that is, a rectangle dot matrix. Each vertex is called a pixel. In this method, we use the getpixel method provided by GDI + to read and calculate the pixel color, then, use the setpixel method to apply the calculated color value to the corresponding pixel to obtain the grayscale image.

The "calculation" mentioned above refers to the calculation of gray-scale images. The formula is as follows:

R = (the red component of the pixel + the green component of the pixel + the blue component of the pixel)/3

The final R is the value that needs to be applied to the original pixel. The specific programming implementation is also very simple. You only need to traverse each pixel of the bitmap, and then use the setpixel method to apply the value calculated above. The main code is as follows:

 

Color currentcolor;

Int R;

Bitmap currentbitmap = new Bitmap (picbox. Image );

Graphics G = graphics. fromimage (currentbitmap );

 

For (int w = 0; W <currentbitmap. width; W ++)

{

For (INT h = 0; H <currentbitmap. height; H ++)

{

Currentcolor = currentbitmap. getpixel (W, H );

R = (currentcolor. R + currentcolor. G + currentcolor. B)/3;

Currentbitmap. setpixel (W, H, color. fromargb (R, R, R ));

}

}

G. drawimage (currentbitmap, 0, 0 );

 

Picbox. Image = currentbitmap;

G. Dispose ();

 

The above code is very simple and does not need to be explained too much. Note that when using the setpixel method, the three-color component value is the result R calculated by our formula.

 

Second, use the colormatrix class

If you have tested the first method, you will find that it is time-consuming to traverse all the pixels of the bitmap cyclically and use the setpixel method to modify the color components of each pixel. The second method is a better implementation method-using the colormatrix class.

Before introducing the specific implementation, it is necessary to introduce the background knowledge to the reader. In GDI +, the color is saved in 32 bits. Red, green, blue, and transparency take 8 bits, so each component can be 28 = 256 (0 ~ (255. In this way, a color information can be expressed by a vector (red, green, blue, alpha). For example, an opaque red color can be expressed as (0,255 ). The Alpha value in the vector is used to indicate the transparency of the color. 0 indicates completely transparent, and 255 indicates completely opaque. Readers may think that we only need to change the values of each component in these vectors according to certain rules to get a variety of color transformations, therefore, we can achieve the grayscale image requirement.

The key issue now is to change the component values according to the rules. In the first method described above, we mentioned the formula for calculating the grayscale image. In fact, it has another representation, as shown below:

R = pixel red component x 0. 299 + pixel green component x 0. 587 + pixel blue component x 0. 114

This formula is our rule. We only need to make the above changes for each color vector. The colormatrix class is needed for the transformation here. This class definition is in system. drawing. in the imaging namespace, it defines an array of 5x5 to record the values calculated by multiplication with the color vector. Colormatrix objects are used together with the imageattributes class. In fact, the color transformation in GDI + is performed through the setcolormatrix of the imageattributes object.

The main implementation code of the second type is as follows:

 

Bitmap currentbitmap = new Bitmap (picbox. Image );

Graphics G = graphics. fromimage (currentbitmap );

Imageattributes IA = new imageattributes ();

 

Float [] [] colormatrix = {

New float [] {0.299f, 0.299f, 0.299f, 0, 0 },

New float [] {0.587f, 0.587f, 0.587f, 0, 0 },

New float [] {0.114f, 0.114f, 0.114f, 0, 0 },

New float [] {0, 0, 0, 1, 0 },

New float [] {0, 0, 0, 0, 1 }};

Colormatrix CM = new colormatrix (colormatrix );

IA. setcolormatrix (CM, colormatrixflag. Default, coloradjusttype. Bitmap );

G. drawimage (currentbitmap, new rectangle (0, 0, currentbitmap. width, currentbitmap. height), 0, 0, currentbitmap. width, currentbitmap. height, graphicsunit. pixel, Ia );

 

Picbox. Image = currentbitmap;

G. Dispose ();

 

Careful readers may ask that the color is composed of four components, so the matrix to be multiplied should also be a 4 × 4 matrix, why is the color transformation matrix defined here 5 × 5? This problem can be solved by referring to an article on msdn ("using color matrix to transform monochrome.

Ask readers to enter the above Code and compile and execute it. Everyone will find that the transformation speed is extremely fast, almost instantly completed. In fact, as long as you know the required parameter values of each row and column of the Matrix, It is very convenient to use colormatrix to implement color transformation. This method is also very convenient and efficient for functions such as making a color transparent.

 

The preceding section briefly introduces two methods for obtaining grayscale images. In actual development, the second method is far better than the first method, and its advantage in computing speed is far from comparable.

 

Refer:

Http://blog.csdn.net/maozefa/archive/2007/12/27/1995949.aspx

<GDI + for VCL basics-Color adjustment matrix colormatrix details>

 

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.