具体原理可以参考另外一篇博客:点击打开链接,实现思想就是,通过计算两个向量的斜率角,然后相减,就得到了夹角,好了,直接上代码!
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
// 以pt1为基准
float getAngelOfTwoVector(Point2f &pt1, Point2f &pt2, Point2f &c)
{
float theta = atan2(pt1.y - c.y, pt1.x - c.x) - atan2(pt2.y - c.y, pt2.x - c.x);
if (theta > CV_PI)
theta -= 2 * CV_PI;
if (theta < -CV_PI)
theta += 2 * CV_PI;
theta = theta * 180.0 / CV_PI;
return theta;
}
void main()
{
Point2f c(0, 0);
Point2f pt1(0, -1);
Point2f pt2(-1, 0);
float theta = getAngelOfTwoVector(pt1, pt2, c);
cout << "theta: " << theta << endl;
}
打完收工!