用opencv绘制一个箭头,沿着圆运动并留下运动轨迹(c++)

用opencv绘制一个箭头,沿着圆运动并留下运动轨迹(c++)。基于该例程可以简单实现一个运动小车的模型。

cpp 复制代码
using namespace cv;

int main()
{
    // 创建一个黑色背景的图像,大小为400*400
    Mat image(400, 400, CV_8UC3, Scalar(0, 0, 0));

    //设置箭头的初始位置和方向
    Point2f arrow_center(200, 200);  //箭头中心点
    double arrow_angle = 0.0;        //箭头角度(弧度)

    // 循环处理每帧图像
    while (true) {
        // 旋转箭头
        arrow_angle += 0.1;
        if (arrow_angle >= 2 * CV_PI) {
            arrow_angle -= 2 * CV_PI;
        }

        // 计算箭头的头和尾位置
        Point2f arrow_head(arrow_center.x + 50 * cos(arrow_angle),
            arrow_center.y + 50 * sin(arrow_angle));
        Point2f arrow_tail(arrow_center.x - 50 * cos(arrow_angle),
            arrow_center.y - 50 * sin(arrow_angle));

        // 绘制箭头
        arrowedLine(image, arrow_tail, arrow_head, Scalar(0, 0, 255), 3);

        // 将箭头中心向前移动10个像素
        arrow_center.x += 10 * cos(arrow_angle);
        arrow_center.y += 10 * sin(arrow_angle);

        // 如果箭头越过边界,则将其移回中央
        if (arrow_center.x < 0 || arrow_center.y < 0 ||
            arrow_center.x > image.rows || arrow_center.y > image.cols) {
            arrow_center.x = image.cols / 2;
            arrow_center.y = image.rows / 2;
        }

        // 如果应该闪烁,将箭头颜色改为绿色,否则为红色


        // 显示图像
        imshow("Arrow", image);

        // 等待一会儿
        waitKey(100);
        //if ((int)(arrow_angle / CV_PI * 5) % 2 == 0) {
            arrowedLine(image, arrow_tail, arrow_head, Scalar(0, 255, 0), 3);
        //}
        imshow("Arrow", image);
    }

    return 0;
}
相关推荐
mjhcsp几秒前
C++ long long 类型深度解析:大整数处理的基石
开发语言·c++·策略模式·long long
ezl1fe1 分钟前
第一篇:把任意 HTTP API 一键变成 Agent 工具
人工智能·后端·算法
Larry_Yanan4 分钟前
QML学习笔记(四十五)QML与C++交互:信号槽的双向实现
c++·笔记·qt·学习·ui·交互
冯诺依曼的锦鲤5 分钟前
算法练习:双指针专题
c++·算法
算家计算6 分钟前
OpenAI推出首款浏览器,能否撼动全球超30亿用户的Chrome?
人工智能·openai·资讯
文火冰糖的硅基工坊7 分钟前
[人工智能-大模型-33]:模型层技术 - 大模型的神经网络架构
人工智能·神经网络·架构
WaWaJie_Ngen7 分钟前
【设计模式】工厂模式(Factory)
c++·设计模式·简单工厂模式·工厂方法模式·抽象工厂模式
文火冰糖的硅基工坊25 分钟前
[人工智能-大模型-33]:模型层技术概览 - 大模型内部组成与层次调用关系
人工智能
蜉蝣之翼❉26 分钟前
检测十字标 opencv python
python·opencv·计算机视觉
埃伊蟹黄面31 分钟前
深入理解STL关联容器:map/multimap与set/multiset全解析
开发语言·c++