Mathematically, you can do this by computing the barycentric coordinates of each point in the original triangle, and converting back to position using the new coordinates:
given initial triangle vertices A, B, and C, and point p,
find barycentric coordinates (a,b,c) such that a+b+c=1 and p = a*A + b*B + c*C:
-> solve [A.x B.x C.x] [a] [p.x]
[A.y B.y C.y] * [b] = [p.y]
[ 1 1 1 ] [c] [ 1 ]
then, given new vertices D, E, and F,
resulting point q = a*D + b*E + c*F:
-> compute [q.x] = [D.x E.x F.x] * [a]
[q.y] [D.y E.y F.y] [b]
[c]
So, in OpenCV:
float p_data[3] =
{ p.x,
p.y,
1.0
};
Mat_<float> p(3, 1, p_data);
float m_data[9] =
{ A.x, B.x, C.x,
A.y, B.y, C.y,
1.0, 1.0, 1.0
};
Mat_<float> M(3, 3, m_data);
Mat_<float> bary(3,1);
cv::solve(M, p, bary, DECOMP_LU);
float n_data[6] =
{ D.x, E.x, F.x,
D.y, E.y, F.y
};
Mat_<float> N(2, 3, n_data);
Mat_<float> result(2,1) = N * bary;
To map point_count
points at the same time, set the number of columns of p
, bary
, and result
to point_count
instead of 1
(while increasing the size of p_data
accordingly, etc.)
Depending on the application, it may be more convenient/efficient to explicitly compute the affine matrix first, and apply it directiy:
Mat_<float> Affine = N * M.inv();
Mat_<float> result = Affine * p;