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?
It is a translation matrix which shifts the image by the vector (x, y). The first row of the matrix is [1, 0, x], the second is [0, 1, y] M = np. float32([[1, 0, x], [0, 1, y]]) shifted = cv. warpAffine(img, M, size)
Transformation Matrix is a matrix that transforms one vector into another vector by the process of matrix multiplication. The transformation matrix alters the cartesian system and maps the coordinates of the vector to the new coordinates.
What is an Affine Transformation? A transformation that can be expressed in the form of a matrix multiplication (linear transformation) followed by a vector addition (translation). From the above, we can use an Affine Transformation to express: Rotations (linear transformation)
OpenCV provides a function cv2. warpAffine() that applies an affine transformation to an image. You just need to provide the transformation matrix (M). The basic syntax for the function is given below.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With