Android matrix and colormatrix

Source: Internet
Author: User

Recently, I learned about Android Image Processing in the system (I collected some information on the Internet and compiled my own testing program.) Here I will summarize:

I. colormatrix class

Colormatrix is a 5x4 matrix. The first line indicates the r Red component, the second line indicates the G green component, the third line indicates the B blue component, and the fourth line indicates the transparency:


The storage method of one-dimensional arrays is as follows: [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, p, Q, R, S, T],

After the color matrix and the color component are multiplied, the new color is as follows:

R' = A * r + B * G + C * B + D * A + E; // red weight

G' = f * r + G * g + H * B + I * A + J; // green component

B '= K * r + L * g + M * B + N * A + O; // blue weight

A' = p * r + Q * g + R * B + S * A + T; // transparency

The fifth column of the color matrix indicates the offset of each color. The following describes the common APIs of 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 color matrix A. It is expressed as a first-order array in the form of public void setconcat (colormatrix Mata, colormatrix matb). The color matrix Mata and matb are combined, this is equivalent to Mata matrix processing for images and then matb. 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 ). Public void setrotate
(INT axis, float degrees) set color weight rotation: axis = 0 rotate 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 setRotate(int axis, float degrees) {
        reset();
        float radians = degrees * (float)Math.PI / 180;
        float cosine = FloatMath.cos(radians);
        float sine = FloatMath.sin(radians);
        switch (axis) {
        // Rotation around the red color
        case 0:
            mArray[6] = mArray[12] = cosine;
            mArray[7] = sine;
            mArray[11] = -sine;
            break;
        // Rotation around the green color
        case 1:
            mArray[0] = mArray[12] = cosine;
            mArray[2] = -sine;
            mArray[10] = sine;
            break;
        // Rotation around the blue color
        case 2:
            mArray[0] = mArray[6] = cosine;
            mArray[1] = sine;
            mArray[5] = -sine;
            break;
        default:
            throw new RuntimeException();
        }
    }


Public void setsaturation
(Float SAT) by changing the matrix value, set the image's saturation parameter 0 to correspond to the gray image, and 1 to do not change the public void setscale.
(Float rscale, float gscale, float bscale, float ascale) set the R, G, B, A and other variables of the Matrix to the corresponding multiples. The corresponding API is setscale (,). This function actually corresponds to the value on the diagonal line 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
         // Clear the color filter of the brush
         paint.setColorFilter (null);
         cmatrix = new ColorMatrix ();
         cmatrix.set (carrycolor);
// cmatrix.reset ();
// cmatrix.setSaturation (0F);
// cmatrix.setRotate (0, 100);
// cmatrix.setScale (2, 2, 2, 2);
         // Set the color matrix filter
         paint.setColorFilter (new ColorMatrixColorFilter (cmatrix));
         canvas.drawBitmap (bitmap, 0,0, paint);
     }


Ii. Details about the Matrix class the matrix class is a 3x3 coordinate matrix. This class must use the reset function or set... this matrix can be used to zoom in, zoom out, move, rotate, pivot, and twist a graph. The first line of position matrix A controls the X coordinate, the second line controls the Y coordinate, and the third line controls the zcoordinate.



The matrix is used to convert coordinates x and y as follows:
X' = A * x + B * Y + c
Y' = D * x + E * Y + F from the above formula, we can see that C and F control the offset of X and Y respectively. A and E control X, and y coordinate multiples, so

X to A, y to B,

The matrix is :,

The translation in the X direction is △x, and the translation in the Y direction is △y. Then, the coordinates of P (x, y) are:

X = x0 + △x
Y = y0 + △y

The matrix expression is as follows:

Moving first and then putting it big, the multiplication of the matrix is like a * B.

The image rotation is slightly complicated: The corresponding point after the P0 (x0, y0) point rotates the θ angle around the origin is p (x, y ). By using a vector, we get the following result: R is the rotation radius:

X0 = r cos α
Y0 = r sin α

X = r cos (α + θ) = x0 cos θ-y0 sin θ (expansion of trigonometric function and replacement of variable)
Y = r sin (α + θ) = x0 sin θ + y0 cos θ

So we get the matrix:

ImageThere are two types: horizontal image and vertical image. First, we will introduce how to implement a vertical image. What is a vertical image is not described in detail. Vertical image changes of images can also be represented by matrix changes,

Set P0 (x0, y0) to p (x, y), fheight, fwidth, and P0 (x0, y0) in the original image) the coordinates after the vertical image are changed to (x0, 2 * fheight-y0 );

X = x 0 after the vertical image, X coordinates remain unchanged
Y = fheight-y0 after the vertical image, it is equivalent to first rotating the image around the X axis 180 degrees, and then moving the image down the height of the two images, so first y =-y0; then double the height.
Export the corresponding matrix:


Final floatF [] = {1.0f, 0.0f, 0.0f, 0.0f,-1.0f, 1200000f, 0.0f, 0.0f, 1.0f };

