Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply a transformation matrix to a point in OpenCV?

Tags:

c++

opencv

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?

like image 500
leinaD_natipaC Avatar asked Oct 29 '14 22:10

leinaD_natipaC


People also ask

What is transformation matrix in OpenCV?

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)

How does a transformation matrix work?

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 affine transformation OpenCV?

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)

What does cv2 warpAffine do?

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.


1 Answers

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.

like image 55
chappjc Avatar answered Sep 22 '22 14:09

chappjc