OpenCV:将点旋转一个角度:Offset/shift in solution
OpenCV: Rotate points by an angle: Offset/shift in solution
我正在尝试按用户定义的角度旋转矢量中的一组点,并在 SO 处找到了解决方案。
在以下代码中,输出图像的尺寸和旋转角度(45 度)是正确的,但点的位置似乎发生了偏移。
有人可以给我提示,问题是什么?
编辑:查看生成的线正确旋转的附加图片,但结果不是从 (0,0)(左上角)开始。
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;
}
这两行:
Rect bb = boundingRect(rotatedPoints);
Mat rotatedImg = Mat(bb.height, bb.width, img.type());
您将新图像尺寸设置为 bb
的大小。但是 bb
本身有一个平移偏移量,您在继续转换点时没有考虑到它;所以等效地,新的 viewport 本身是偏移的。
您可以做的是扩展原始图像以适应 bb
,即像这样:
Mat rotatedImg(std::max(img.width(), bb.x + bb.width), std::max(img.height(), bb.y + bb.height), img.type());
我正在尝试按用户定义的角度旋转矢量中的一组点,并在 SO 处找到了解决方案。 在以下代码中,输出图像的尺寸和旋转角度(45 度)是正确的,但点的位置似乎发生了偏移。 有人可以给我提示,问题是什么? 编辑:查看生成的线正确旋转的附加图片,但结果不是从 (0,0)(左上角)开始。
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;
}
这两行:
Rect bb = boundingRect(rotatedPoints);
Mat rotatedImg = Mat(bb.height, bb.width, img.type());
您将新图像尺寸设置为 bb
的大小。但是 bb
本身有一个平移偏移量,您在继续转换点时没有考虑到它;所以等效地,新的 viewport 本身是偏移的。
您可以做的是扩展原始图像以适应 bb
,即像这样:
Mat rotatedImg(std::max(img.width(), bb.x + bb.width), std::max(img.height(), bb.y + bb.height), img.type());