user901898
user901898

Reputation: 335

Rotate, Scale and Translate around image centre in OpenCV

I really hope this isn't a waste of anyone's time but I've run into a small problem. I am able to construct the transformation matrix using the following:

M = 
s*cos(theta)        -s*sin(theta)       t_x
s*sin(theta)        s*cos(theta)        t_y
0                   0                   1

This works if I give the correct values for theta, s (scale) and tx/ty and then use this matrix as one of the arguments for cv::warpPerspective. The problem lies in that this matrix rotates about the (0,0) pixel whereas I would like it to rotate about the centre pixel (cols/2, rows/2). How can incoporate the centre point rotation into this matrix?

Upvotes: 1

Views: 3531

Answers (1)

dvhamme
dvhamme

Reputation: 1450

Two possibilities. The first is to use the function getRotationMatrix2D which takes the center of rotation as an argument, and gives you a 2x3 matrix. Add the third row and you're done.

A second possibility is to construct an additional matrix that translates the picture before and after the rotation:

T = 
1 0 -cols/2
0 1 -rows/2
0 0 1

Multiply your rotation matrix M with this one to get the total transform -TMT (e.g. with function gemm) and apply this one with warpPerspective.

Upvotes: 2

Related Questions