我正在尝试按照用户定义的角度旋转矢量中的一组点,并在SO处找到解决方案。 在下面的代码中,输出图像的尺寸和旋转角度(45度)是正确的,但点的位置似乎是偏移的。 有人可以给我一个提示,问题是什么? 编辑:请参见附图,其中生成的线旋转正确,但结果不是从(0,0)(topleft)开始。
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;
}
答案 0 :(得分:0)
在这两行上:
Rect bb = boundingRect(rotatedPoints);
Mat rotatedImg = Mat(bb.height, bb.width, img.type());
您将新图片尺寸设置为bb
的尺寸。但是bb
本身有一个平移偏移,当你继续转换点时你没有考虑到它;等效地,新的视口本身是偏移的。
您可以做的是将原始图片展开以适合bb
,例如:
Mat rotatedImg(std::max(img.width(), bb.x + bb.width), std::max(img.height(), bb.y + bb.height), img.type());