Matrix transformation in GDI +

zhaozj2021-02-11  199

First, color transform ColorMatrix

ColorMatrix is ​​located in system.drawing.Imaging namespace. Its constructor has two, public color quatrix (); public color, (SINGLE [] []); if the parameter is empty, you can specify each value of the color matrix in the program, or it can be transmitted directly in the constructor. The matrix value, which uses the second constructor. Although the Argb vector is explained in the SDK document as Alpha, Red (red), Green (green), Blue (blue), and W, in fact, the order should be RGBAW, Alpha is the fourth vector, such as [R G B A W]. In the color matrix, the value of the 5th row 5 column must be 1, each value in 5 columns must be 0 (divided by 5 rows 5), which ensures that the value of the W vector of the final resulting color vector is still 1 Why is Microsoft to add a W vector, slowly explain

Color transform formula: [R G B a W] = [R0 G0 B0 A0 W0] M

Can obtain r = A00r0 A10g0 A20b0 A30a0 A40w0g = A01r0 A11g0 A21b0 A31a0 A41w0b = A02r0 A12g0 A22b0 A32a0 A42w0a = A03r0 A13g0 A23b0 A33a0 A43w0w = A04r0 A14g0 A24b0 A34a0 A44w0 because w0 = 1, A04 = A14 = A24 = 14 = 0, A44 = 1 So the W is 1 everyone can see the new R, G, B, and A vector except for the value R, G, B, A related to the original color. However, it is also possible to increase the value-independent value of the original color, which is A40W0, A41W0, A42W0, A43W0, which is why the plus has a vector W. The values ​​of different matrices, attention A40, A41, A42, A43 are generally valued in [-1, 1], otherwise, once the value is exceeded, the maximum value of the color is easily reached. Such as

Show out is full white you can look at the image processing of several color matrices I constructed

Red channel

GREEN channel

Blue channel

Alpha channel

Grayscale

Main code:

Bitmap Bitmap = New Bitmap ("Smallnest.bmp");

// Initialize the color matrix. Float [] [] matrixitems = {new float [] {0.4f, 0.2F, 0.4F, 0, 0}, new float [] {0.4F, 0.2F, 0.4F, 0, 0}, new float [] {0.4F, 0.2F, 0.4F, 0, 0}, new float [] {0, 0, 0, 1, 0}, new float [] {0, 0, 0, 0 , 1}}; ColorMatrix ColorMatrix = New ColorMatrix (MatrixItems);

// Create an ImageAttributes object and set its color matrix ImageAttributes imageAtt = new ImageAttributes ();. ImageAtt.SetColorMatrix (colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

转载请注明原文地址:https://www.9cbs.com/read-4822.html

New Post(0)