Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Rotate points by an angle

Hello, i am trying to rotate a set of points in a vector<points> by an user-defined angle and found a solution at SO. In the following code the dimension of the output image (rotated by 45 degree) is correct but the position of the points seem to be shifted. Can someone give me a tip, what the problem is?

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;
}

Rotate points by an angle

Hello, i am trying to rotate a set of points in a vector<points> by an user-defined angle and found a solution at SO. In the following code the dimension of the output image (rotated by 45 degree) is correct but the position of the points seem to be shifted. Can someone give me a tip, what the problem is?

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;
}