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

edit retag close merge delete

Why don't you use getRotationMatrix2D and pDst = (rot2d*Mat(p[i])).t()

( 2017-07-11 09:23:57 -0500 )edit

Thank you LBerger, I thought about asking the question directly at stackoverflow, because the solution was from there but you were faster with your comment than I was with deleting the question. If the duplication is troublesome, I can remove it here.

To your comment: With Mat(p[i]) you mean the input point, don't you?

( 2017-07-11 09:33:40 -0500 )edit

Sort by ยป oldest newest most voted

Something like this :

    Mat rot2d = getRotationMatrix2D(Point(256,256), 45, 1);
vector<Point3d> p = { Point3d(0,0,1),Point3d(0,512,1),Point3d(512,512,1),Point3d(512,0,1) };
Mat pDst;
for (int i = 0; i < 4; i++)
{
pDst = (rot2d*Mat(p[i])).t();
cout<< p[i] <<" ---> "<<pDst<<"\n";

}


results

[0, 0, 1] ---> [-106.0386719675124, 256]
[0, 512, 1] ---> [256, 618.0386719675123]
[512, 512, 1] ---> [618.0386719675123, 256]
[512, 0, 1] ---> [256, -106.0386719675124]

more

1

Seems to be a good solution for the rotation as well. My problem was asked a bit unclear: Instead of fitting a Mat to the corresponding points, my output-Points should begin at the point (0,0) and be positiv. I managed to do this by subtract the bb.x and bb.y values from every single point. The Question is answered - thank you again @LBerger and sorry for the duplicate with SO.

( 2017-07-11 11:01:22 -0500 )edit