C++ 计算点之间的角度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15888180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:51:04  来源:igfitidea点击:

Calculating the angle between Points

c++opencv

提问by Tomazi

Working with C++ and opencv

使用 C++ 和 opencv

I am trying to calculate an angle between two points.....I have a 2D plane with a changing center point of a bounding box, Now if the center point in frame 1 has changed its location in frame 2 i need to find the angle of these two points.

我正在尝试计算两点之间的角度.....我有一个 2D 平面,边界框的中心点不断变化,现在如果第 1 帧中的中心点已更改其在第 2 帧中的位置,我需要找到这两点的角度。

Here is the example of what i am trying to do:

这是我正在尝试做的示例:

enter image description here

在此处输入图片说明

Can someone suggest a way of working this out.......? some kind of mathematical solution or perhaps a C++ function.

有人可以建议一种方法来解决这个问题......?某种数学解决方案或 C++ 函数。

回答by masoud

Use Dot product:

使用点积

v1.v2 = v1.x * v2.x + v1.y * v2.y

v1.v2 = |v1| * |v2| * cos(theta)
---------------------------------+
                                 |
                                 +-->  theta = acos(v1.v2 / |v1|*|v2|)

A sample code is:

示例代码是:

float angleBetween(const Point &v1, const Point &v2)
{
    float len1 = sqrt(v1.x * v1.x + v1.y * v1.y);
    float len2 = sqrt(v2.x * v2.x + v2.y * v2.y);

    float dot = v1.x * v2.x + v1.y * v2.y;

    float a = dot / (len1 * len2);

    if (a >= 1.0)
        return 0.0;
    else if (a <= -1.0)
        return PI;
    else
        return acos(a); // 0..PI
}

It calculates angle between v1 and v2 as below image

它计算 v1 和 v2 之间的角度,如下图

enter image description here

在此处输入图片说明

回答by oscfri

Assuming you want to calculate the angle between the two points relative to the origin of the 2D plane, you can use the dot product to calculate the angle between the points (like how the other answerof this question described).

假设您要计算相对于 2D 平面原点的两点之间的角度,您可以使用点积来计算点之间的角度(就像这个问题的另一个答案所描述的那样)。

OpenCV has implementations of calculating the dot product and the length of points. The dot product of two points is:

OpenCV 有计算点积和点长度的实现。两点的点积为:

v1.dot(v2) // v1.x * v2.x + v1.y * v2.y

Length of vector (commonly referred as the L2 Norm of vector) using cv::normis:

向量的长度(通常称为向量的 L2 范数)使用cv::norm为:

cv::norm(v1) // sqrt(v1.x * v1.x + v1.y * v1.y)

Using OpenCV's implementations of dot product and the length of vector we have the following sample code

使用 OpenCV 的点积和向量长度的实现,我们有以下示例代码

double angle(const Point& v1, const Point& v2)
{
    double cosAngle = v1.dot(v2) / (cv::norm(v1) * cv::norm(v2));
    if (cosAngle > 1.0)
        return 0.0;
    else if (cosAngle < -1.0)
        return CV_PI;
    return std::acos(cosAngle);
}

This solution isn't only limited to 2D points. It can be used for calculating the angle between 3D points as well.

此解决方案不仅限于 2D 点。它也可用于计算 3D 点之间的角度。