用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;
}
相关推荐
CoderCodingNo1 分钟前
【GESP】C++五级练习题 luogu-P1102 A-B 数对
开发语言·c++·算法
zhjadsf1 分钟前
模型量化基础知识 - PTQ - 训练后量化
人工智能
Fleshy数模3 分钟前
玩转OpenCV DNN模块:实现图片与实时视频风格迁移
opencv·音视频·dnn
Storynone4 分钟前
【踩坑笔记】Geforce RTX5060 显卡对应的 Pytorch 安装
人工智能·pytorch·笔记
Deepoch7 分钟前
Deepoc 具身模型开发板:重构机械臂扫地机智能清洁新范式
人工智能·科技·机械臂·具身模型·deepoc·扫地机
技术小黑9 分钟前
TensorFlow学习系列09 | 优化猫狗识别
人工智能·学习·tensorflow
指掀涛澜天下惊10 分钟前
AI 基础知识十三 Transformer注意力机制(Attention)
人工智能·深度学习·机器学习·transformer·q k v
weifont11 分钟前
太烧token了,我用Ai写了一个vscode的插件wps-editor(已开源)
人工智能·vscode·wps
cpp_250112 分钟前
B3873 [GESP202309 六级] 小杨买饮料
数据结构·c++·算法·动态规划·题解·洛谷
2301_7890156215 分钟前
C++11新增特性:可变参数模板、lambda表达式、function包装器、bind绑定、defult和delete
c语言·开发语言·c++·算法·c++11·万能引用