Skewing an image using Perspective Transforms

前端 未结 3 2059
轻奢々
轻奢々 2020-12-04 20:50

I\'m trying to perform a skew on an image, like one shown here


(source: microsoft.com)
.

I have an array of pixels representing my imag

3条回答
  •  孤城傲影
    2020-12-04 20:55

    As commented by KennyTM you just need an affine transform that is a linear mapping obtained by multiplying every pixel by a matrix M and adding the result to a translation vector V. It's simple math

    end_pixel_position = M*start_pixel_position + V
    

    where M is a composition of simple transformations like rotations or scalings and V is a vector that translates every point of your images by adding fixed coefficients to every pixel.

    For example if you want to rotate the image you can have a rotation matrix defined as:

        | cos(a) -sin(a) |
    M = |                |
        | sin(a)  cos(a) |
    

    where a is the angle you want to rotate your image by.

    While scaling uses a matrix of the form:

        | s1   0 |
    M = |        |
        | 0   s2 |
    

    where s1 and s2 are scaling factors on both axis.

    For translation you just have the vector V:

        | t1 |
    V = |    |
        | t2 |
    

    that adds t1 and t2 to pixel coordinates.

    You then combine the matrixes in one single transformation, for example if you have either scaling, rotation and translation you'll end up having something like:

    | x2 |             | x1 |
    |    | = M1 * M2 * |    | + T
    | y2 |             | y1 |
    

    where:

    • x1 and y1 are pixel coordinates before applying the transform,
    • x2 and y2 are pixels after the transform,
    • M1 and M2 are matrixes used for scaling and rotation (REMEMBER: the composition of matrixes is not commutative! Usually M1 * M2 * Vect != M2 * M1 * Vect),
    • T is a translation vector use to translate every pixel.

提交回复
热议问题