Principles, code verification and Application of matrix for image transformation in Android (1)

Source: Internet
Author: User

Part 1 mathematical principles of Matrix

In Android, if you have used matrix for image processing, you must know the matrix class. The matrix in Android is a 3x3 matrix with the following content:

 

Matrix processing of images can be divided into four types of basic transformations:

Translate Translation

Rotate rotation and Transformation

Scale

Skew mistangent Transformation

 

Literally, mscale in the matrix is used to process scaling transformations, mskew is used to process mistangent transformations, mtrans is used to process Translation Transformations, and mpersp is used to process perspective transformations. In reality, we certainly cannot fully understand the matrix in words. At the same time, in the android documentation, we have not seen any instructions on using matrix for perspective transformation. Therefore, this article does not discuss this issue.

 

For each type of transformation, Android provides three operations: PRE, set, and post. Where

Set is used to set values in matrix.

PRE is the first multiplication, because the multiplication of the matrix does not meet the exchange law, so the first multiplication and the second multiplication must be strictly differentiated. The first multiplication is equivalent to the right multiplication in the matrix operation.

Post is a post multiplication. Because the multiplication of matrices does not satisfy the exchange law, the first multiplication and the second multiplication must be strictly distinguished. The posterior multiplication is equivalent to the left multiplication in the matrix operation.

 

Except for translation, rotation, scale, and skew can all be performed around a center point. If this parameter is not specified, by default, the changes are performed around (0, 0.

 

Let's take a look at the specific situations of the four transformations. Since all the images are a little composed, we only need to examine a point-related transformation.

 

I. Translation Transformation

Assume that the coordinate of a point is to move it toXAxis andYThe sizes of moving axes are as follows:


As shown in:


It is easy to know:


If a matrix is used for representation, it can be written as follows:

 


Ii. Rotation Transformation

 

2.1 rotate around the coordinate origin:

Assume that there is a point that rotates clockwise relative to the coordinate origin.PThe distance from the point to the coordinate origin isRSuch:


So,


If a matrix is used, it can be expressed:


 

2.2 rotate around a certain point

If the rotation is clockwise around a certain point, it can be expressed:


Possible values:


Apparently,

1.

After moving the coordinate origin to the point,
.

2.


Is to convert the previous step, rotate clockwise around the new coordinate origin.

3.


After the previous rotation and transformation, the coordinate origin is moved back to the original coordinate origin.

 

Therefore, the Rotation Transformation around a certain point can be divided into three steps, that is, first moving the coordinate origin to the point, and then rotating the transformation around the new coordinate origin, then, the coordinates are moved back to the original coordinates.

 

Iii. scaling and Transformation

Theoretically, there is no scaling or transformation for a vertex, but considering that all images are composed of verticesXAxis andYAxis Direction AmplificationK1AndK2Times, then all vertices in the imageXCoordinates andYThe coordinates are amplified separately.K1AndK2Times, that is


In a matrix:


It's easy to understand scaling and transformation.

 

Iv. mistangent Transformation

The skew is also known as shear mapping (shear transformation) or transvection (contraction). It is a special linear transformation. The effect of mistangent transformation is to make all verticesXCoordinates (orYCoordinates) remain unchanged, and the correspondingYCoordinates (orXCoordinates.XThe vertical distance between the axis (or Y axis) is proportional. The error tangent transformation belongs to the same area transformation. That is, the area of a shape before and after the error tangent transformation is equal.

For exampleYThe coordinates remain unchanged,XThe coordinates are translated proportionally. In this case, it will be horizontally staggered.


OfXThe coordinates remain unchanged,YThe coordinates are translated proportionally. In this case, vertical failovers are called.

 

If a point is obtained after an error tangent transformation, the following relationship should exist for the horizontal error tangent:


In a matrix:


The matrix extending to 3x3 is in the following form:

 

Likewise, for vertical miscut, you can:


In mathematics, the strictly staggered transformation is like this. In addition to the above mentioned situations, in Android, horizontal and vertical miscutting can also be performed at the same time. The form is:


 

V. symmetric transformation

In addition to the basic transformations mentioned above, we can also use matrix to perform symmetric transformations. The so-called symmetric transformation means that the changed image and the source image are symmetric about a certain symmetric axis. For example, a point is obtained after symmetric transformation,

If the axis of symmetry is the X axis, is it difficult,


In a matrix:


If the axis of symmetry is Y axis,


In a matrix:


If the axis of symmetry isY = x,


So,


It is easy to understand:


In a matrix:


Similarly, if the axis of symmetry isY =-xIn a matrix:

 

In particular, if the axis of symmetry isY = kxSuch:


So,


It is easy to solve:


In a matrix:


WhenK = 0, That isY = 0, That is, the axis of symmetry isXAxis; whenKWhen it tends to be infinite, that isX = 0, That is, the axis of symmetry isYAxis; whenK =1, that isY = x, That is, the axis of symmetry isY = xWhenK =-1, that isY =-x, That is, the axis of symmetry isY =-x. It is not difficult to verify that this is consistent with the specific situation in section 4 above.

 

If the axis of symmetry isY = kx + BIn this case, you only need to add two Translation Transformations on the basis of the above, that is, first move the coordinate origin (0, B).Y = kxAnd then move the coordinate origin back to the original coordinate origin. The matrix representation is roughly like this:


Note: In actual programming, we knowYIn the forward and mathematical aspects of coordinatesYThe positive of coordinates is just the opposite, so in MathematicsY = xAnd on-screenY =-xIs the real same thing, and vice versa. That is to say, if you want to make the image look like a math on the screenY = xSymmetric:


To make the image look like on the screen, in a mathematical senseY =-xSymmetric:
 

About the axis of symmetryY = kxOrY = kx + BYou also need to consider this issue.

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.