Search code examples
c++opencvrotationpoints

OpenCV: Rotate points by an angle: Offset/shift in solution


i am trying to rotate a set of points in a vector by an user-defined angle and found a solution at SO. In the following code the dimension of the output image and the rotation angle (45 degree) is correct but the position of the points seem to be shifted. Can someone give me a tip, what the problem is? edit: See attached pictures where the generated line is rotated correct, but the result does not begin at (0,0) (topleft).

generated Line rotated Line

cv::Point rotate2d(const cv::Point& inPoint, const double& angRad)
{
    cv::Point 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::Point rotatePoint(const cv::Point& inPoint, const cv::Point& center, const double& angRad)
{
    return rotate2d(inPoint - center, angRad) + center;
}


int main( int, char** argv )
{
    // Create an dark Image with a gray line in the middle
    Mat img = Mat(83, 500, CV_8U);
    img = Scalar(0);
    vector<Point> pointsModel;

    for ( int i = 0; i<500; i++)
    {
        pointsModel.push_back(Point(i , 41));
    }

    for ( int i=0; i<pointsModel.size(); i++)
    {
        circle(img, pointsModel[i], 1, Scalar(120,120,120), 1, LINE_8, 0);
    }
    imshow("Points", img);

    // Rotate Points
    vector<Point> rotatedPoints;
    Point tmpPoint;
    cv::Point pt( img.cols/2.0, img.rows/2.0 );
    for ( int i=0; i<pointsModel.size(); i++)
    {
        tmpPoint = rotatePoint(pointsModel[i] , pt , 0.7854);
        rotatedPoints.push_back(tmpPoint);
    }
    Rect bb = boundingRect(rotatedPoints);
    cout << bb;
    Mat rotatedImg = Mat(bb.height, bb.width, img.type());
    rotatedImg = Scalar(0);

    for (int i=0; i<rotatedPoints.size(); i++ )
    {
        circle(rotatedImg, rotatedPoints[i], 1, Scalar(120,120,120), 1, LINE_8, 0);
    }
    imshow("Points Rotated", rotatedImg);
    waitKey();

    return 0;
}

Solution

  • On these two lines:

    Rect bb = boundingRect(rotatedPoints);
    Mat rotatedImg = Mat(bb.height, bb.width, img.type());
    

    You set the new image dimensions to the size of bb. But bb itself has a translational offset that you didn't take into account when you proceeded to transform the points; so equivalently, the new viewport itself is offset.

    What you can do instead is to expand the original image to fit bb, i.e. something like this:

    Mat rotatedImg(std::max(img.width(), bb.x + bb.width), std::max(img.height(), bb.y + bb.height), img.type());