Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate a point around a point with OpenCV

Does anyone know how I can rotate a point around another in OpenCV?

I am looking for a function like this:

Point2f rotatePoint(Point2f p1, Point2f center, float angle)
{
    /* MAGIC */
}
like image 412
user1021793 Avatar asked Oct 31 '11 11:10

user1021793


2 Answers

These are the steps needed to rotate a point around another point by an angle alpha:

  1. Translate the point by the negative of the pivot point
  2. Rotate the point using the standard equation for 2-d (or 3-d) rotation
  3. Translate back

The standard equation for rotation is:

x' = xcos(alpha) - ysin(alpha)

y' = xsin(alpha) + ycos(alpha)

Let's take the example of Point(15,5) around Point(2,2) by 45 degrees.

Firstly, translate:

v = (15,5) - (2,2) = (13,3)

Now rotate by 45°:

v = (13*cos 45° - 3*sin 45°, 13*sin 45° + 3*cos 45°) = (7.07.., 11.31..)

And finally, translate back:

v = v + (2,2) = (9.07.., 13.31..)

Note: Angles must be specified in radians, so multiply the number of degrees by Pi / 180

like image 67
Adrian Avatar answered Sep 30 '22 13:09

Adrian


To rotate point p1 = (x1, y1) around p (x0, y0) by angle a:

x2 = ((x1 - x0) * cos(a)) - ((y1 - y0) * sin(a)) + x0;
y2 = ((x1 - x0) * sin(a)) + ((y1 - y0) * cos(a)) + y0;

where (x2, y2) is the new location of point p1

like image 28
razz Avatar answered Sep 30 '22 13:09

razz