用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;
}
相关推荐
谢铭轩1 天前
题解:P8035 [COCI 2015/2016 #7] Otpor
c++·算法
阿猿收手吧!1 天前
【C++】模块:告别头文件新时代
开发语言·c++
陈广亮1 天前
OpenClaw 多 Agent 配置实战:踩坑指南与最佳实践
人工智能
GHL2842710901 天前
TensorFlow学习
人工智能·学习
阿杰学AI1 天前
AI核心知识100——大语言模型之 LM Arena(简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·模型评测·lm arena
星火开发设计1 天前
虚析构函数:解决子类对象的内存泄漏
java·开发语言·前端·c++·学习·算法·知识
小刘的大模型笔记1 天前
大模型微调实战——从数据准备到落地部署全流程
人工智能
技术狂人1681 天前
告别“复读机“AI:用Agent Skills打造你的专属编程副驾
人工智能·职场和发展·agent·skills
闻缺陷则喜何志丹1 天前
【拆位法】P9277 [AGM 2023 资格赛] 反转|普及+
c++·算法·位运算·拆位法
maplewen.1 天前
C++ 多态原理深入理解
开发语言·c++·面试