How can I apply a transformation matrix to a point

2020-07-03 06:21发布

问题:

Suppose I have a transformation matrix Mat tr that I got from getAffineTransform() and a Point2d p. I want the point that is the result of warping p with tr. Does OpenCV provide a way of doing this?

回答1:

cv::transform is used for transforming points with a transformation matrix.

Every element of the N -channel array src is interpreted as N -element vector that is transformed using the M x N or M x (N+1) matrix m to M-element vector - the corresponding element of the output array dst .

The function may be used for geometrical transformation of N -dimensional points, arbitrary linear color space transformation (such as various kinds of RGB to YUV transforms), shuffling the image channels, and so forth.

There's a concise example in the InputArray documentation (otherwise not relevant):

std::vector<Point2f> vec;
// points or a circle
for( int i = 0; i < 30; i++ )
    vec.push_back(Point2f((float)(100 + 30*cos(i*CV_PI*2/5)),
                          (float)(100 - 30*sin(i*CV_PI*2/5))));
cv::transform(vec, vec, cv::Matx23f(0.707, -0.707, 10, 0.707, 0.707, 20));

Or you can likely just convert the Point2f into a Mat and multiply by the matrix.



标签: c++ opencv