trying to understand the Affine Transform

后端 未结 1 1333
广开言路
广开言路 2020-12-05 08:46

I am playing with the affine transform in OpenCV and I am having trouble getting an intuitive understanding of it workings, and more specifically, just how do I specify the

相关标签:
1条回答
  • 2020-12-05 09:32

    Here is a mathematical explanation of affine transform: this is a matrix of size 3x3 that applies the foolowing transformations on 2D vector: Scale in X axis, scaleY, rotation, skew, translation in x axis and y. These are 6 transformations and thus you have six elements in your 3x3 matrix. The bottom row is always [0 0 1]. Why? because the bottom row represents the a perspective transformation in axis x and y, and affine transformation does not include perspective transform. (If you want to apply perspective warping use homography: also 3x3 matrix )

    What is the relation between 6 values you insert into affine matrix and the 6 transformation it does? Let us look at this 3x3 matrix like

    e*Zx*cos(a), -q1*sin(a)  ,  dx,
    e*q2*sin(a),     Z y*cos(a),  dy,
    0       ,            0  ,   1
    
    1. The dx and
    2. dy elements are translation in x and y axis (just move the picture left-right, up down).
    3. Zx is the relative scale(zoom) you apply to the image in X axis.
    4. Zy is the same as above for y axis
    5. a is the angle of rotation of the image. This is tricky since when you want to rotate by 'a' you have to insert sin(), cos() in 4 different places in the matrix.
    6. 'q' is the skew parameter. It is rarely used. It will cause your image to skew on the side (q1 causes y axis affects x axis and q2 causes x axis affect y axis)
    7. Bonus: 'e' parameter is actually not a transformation. It can have values 1,-1. If it is 1 than nothing happens, but if it is -1 than the image is flipped horizontally. You can use it also to flip the image vertically but, this type of transformation is rarely used.

    Very important Note!!!!!

    The above explanation is mathematical. It assumes you multiply the matrix by column vector from the right. As far as I remember, Matlab uses reverse multiplication (row vector from the left) so you will need to transpose this matrix. I am pretty sure that openCV uses regular multiplication but you need to check it. Just enter only translation matrix (x shifted by 10 pixels, y by 1).

    1,0,10
    0,1,1
    0,0,1
    

    If you see a normal shift than everything is OK, but If shit appears than transpose the matrix to:

    1,0,0
    0,1,0
    10,1,1
    
    0 讨论(0)
提交回复
热议问题