Home >Web Front-end >JS Tutorial >javascript image processing - deep understanding of affine transformation_javascript skills
In the previous article , we explained the image pyramid. In this article we will learn about affine transformation.
Affine?
Any affine transformation can be converted to, multiplied by a matrix (linear change), plus a vector (translational change).
In fact, affine is the transformation relationship between two pictures.
For example, we can perform operations on images through affine transformation: scaling, rotation, translation, etc.
A math problem
Before solving the affine problem, let’s do a math problem.
As shown in the figure, for point (x1, y1), it is rotated by an angle a relative to the origin, so where does this point go?
We change the coordinate system into a polar coordinate system, then the point (x1, y1) becomes (r, β), and after rotation it becomes (r, α β) .
Return to the Cartesian coordinate system, then the rotated point becomes (cos(α β) * r, sin(α β) * r).
Then use the formula :
cos(α β)=cosαcosβ-sinαsinβ
sin(α β)=sinαcosβ cosαsinβ
and the original point is (cosβ * r, sinβ * r), so it is easy to get the new point as (x1 * cosα - y1 * sinα, x1 * sinaα y1 *cosα).
We can derive the rotation transformation formula from it:
Then translation is relatively simple, it is equivalent to adding a vector (c, d).
Get the transformation matrix function implementation
Usually we use matrix to represent affine transformation.
Where A is the rotation scaling transformation, and B is the translation transformation. Then the result T satisfies:
or
That is:
Of course, this implementation itself has certain problems, because the origin is fixed at the upper left corner.
Affine transformation implementation
Then traverse all elements and assign values to the corresponding points.
Effect