学习OpenCV3:计算三角形的3个角度


一、问题

    已知三角形的3个点p0p1p2。现希望画出三角形,并计算3个角度的大小。

二、分析

    直线a的直线方程:
\frac{y-y_1}{y_2-y_1} = \frac{x-x_1}{x_2-x_1} \Rightarrow \begin{cases} ax+by+c=0 \\a=-(y_2-y_1) \\ b = x_2-x_1 \\ c = (y_2-y_1)x_1 - (x_2-x_1) y_1\end{cases}

    已知3个点p0p1p2,要构造三角形必须确保3个点不在同一直线上,即p0不在直线a上:

    a * p0.x + b * p0.y + c != 0

   三角形各边边长为:

a = \sqrt{(p2.x-p1.x)^2+(p2.y-p1.y)^2}

b = \sqrt{(p2.x-p0.x)^2+(p2.y-p0.y)^2}

c = \sqrt{(p1.x-p0.x)^2+(p1.y-p0.y)^2}

  由余弦定理得:
a^2=b^2+c^2-2bc \cos \theta \Rightarrow \cos \theta = \frac{b^2+c^2-a^2}{2bc} \Rightarrow \theta = \arccos (\frac{b^2+c^2-a^2}{2bc})

三、实现

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
using namespace cv;

Point2d g_p0(200, 500), g_p1(400, 100), g_p2(600, 500);

// 判断3个点不在同一直线上
bool same_line(Point2d p0, Point2d p1, Point2d p2)
{
    double a = -(p2.y - p1.y);
    double b = p2.x - p1.x;
    double c = (p2.y - p1.y) * p1.x - (p2.x - p1.x) * p1.y;
    if (a * p0.x + b * p0.y + c != 0) // p0不在直线p1p2上
        return true;
    else
        return false;
}

// 计算夹角
double calculate_angle(Point2d p0, Point2d p1, Point2d p2)
{
    double a = sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
    double b = sqrt(pow(p2.x - p0.x, 2) + pow(p2.y - p0.y, 2));
    double c = sqrt(pow(p1.x - p0.x, 2) + pow(p1.y - p0.y, 2));
    double angle = acos((b * b + c * c - a * a) / (2 * b * c)) * 180 / CV_PI;
    return angle;
}

// 画夹角
void draw_angle(Mat img, Point2d p0, Point2d p1, Point2d p2, double radius, const Scalar color, int thickness)
{
    // 计算直线的角度
    double angle1 = atan2(-(p1.y - p0.y), (p1.x - p0.x)) * 180 / CV_PI;
    double angle2 = atan2(-(p2.y - p0.y), (p2.x - p0.x)) * 180 / CV_PI;
    // 计算主轴的角度
    double angle = angle1 <= 0 ? -angle1 : 360 - angle1;
    // 计算圆弧的结束角度
    double end_angle = (angle2 < angle1) ? (angle1 - angle2) : (360 - (angle2 - angle1));
    if (end_angle > 180)
    {
        angle = angle2 <= 0 ? -angle2 : 360 - angle2;
        end_angle = 360 - end_angle;
    }
    // 画圆弧
    ellipse(img, p0, Size(radius, radius), angle, 0, end_angle, color, thickness);
}

// 鼠标回调函数
void mouse_callback(int event, int x, int y, int flags, void *param)
{
    static Point2d p(0, 0), p1(0, 0), p2(0, 0);
    static int n = -1;
    switch (event)
    {
    case cv::EVENT_LBUTTONDOWN: // 鼠标左键点击
    {
        p1 = Point2d(x, y);
        int w = 15, h = 15;
        Rect r0(g_p0.x - w, g_p0.y - h, 2 * w, 2 * h);
        Rect r1(g_p1.x - w, g_p1.y - h, 2 * w, 2 * h);
        Rect r2(g_p2.x - w, g_p2.y - h, 2 * w, 2 * h);
        if (r0.contains(p1)) // 鼠标落在中心点g_p0
        {
            n = 0;
            p = g_p0;
        }
        else if (r1.contains(p1)) // 鼠标落在g_p1
        {
            n = 1;
            p = g_p1;
        }
        else if (r2.contains(p1)) // 鼠标落在g_p2
        {
            n = 2;
            p = g_p2;
        }
        break;
    }
    case cv::EVENT_MOUSEMOVE: // 鼠标移动
        p2 = Point2d(x, y);
        if (n == 0)
        {
            g_p0 = p + p2 - p1;
        }
        else if (n == 1)
        {
            g_p1 = p + p2 - p1;
        }
        else if (n == 2)
        {
            g_p2 = p + p2 - p1;
        }
        break;
    case cv::EVENT_LBUTTONUP: // 鼠标左键释放
        p1 = Point2d(0, 0);
        p2 = Point2d(0, 0);
        n = -1;
        break;
    default:
        break;
    }
}

// 主函数
int main()
{
    string window_name = "image";
    namedWindow(window_name, WINDOW_AUTOSIZE);
    int w = 800, h = 600;
    Mat image_original = Mat(h, w, CV_8UC3, Scalar(255, 255, 255));
    cv::setMouseCallback(window_name, mouse_callback); // 调用鼠标回调函数
    while (true)
    {
        Mat img = image_original.clone(); // 拷贝空白图片,方便重复画图

        line(img, g_p1, g_p2, Scalar(255, 0, 0), 2); // 画蓝线a
        line(img, g_p2, g_p0, Scalar(0, 255, 0), 2); // 画绿线b
        line(img, g_p1, g_p0, Scalar(0, 0, 255), 2); // 画红线c

        if (same_line(g_p0, g_p1, g_p2)) // 3个点不在同一条直线上
        {
            draw_angle(img, g_p0, g_p1, g_p2, 15, Scalar(255, 0, 0), 2); // 画蓝色夹角
            double b = calculate_angle(g_p0, g_p1, g_p2);
            string s2 = "p0: (" + to_string(g_p1.x) + ", " + to_string(g_p1.y) + ") " + to_string(b);
            putText(img, s2, Point2d(10, 25), cv::FONT_HERSHEY_COMPLEX, 0.5, Scalar(255, 0, 0));

            draw_angle(img, g_p1, g_p2, g_p0, 15, Scalar(0, 255, 0), 2); // 画绿色夹角
            double c = calculate_angle(g_p1, g_p2, g_p0);
            string s3 = "p1: (" + to_string(g_p2.x) + ", " + to_string(g_p2.y) + ") " + to_string(c);
            putText(img, s3, Point2d(10, 50), cv::FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0));

            draw_angle(img, g_p2, g_p0, g_p1, 15, Scalar(0, 0, 255), 2); // 画红色夹角
            double a = calculate_angle(g_p2, g_p0, g_p1);
            string s1 = "p2: (" + to_string(g_p0.x) + ", " + to_string(g_p0.y) + ") " + to_string(a);
            putText(img, s1, Point2d(10, 75), cv::FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 0, 255));
        }
        else
        {
            putText(img, "error", Point2d(10, 25), cv::FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 0, 255), 1);
        }

        imshow(window_name, img);
        if (waitKey(3) > 0)
            break;
    }
    return 0;
}

操作方法:
  鼠标左键点击三角形任意一个顶线并按住移动,由此可修改三角形。在键盘输入任意的键,可退出程序。

运行结果:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。