Matrix matrix =NewMatrix ();
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 rotate 180 degrees around the x axis
        
         matrix.setValues (carrypos);
         canvas.drawBitmap (bitmap, matrix, paint);
        
     }

 

The result is as follows:


 

In fact, you can use the following method to implement a vertical image:
Matrix matrix =NewMatrix ();
Matrix. setscale (1.0,-1.0 );
Matrix. posttraslate (0, 2 * fheight );

 

 

Matrix learning-compound changes of images

If the image rotates around a certain point P (A, B), first you need to translate the coordinate system to this point, then rotate, and then translate the rotated image back to the original coordinate origin.

We need three steps:

1.Translation-- Shifts the coordinate system to P (A, B );

2.Rotate-- Rotate the image with the origin as the center;

3.Translation-- Translate the rotated image back to the original coordinate origin;

Compared with the aforementioned geometric changes of images (Basic geometric changes of images ),Pan -- rotate -- panSuch geometric changes that require multiple types of images are called compound changes of images.

For a given image, F1, F2, F3 ..... , FN, their change matrices are T1, T2, T3 ..... , TN, image composite change matrix t can be expressed as: t = TnTn-1... T1.

According to the above principle, the change matrix sequence of θ rotating around a certain point (a, B) is:


Based on the above formula, we will give a simple example: rotate around (100,100) 30 degrees (SIN 30 = 0.5, cos 30 = 0.866)
FloatF [] = {0.866f,-0.5f, 63.4f, 0.5f, 0.866f,-36.6f, 0.0f, 0.0f, 1.0f };

Matrix =NewMatrix ();
Matrix. setvalues (f );
The rotated image is as follows:


Android provides us with a simpler method, as follows:
Matrix matrix = new matrix ();
Matrix. setrotate (30,100,100 );
Actual results after matrix operation:

This is exactly the same as the matrix obtained through the formula.

Here we provide another method to achieve the same effect:
Float a = 1000000f, B = 1000000f;
Matrix = new matrix ();
Matrix. settranslate (A, B );
Matrix. prerotate (30 );
Matrix. pretranslate (-a,-B); // equivalent to matrix = matrix * t (moving the matrix of the corresponding distance) post... (swapping the product factor of pre)

 

Matrix learning-error tangent Transformation

What isMistangent transformation of images(Shear transformation )? We can directly see the effect after the incorrect image switch:


Is to set the second parameter of the X vector to 0.5f, that is, x = x0 + 0.5y0, So X increases with Y and the slope is 0.5 linear change.

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 perform the following operations:


Make a summary of the miscut transformation of the image:


X = x0 + B * y0;

Y = D * x0 + y0;


Here we will introduce you to the following points:


Through the above, we found that the setxxxx () function of matrix calls a reset () during the call, which requires attention during composite transformation.

Matrix learning-symmetric transformation (reflection)

What is symmetric transformation? The specific theory is not detailed. The image is a type of symmetric transformation.


Take the above summary as an example to generate a reflection image that is symmetric with the straight line y =-X. The code snippet is as follows:


The current matrix output is:


The effect of image transformation is as follows:


 

Appendix: trigonometric function formula

Two corners and Formula

Sin (a + B) = sinaco Sb + cosasinb

Sin (a-B) = sinaco Sb-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)

Angle X formula

Tan2a = 2 Tana/[1-(Tana) ^ 2]

Cos2a = (COSA) ^ 2-(SINA) ^ 2 = 2 (COSA) ^ 2-1 = 1-2 (SINA) ^ 2

Sin2a = 2sina * cosa

Halfwidth 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 difference Product

2 sinaco sb = sin (a + B) + sin (a-B)

2 cosasinb = sin (a + B)-sin (a-B ))

2 cosacosb = cos (a + B)-sin (a-B)

-2 sinasinb = 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

Product and difference Formulas

Sin (a) sin (B) =-1/2 * [cos (a + B)-cos (a-B)]

Cos (a) Cos (B) = 1/2 * [cos (a + B) + cos (a-B)]

Sin (a) Cos (B) = 1/2 * [sin (a + B) + sin (a-B)]

Induction Formula

Sin (-a) =-sin ()

Cos (-a) = cos ()

Sin (PI/2-A) = cos ()

Cos (PI/2-A) = sin ()

Sin (PI/2 + a) = cos ()

Cos (PI/2 + a) =-sin ()

Sin (Pi-a) = sin ()

Cos (Pi-a) =-cos ()

Sin (PI + a) =-sin ()

Cos (PI + a) =-cos ()

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-key trigonometric Functions


SEC (A) = 1/cos ()

Hyperbolic Functions

Sinh (A) = (E ^ A-E ^ (-A)/2

Cosh (A) = (E ^ A + e ^ (-A)/2

TGH (A) = sinh (a)/cosh ()



Source: http://shaojun168.blog.51cto.com/4669717/951541


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.