简体   繁体   中英

Why I can not rotate points around a point by 90 degree in opencv?

I want to rotate a set of points by 90° clockwise, like this:

在此处输入图片说明

But I use the code as I show here, and set the angle as 90°, center as (100,100), and I get a result like this:

在此处输入图片说明

I check the formula, is the same as my code:

cv::Point2f rotate2d(const cv::Point2f& inPoint, const double& angRad)
{
    cv::Point2f outPoint;
    //CW rotation
    outPoint.x = std::cos(angRad)*inPoint.x - std::sin(angRad)*inPoint.y;
    outPoint.y = std::sin(angRad)*inPoint.x + std::cos(angRad)*inPoint.y;
    return outPoint;
}

cv::Point2f rotatePoint(const cv::Point2f& inPoint,
                        const cv::Point2f& center,
                        const double& angRad)
{
    return rotate2d(inPoint - center, angRad) + center;
}

The problem is probably hiding in the retuning value of your second function:

cv::Point2f rotatePoint(const cv::Point2f& inPoint, const cv::Point2f& center, const double& angRad){
return rotate2d(inPoint - center, angRad) + center;
               ------------^      --------------^
}

You firstly apply it and then translate the center back an forth . If you want to rotate around a pivot point: you subtract it (perform rotation around the origin) and then add it after the transformation is applied. You can do this directly in your first function, by modifying the transformations to:

int x = cos(angRad) * (inPoint.x - axisOfRotation.x)
      - sin(angRad) * (inPoint.y - axisOfRotation.y) + axisOfRotation.x;
                                                       -------^
int y = sin(angRad) * (inPoint.x - axisOfRotation.x) 
      + cos(angRad) * (inPoint.y - axisOfRotation.y) + axisOfRotation.y;
                        // added to both coordinates   -------^

Where: inPoint is the point to be rotated, axisOfRotation the pivot point and angRad = angInDegrees * PI / 180. the angle of clockwise rotation.

Check this for further insight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM