OpenCV:按一定角度旋转点:溶液中的偏移/偏移
OpenCV: Rotate points by an angle: Offset/shift in solution
>我正在尝试按用户定义的角度旋转矢量中的一组点,并在 SO 找到了解决方案。 在下面的代码中,输出图像的尺寸和旋转角度(45 度(是正确的,但点的位置似乎发生了偏移。 有人可以给我小费,问题是什么? 编辑:请参阅附加的图片,其中生成的线正确旋转,但结果不是从 (0,0((左上角(开始。
生成行 旋转线
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
本身有一个平移偏移,您在继续变换点时没有考虑到这一点;因此,等效地,新视口本身是偏移的。
相反,您可以做的是扩展原始图像以适应bb
,即如下所示:
Mat rotatedImg(std::max(img.width(), bb.x + bb.width), std::max(img.height(), bb.y + bb.height), img.type());
相关文章:
- 与互斥锁相比,旋转锁可以保证上下文切换
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- C / C++ 移位/偏移/向左或向右移动位图?
- 绘制旋转的三角形
- 旋转模型矩阵时的形状失真
- 四边形的 2D 旋转
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- 发布旋转矩阵(openGL/glm)
- 顺时针迭代旋转 3 位数字
- 通过指针偏移量访问结构变量值
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 如何有效地计算将单位立方体映射到自身的反射和旋转?
- 子轴围绕父轴而不是他自己的轴旋转
- 将使用太多的纹理插值器 - 带旋转的着色器
- 以线程安全的方式转换 C/C++ 中时区名称字符串的时区偏移量
- SFML 向下移动时如何围绕屏幕中心旋转?
- 易于访问带有偏移索引的C++矢量
- OpenCV:按一定角度旋转点:溶液中的偏移/偏移
- 精灵旋转偏移不会停留在它所属的位置。(SDL)