Citation: http://www.chinabaike.com/t/37396/2014/0624/2556217.html
Android Matrix class and ColorMatrix class
Recently in the system to learn the image processing of Android (on the Internet to collect some information and write their own test procedures, did the collation), now do a summary:
First, ColorMatrix class
ColorMatrix is a 5x4-order matrix shown below as a, the first line represents the R red component, the second line represents the G green component, the third line represents the B blue component, and the fourth line represents transparency:
One-dimensional arrays are stored in the following ways: [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, p, Q, R, S, T],
After multiplying the color matrix and color components, the new colors are as follows:
R ' = a*r + b*g + c*b + d*a + E; Red component g ' = F*r + g*g + h*b + i*a + j; Green component B ' = k*r + l*g + m*b + n*a + o; Blue component A ' = p*r + q*g + r*b + s*a + t; The fifth column of the transparency color matrix represents the offsets for each color.
Here's a look at the common APIs for ColorMatrix:
public void set (float[] src) assigns the value of the SRC array to the color matrix Apublic final float[] GetArray () returns the specific value of the color matrix A, representing the public void Setconcat in the first-order array form ( ColorMatrix Mata,colormatrix MATB) The color matrix MatA and MATB Composite, quite with the image of the MatA matrix processing and then the matrix MATB processing. public void Postconcat (ColorMatrix postmatrix) if Mata.postconcat (Postmatrix) is equivalent to Setconcat (Postmatrix,mata). public void Preconcat (ColorMatrix prematrix) if Mata.preconcat (Prematrix) is equivalent to Setconcat (Mata,prematrix). The public void setRotate (int axis, float degrees) sets the color component rotation: axis==0 rotates red; axis==1 corresponds to green; axis==2 corresponds to Blue,
The source code is as follows:
/*** Set the rotation on a color axis by the specified values.* axis=0 correspond to a rotation around the RED color* axis =1 correspond to a rotation around the GREEN color* axis=2 correspond to a rotation around the BLUE color*/public void set Rotate (int axis, float degrees) {reset (); float radians = degrees * (float) math.pi/180;float cosine = Floatmath.cos (radia (ns); float sine = floatmath.sin (radians); switch (axis) {//Rotation around the red colorcase 0:marray[6] = marray[12] = cos INE;MARRAY[7] = sine;marray[11] =-sine;break;//Rotation around the green colorcase 1:marray[0] = marray[12] = Cosine;mar RAY[2] =-sine;marray[10] = sine;break;//Rotation around the blue colorcase 2:marray[0] = marray[6] = cosine;marray[1] = SINE;MARRAY[5] =-sine;break;default:throw new RuntimeException ();}}
public void Setsaturation (float Sat) sets the saturation parameter of the image by changing the value of the matrix 0 corresponds to the gray image, 1 corresponds unchanged
The public void Setscale (float rscale, float gscale, float bscale, float ascale) sets the r,g,b,a of the matrix to a corresponding multiple.
The corresponding API is Setscale (1,2,1,1); This function actually sets the value on the diagonal of the color matrix.
The following example makes some processing of the image, which is relatively simple:
private void Usecolormatrix (Canvas canvas,paint Paint) {//TODO auto-generated method stub// Clears the brush's color filter paint.setcolorfilter (null); Cmatrix = new ColorMatrix (); Cmatrix.set (Carrycolor);//cmatrix.reset ();// Cmatrix.setsaturation (0F);//cmatrix.setrotate (0, 2, 2, 2);// Set the color matrix filter paint.setcolorfilter (new Colormatrixcolorfilter (Cmatrix)); Canvas.drawbitmap (bitmap, 0,0, paint);}
Second, The Matrix class detailed
The Matrix class is a 3x3 positional coordinate matrix that initializes a matrix that must pass the Reset function or set ... and other methods,
Zooming in and out of the graph, moving, rotating, pivoting, and twisting these effects can all be done with this matrix. The first row of position matrix A controls the x-coordinate, and the second row controls the z-coordinate of the third row of the y-coordinate.
The function of this matrix is to transform the coordinates x, y to calculate the result as follows:
X ' =a*x+b*y+c
Y ' =d*x+e*y+f
From the above formula can be seen c,f control X, and the offset of the Y position, a and E control X, and the y-coordinate multiple changes, so
x enlarges A, y magnifies b,
The matrix is:,
The shift amount in the x direction is y in the direction of the Y, then the coordinates of point P (x, y) are:
x = x0 + x
y = y0 + y
The expression of the matrix is as follows:
The first move and then amplification is the multiplication of matrices such as a*b.
The rotation of the image is slightly more complex: the point at which the P0 (x0, y0) rotates around the origin point is P (x, y). By using vectors, we get the following, R is the radius of rotation:
x0 = R Cosα
Y0 = r sine
x = R cos (α+θ) = x0 cosθ-y0 sinθθ (trigonometric expansion then variable substitution)
y = r sin (α+θ) = x0 sinθ+ y0 cosθ
So we get the matrix:
Image is divided into 2 kinds: horizontal mirror, vertical mirror. This article first describes how to implement vertical mirroring, and what vertical mirrors do not specify. The vertical mirror change of the image can also be expressed as a matrix change,
Point P0 (x0, y0) mirror the corresponding points for P (x, y), the height of the image is Fheight, the width is fwidth, the original image of the P0 (x0, y0) after the vertical image of the coordinates changed (x0, 2*fheight-y0);
x = x0 The x-coordinate after the vertical mirror is not changed
y = fheight–y0 a vertical mirror is equivalent to first rotate the image around the x-axis 180 degrees, and then translate the image down two image height, so first y=-y0; then add twice times the height
The corresponding matrix is deduced as follows:
Final float f[] = {1.0f,0.0f,0.0f,0.0f,-1.0f,120.0f,0.0f,0.0f,1.0f};
Matrix matrix = new Matrix ();
Matrix.setvalues (f);
The code is as follows:
@Override
protected void OnDraw (canvas canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);
Paint paint = mpaint;
Usecolormatrix (Canvas,paint);
Matrix = new Matrix ();
Matrix.setvalues (Carrypos);
Paint.setcolorfilter (NULL);
Canvas.drawbitmap (bitmap,matrix,paint);//You can setcolorfilter (null);
CARRYPOS[5] = 2*bitmap.getheight ();//Set y-coordinate movement
CARRYPOS[4] = -1;//set to 180 degrees around the x-axis
Matrix.setvalues (Carrypos);
Canvas.drawbitmap (bitmap, matrix, paint);
}
Results after running as described above:
In fact, vertical mirroring can also be implemented in the following ways:
Matrix matrix = new Matrix ();
Matrix.setscale (1.0,-1.0);
Matrix.posttraslate (0,2* fheight);
Matrix Learning-The composite change of images
If the image rotates around a point P (A, A, b), the coordinate system is first panned to that point, then rotated, and the rotated image is translated back to the original coordinate origin.
We need 3 steps:
1. Pan-shifts the coordinate system to the point P (A, b);
2. Rotate--Rotate the image at the center of the original point;
3. Pan-translates the rotated image back to the original coordinate origin;
Comparing the geometric changes of the previously mentioned images (basic geometric variations of the images), this requires a translation--rotation--translation, which requires a geometric change of multiple images called composite changes of the image.
The basic changes of the given image are F1, F2, F3 ..., Fn, and their change matrices are T1, T2, T3 ...., Tn, the matrix T of the image composite change can be expressed as: T = TnTn-1 ... T1.
According to the above principle, the sequence of changes of the rotation θ around a point (a, b) is:
In accordance with the above formula, we cite a simple example: rotate around (100,100) 30 degrees (sin = 0.5, cos 30 = 0.866)
Float f[]= {0.866F, -0.5f, 63.4f,0.5f, 0.866f,-36.6f,0.0f, 0.0F, 1.0F};
Matrix = new Matrix ();
Matrix.setvalues (f);
The rotated image is as follows:
Android provides us with a much simpler approach, as follows:
Matrix matrix = new Matrix ();
Matrix.setrotate (30,100,100);
Actual results after the matrix is run:
It's exactly the same as the matrix we get from the formula before.
Here we provide another way to achieve the same effect:
float a = 100.0f,b = 100.0F;
Matrix = new Matrix ();
Matrix.settranslate (A, b);
Matrix.prerotate (30);
Matrix.pretranslate (-a,-b); Equivalent to Matrix=matrix*t (the matrix that moves the corresponding distance) post ... (the product factor of the pre is exchanged)
Matrix Learning-the wrong cut transformation
What is the wrong-cut transformation of an image (Shear transformation)? We're still looking directly at the picture. The effect of the error-cut transformation is:
is the second parameter with the X vector set to 0.5F that is x=x0+0.5y0 so that x increases with Y to a linear change of slope of 0.5.
The code is as follows:
Private Float[]carrypos2 ={1,0,100,
0,1,100,
0,0,1};//z No change
float[] test01 = Carrypos2;
TEST01[1] = 0.5F;
Matrix.setvalues (test01);
Canvas.drawbitmap (bitmap, matrix, paint);
You can also write this:
Make a summary of the image's error-cutting transformation:
x = x0 + b*y0;
y = d*x0 + y0;
Here again, let's introduce a place to note:
By the above, we found that the Matrix's setxxxx () function called Reset () at the time of the call, which needs to be noted in the composite transformation.
Matrix Learning-Symmetric transformations (reflection)
What is a symmetric transformation? The concrete theory does not elaborate, the image is a kind of symmetry transformation.
Using the above summary to make a concrete example, and the straight line y=–x symmetrical reflection graphics, the code snippet is as follows:
The current matrix output is:
The effect of image transformation is as follows:
Attached: Trigonometric formula
Two corners and formulas
Sin (a+b) =sinacosb+cosasinb
Sin (A-B) =sinacosb-sinbcosa
cos (a+b) =cosacosb-sinasinb
cos (A-B) =cosacosb+sinasinb
Tan (a+b) = (TANA+TANB)/(1-TANATANB)
Tan (A-B) = (TANA-TANB)/(1+TANATANB)
Cot (a+b) = (cotacotb-1)/(Cotb+cota)
Cot (A-B) = (cotacotb+1)/(Cotb-cota)
Double Angle formula
Tan2a=2tana/[1-(Tana) ^2]
Cos2a= (cosa) ^2-(Sina) ^2=2 (cosa) ^2-1=1-2 (Sina) ^2
Sin2a=2sina*cosa
Half-width formula
Sin (A/2) =√ ((1-cosa)/2) sin (A/2) =-√ ((1-cosa)/2)
cos (A/2) =√ ((1+cosa)/2) cos (A/2) =-√ ((1+cosa)/2)
Tan (A/2) =√ ((1-cosa)/((1+cosa)) tan (A/2) =-√ ((1-cosa)/((1+cosa))
Cot (A/2) =√ ((1+cosa)/((1-cosa)) cot (A/2) =-√ ((1+cosa)/((1-cosa))
Tan (A/2) = (1-cosa)/sina=sina/(1+cosa)
And the difference of product
2sinacosb=sin (a+b) +sin (A-B)
2cosasinb=sin (a+b)-sin (A-B))
2cosacosb=cos (a+b)-sin (A-B)
-2sinasinb=cos (a+b)-cos (A-B)
Sina+sinb=2sin ((a+b)/2) cos ((A-B)/2
Cosa+cosb=2cos ((a+b)/2) sin ((A-B)/2)
Tana+tanb=sin (a+b)/COSACOSB
Accumulation and Difference formula
Sin (a) sin (b) =-1/2*[cos (a+b)-cos (a)]
cos (a) cos (b) =1/2*[cos (a+b) +cos (a)]
Sin (a) cos (b) =1/2*[sin (a+b) +sin (a)]
Induction formula
Sin (-a) =-sin (a)
Cos (-a) =cos (a)
Sin (pi/2-a) =cos (a)
cos (pi/2-a) =sin (a)
Sin (pi/2+a) =cos (a)
cos (pi/2+a) =-sin (a)
Sin (pi-a) =sin (a)
cos (pi-a) =-cos (a)
Sin (pi+a) =-sin (a)
cos (pi+a) =-cos (a)
Tga=tana=sina/cosa
Universal formula
Sin (a) = (2tan (A/2))/(1+tan^2 (A/2))
cos (a) = (1-tan^2 (A/2))/(1+tan^2 (A/2))
Tan (a) = (2tan (A/2))/(1-tan^2 (A/2))
Other formulas
A*sin (a) +b*cos (a) =sqrt (a^2+b^2) sin (a+c) [Where, Tan (c) =b/a]
A*sin (a)-b*cos (a) =sqrt (a^2+b^2) cos (a-c) [Where, Tan (c) =a/b]
1+sin (a) = (sin (a/2) +cos (A/2)) ^2
1-sin (a) = (sin (a/2)-cos (A/2)) ^2
Other non-focused trigonometric functions
CSC (a) =1/sin (a)
SEC (a) =1/cos (a)
Hyperbolic functions
Sinh (a) = (e^a-e^ (-a))/2
Cosh (a) = (e^a+e^ (-a))/2
Tgh (a) =sinh (a)/cosh (a)
Android Matrix class and Colormatri