opencv - trying to understand the Affine Transform -
i playing affine transform in opencv , having trouble getting intuitive understanding of workings, , more specifically, how specify parameters of map matrix can specific desired result.
to setup question, procedure using 1st define warp matrix, transform.
in opencv 2 routines (i using example in excellent book opencv bradski & kaehler):
cvgetaffinetransorm(srctri, dsttri, warp_matrix); cvwarpaffine(src, dst, warp_mat);
to define warp matrix, srctri
, dsttri
defined as:
cvpoint2d32f srctri[3], dsttri[3];
srctri[3]
populated follows:
srctri[0].x = 0; srctri[0].y = 0; srctri[1].x = src->width - 1; srctri[1].y = 0; srctri[2].x = 0; srctri[2].y = src->height -1;
this top left point, top right point, , bottom left point of image starting point of matrix. part makes sense me.
but values dsttri[3]
confusing, @ least, when vary single point, not result expect.
for example, if use following dsttri[3]
:
dsttri[0].x = 0; dsttri[0].y = 0; dsttri[1].x = src->width - 1; dsttri[1].y = 0; dsttri[2].x = 0; dsttri[2].y = 100;
it seems difference between src , dst point bottom left point moved right 100 pixels. intuitively, feel bottom part of image should shifted right 100 pixels, not so.
also, if use exact same values dsttri[3]
use srctri[3]
, think transform produce exact same image--but not.
clearly, not understand going on here. so, mapping srctri[]
dsttri[]
represent?
here mathematical explanation of affine transform: matrix of size 3x3 applies foolowing transformations on 2d vector: scale in x axis, scaley, rotation, skew, translation in x axis , y. these 6 transformations , have 6 elements in 3x3 matrix. bottom row [0 0 1]. why? because bottom row represents perspective transformation in axis x , y, , affine transformation not include perspective transform. (if want apply perspective warping use homography: 3x3 matrix )
what relation between 6 values insert affine matrix , 6 transformation does? let @ 3x3 matrix like
e*zx*cos(a), -q1*sin(a) , dx, e*q2*sin(a), z y*cos(a), dy, 0 , 0 , 1
- the dx ,
- dy elements translation in x , y axis (just move picture left-right, down).
- zx relative scale(zoom) apply image in x axis.
- zy same above y axis
- a angle of rotation of image. tricky since when want rotate 'a' have insert sin(), cos() in 4 different places in matrix.
- 'q' skew parameter. used. cause image skew on side (q1 causes y axis affects x axis , q2 causes x axis affect y axis)
- bonus: 'e' parameter not transformation. can have values 1,-1. if 1 nothing happens, if -1 image flipped horizontally. can use flip image vertically but, type of transformation used.
very important note!!!!!
the above explanation mathematical. assumes multiply matrix column vector right. far remember, matlab uses reverse multiplication (row vector left) need transpose matrix. pretty sure opencv uses regular multiplication need check it. enter translation matrix (x shifted 10 pixels, y 1).
1,0,10 0,1,1 0,0,1
if see normal shift ok, if shit appears transpose matrix to:
1,0,0 0,1,0 10,1,1
Comments
Post a Comment