使用OpenCV旋转点周围的点

use*_*793 16 c++ opencv point rotation

有谁知道如何在OpenCV中围绕另一个点旋转一个点?

我正在寻找这样的函数:

Point2f rotatePoint(Point2f p1, Point2f center, float angle)
{
    /* MAGIC */
}
Run Code Online (Sandbox Code Playgroud)

Adr*_*ian 30

这些是围绕另一个点旋转角度α所需的步骤:

  1. 通过枢轴点的负数转换点
  2. 使用标准公式旋转该点以进行2-d(或3-d)旋转
  3. 翻译回来

轮换的标准方程是:

x'= x cos(alpha) - y sin(alpha)

y'= x sin(alpha)+ y cos(alpha)

让我们以Point(2,2)为中心点(15,5)45度的例子.

首先,翻译:

v =(15,5) - (2,2)=(13,3)

现在旋转45°:

v =(13*cos 45° - 3*sin 45°,13*sin 45°+ 3*cos 45°)=(7.07 ..,11.31 ..)

最后,翻译回来:

v = v +(2,2)=(9.07 ..,13.31 ..)

注意:角度必须以弧度指定,因此乘以度数 Pi / 180

  • +1是一个很好的答案,虽然我避免使用45°作为测试用例,因为正弦和余弦是相同的. (5认同)

raz*_*zak 7

要旋转点p1 = (x1, y1)周围p (x0, y0)的角度a:

x2 = ((x1 - x0) * cos(a)) - ((y1 - y0) * sin(a)) + x0;
y2 = ((x1 - x0) * sin(a)) + ((y1 - y0) * cos(a)) + y0;
Run Code Online (Sandbox Code Playgroud)

(x2, y2)点的新位置在哪里p1


Yon*_*son 5

这可能有帮助

cv::Point2f rotate2d(const cv::Point2f& inPoint, const double& angRad)
{
    cv::Point2f 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::Point2f rotatePoint(const cv::Point2f& inPoint, const cv::Point2f& center, const double& angRad)
{
    return rotate2d(inPoint - center, angRad) + center;
}
Run Code Online (Sandbox Code Playgroud)