Application of GDI + in Delphi-colormatrix and grayscale image

Source: Internet
Author: User
The process of converting an image from a color to a gray scale is called grayscale or de-colored. The grayscale principle of color images is very simple, that is, the R, G, and B components of each pixel of the image can be obtained into the same value by some calculation method. There are several calculation methods: 1. The values of R, G, and B of each pixel are the average values of R, G, and B of the pixel; 2. The R, G, and B values of each pixel are the maximum values in R, G, and B. 3. Based on the Principle of YUV color space, the R, G, and B values of each pixel are: Y = 0.3r + 0.59G + 0.11b. In the specific application of the Delphi Program, the process of point-by-point value, calculation and modification of each pixel of the image is generally implemented, which is cumbersome and inefficient. Using GDI +, you can easily set the colormatrix array. The following is a gray-scale Delphi Process for color images based on the principle of YUV color space:
Implementation
 
Uses gdiplus, gdiptypes;
 
// Grayscale Color Image
Procedure grayimage (image: tgpimage );
Const
Colormatrix: tcolormatrix =
(0.3, 0.3, 0.3, 0.0, 0.0), // red
(0.59, 0.59, 0.59, 0.0, 0.0), // green
(0.11, 0.11, 0.11, 0.0, 0.0), // blue
(0.0, 0.0, 0.0, 1.0, 0.0), // alpha
(0.0, 0.0, 0.0, 0.0, 1.0 ));
VaR
TMP: tgpimage;
ATTR: tgpimageattributes;
G: tgpgraphics;
Begin
TMP: = image. Clone;
G: = tgpgraphics. Create (image );
ATTR: = tgpimageattributes. Create;
Try
ATTR. setcolormatrix (colormatrix );
G. drawimage (TMP, gprect (0, 0, image. Width, image. Height ),
0, 0, TMP. Width, TMP. Height, utpixel, ATTR );
Finally
G. Free;
ATTR. Free;
TMP. Free;
End;
End;
// Test
Procedure tform1.button1click (Sender: tobject );
VaR
Image: tgpimage;
G: tgpgraphics;
Begin
Image: = tgpimage.create('.media41001.jpg ');
Grayimage (image );
G: = tgpgraphics. Create (handle, false );
G. drawimage (image, 10, 10 );
G. Free;
Image. Free;
End;

It can be seen that the implementation code in the process is quite simple, mainly the code of two sentences between try... finally, and the key role is this colormatrix.

For colormatrix, the related materials are described as follows:

GDI + providesImageAndBitmapClass.ImageAndBitmapThe object stores the color of each pixel as 32 bits: red, green, blue, and Alpha each occupy 8 bits. The values of these four components are from 0 to 255. 0 indicates no brightness, and 255 indicates the maximum brightness. The Alpha component specifies the transparency of the color. 0 indicates full transparency, and 255 indicates full opacity.

The color vector is in the form of 4 tuples (red, green, blue, and alpha ). For example, a color vector (0,255, 0,255) indicates an opaque color without red or blue but green reaching the maximum brightness. Another convention for color is to use number 1 to indicate that the brightness reaches the maximum. With this convention, the colors described in the previous section are represented by (0, 1, 0, 1. GDI + uses 1 to represent the maximum brightness during color conversion. Linear transformation (rotation, scaling, etc.) can be applied to the color vector by multiplying the color vectors with a 4 × 4 matrix. However, you cannot use a 4x4 matrix for translation (non-linear ). If you add a virtual 5th coordinate (for example, number 1) to each color vector, you can use the 5-xx matrix to apply any combination form of linear transformation and shift. A transformation composed of linear transformations followed by translation is called an affine transformation.
In principle, colormatrix is not difficult to understand. It mainly converts color vectors linearly or non-linearly through matrix operations to process various numbers of images. I have seen on the Internet that many people do not understand the virtual space in the matrix, and the answer may be clear, but it is always hard to understand. Sometimes people get confused and confused, it seems that this concept can only be discussed. In fact, the above answer is already very clear. The 4*4 matrix can only undergo linear color transformation, rather than non-linear transformation and peaceful shift. This means four people sit in four chairs, you cannot shift each other. If you add an empty chair, you can easily shift it. This empty chair is a virtual space! If you still don't understand it, you can take a look at "The Great Tang Dynasty and the Double Dragon Biography". It is estimated that many people have read it. this virtual space is the so-called "Let's go" in the novel "! ^_^ From the perspective of practical application, it is not that easy to use colormatrix skillfully. At the same time, it is necessary to have good knowledge of mathematics and computer graphics and a high level of programming capabilities in order to achieve the desired use. I have not fully understood some of its specific applications because of its poor performance in various aspects (for example, the article "Application of GDI + in Delphi Program-adjusting image brightness" published yesterday, I know that you can use colormatrix to adjust the brightness of an image without having to calculate it point by point. However, after several experiments, the image can be adjusted, but it is always unsatisfactory ). Postscript: colormatrix can be used to adjust the image brightness. For details, see "Application of GDI + in Delphi-colormatrix and image brightness" (2007.11.17)

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.