Maik Fruhner
Maik Fruhner

Reputation: 310

Rotate 2D points using openCV and python

I am using cv2.getRotationMatrix2D to get a 2x3 matrix to rotate a source image. This works fine. Now i have several 2D coordinates, which represent points in the unrotated image. Can I use the same matrix to rotate the indiviual points and if yes, how? If i understood correcty, I should be able to multiply the matrix with the (x,y)-coordinates, but no matter which combination of matrix and points I try, I never get simple x and y coordinates as a result.

Thanks for your help!

Edit:

the rotation matrix is

[[  9.24630834e-01   3.80864571e-01   1.52747702e-01]
 [ -3.80864571e-01   9.24630834e-01   1.57068951e+02]]

What I am doing so far:

point = np.array([marker.x, marker.y, 1])
transform = rot_matrix * point.T
print transform

But this does not give me a 2x1 matrix as it should, but:

[[  2.10815830e+02   7.92198307e+01   1.52747702e-01]
 [ -8.68371221e+01   1.92323213e+02   1.57068951e+02]]

Upvotes: 3

Views: 7481

Answers (2)

Eamonn Kenny
Eamonn Kenny

Reputation: 2042

Would this work for you?

Define two arrays xArray and yArray with the points you want to transform

for indexItem in xrange(len(xArray)):
  point = np.array([xArray[indexItem], yArray[indexItem], 1])
  transform = rot_matrix * point.T
  print transform

Its not quite as eloquent but should do the trick. If you really want to create an array of markers then you must use:

 markers = zip( xArray, yArray)

Upvotes: 0

zimmerrol
zimmerrol

Reputation: 4951

Please check this site. As cv2.getRotationMatrix2D creates a affine transformation, you can write the result of the call like this:

.

If you now want to transform a 2D point you have to calculate this expression


This should solve your issue. (All images come from the referenced documentation page)

Edit: Regarding your edit: You cannot use the * operator, but should maybe use np.dot(). The * operator is only performing a real matrix-vector product for matrix object. Otherwise, it is performing a element-wise product.

Upvotes: 4

Related Questions