news 2026/9/1 11:05:14

C++实现动态前瞻

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++实现动态前瞻

自己写的代码,可能有问题,参数是随便给的,自己调也不麻烦

#include <opencv2/opencv.hpp>

#include

using namespace cv;

const int img_center_col = 320;

const int total_rows = 480;

int valid_rows = 0;

float valid_ratio = 1.0f;

const float valid_row_thresh = 0.7f; // 有效行阈值

// 输入:二值图 binImg(黑白图)

// 输出:speed —— 最终速度

float getSpeedByCurvatureAndWhite(Mat &binImg,char cishu)

{

float factor = 1.0f;

// 1. 统计白点数量

int whiteCount = countNonZero(binImg);

// 归一化到 0~1

float totalPixel = binImg.rows * binImg.cols;

float whiteRatio = (float)whiteCount / totalPixel;

whiteRatio = fmin(fmax(whiteRatio, 0.0f), 1.0f); // 限制0~1

//2. 计算曲率

float curvature = calcCurvature(binImg);

// 取车正前方最近的行(最下方行)计算偏移

int row = binImg.rows - 10;

float center_col = getCenterAtRow(binImg, row); // 找中心

// 计算偏移量(归一化到0~1)

float offset = fabs(center_col - img_center_col) / (binImg.cols / 2.0f);//img_center_col:中心点坐标

offset = fmin(offset, 1.0f); // 限制最大偏移为1

valid_rows = 0; //有效行数

int check_rows = 0; // 统计实际检查的行数

// 统计有效行(能找到中心的行)

for (int y = 20; y < total_rows - 20; y += 5) { // 每隔5行统计,减少计算

check_rows ++;

if (getCenterAtRow(binImg, y) >= 0) {

valid_rows++;

}

}

if (check_rows > 0)

{ // 防止除0崩溃

valid_ratio = (float)valid_rows / check_rows;

}

else

{

valid_ratio = 0.3f; // 没检查到行,强制减速

}

if(valid_ratio < valid_row_thresh)

{

valid_ratio = valid_ratio / valid_row_thresh;

}

else

valid_ratio = 1;

//3. 函数算速度

const float v_max = 80.0f; // 直道最大速度

const float v_min = 25.0f; // 最小速度

const float k_curve = 0.9f; // 曲率强度

const float k_white = 0.7f; // 白点抑制强度

switch (cishu) {

case 1: // 一次曲线(线性)

{

factor = 1.0f - k_curve * curvature - k_white * whiteRatio;

break;

}

case 2: // 二次曲线(平缓)

{

float curveFactor = 1.0f - k_curve * curvature * curvature;

float whiteFactor = 1.0f - k_white * whiteRatio * whiteRatio;

factor = curveFactor * whiteFactor * offset * valid_ratio;

break;

}

case 3: // 三次曲线(急弯减速最大)

{

float curveFactor = 1.0f - k_curve * pow(curvature, 3);

float whiteFactor = 1.0f - k_white * pow(whiteRatio, 3);

factor = curveFactor * whiteFactor * offset * valid_ratio;

break;

}

default: // 传错值时默认用二次曲线

{

float curveFactor = 1.0f - k_curve * curvature * curvature;

float whiteFactor = 1.0f - k_white * whiteRatio * whiteRatio;

factor = curveFactor * whiteFactor * offset * valid_ratio;

break;

}

}

// 限幅,防止速度为负

factor = fmax(factor, 0.0f);

// 最终速度

return v_min + (v_max - v_min) * factor;

}

// 从二值图计算赛道曲率(0~1)

float calcCurvature(Mat &binImg)

{

int h = binImg.rows;

int w = binImg.cols;

// 取三行:下、中、上

int y1 = h - 20; // 近

int y2 = h / 2; // 中

int y3 = 20; // 远

// 找每行中心

float cx1 = getCenterAtRow(binImg, y1);

float cx2 = getCenterAtRow(binImg, y2);

float cx3 = getCenterAtRow(binImg, y3);

if (cx1 < 0 || cx2 < 0 || cx3 < 0)

return 1.0; // 丢线 → 曲率最大,速度最慢

// 计算偏差 → 代表弯曲程度

float err1 = fabs(cx2 - cx1);

float err2 = fabs(cx3 - cx2);

float totalErr = err1 + err2;

// 归一化曲率 0~1

float curvature = totalErr / (w / 2.0f);

curvature = fmin(curvature, 1.0f);

return curvature;

}

float getCenterAtRow(Mat &binImg, int y)

{

if (y < 0 || y >= binImg.rows) return -1; // 行号越界返回-1

uchar *ptr = binImg.ptr(y);

int w = binImg.cols;

int left = -1, right = -1;

// 找左边缘

for (int x = 0; x < w; x++) {

if (ptr[x] == 255) {

left = x;

break;

}

}

// 找右边缘

for (int x = w - 1; x >= 0; x--) {

if (ptr[x] == 255) {

right = x;

break;

}

}

// 没找到边缘返回-1,否则返回中心点

return (left == -1 || right == -1) ? -1 : (left + right) / 2.0f;

}

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/1 11:03:19

AI 时代测试员的进化:从“Bug猎人”到“质量策略专家”

前言测试工程师这个职业&#xff0c;长期以来有一个根深蒂固的自我定义&#xff1a;发现 Bug 的人。这个定义不错&#xff0c;但它太窄了。它把测试工作的价值&#xff0c;锚定在一个具体的、可被计数的产出物上——缺陷单的数量、严重级别的分布、发现率的高低。这套评价体系在…

作者头像 李华
网站建设 2026/7/14 17:24:56

Leetcode 137 组合 | 电话号码的字母组合

新的模块&#xff0c;回溯算法解题套路框架 1 题目 77. 组合 给定两个整数 n 和 k&#xff0c;返回范围 [1, n] 中所有可能的 k 个数的组合。 你可以按 任何顺序 返回答案。 示例 1&#xff1a; 输入&#xff1a;n 4, k 2 输出&#xff1a; [[2,4],[3,4],[2,3],[1,2],[…

作者头像 李华
网站建设 2026/7/14 17:24:56

Python 自然语言处理(NLP)

本文是自然语言处理&#xff08;NLP&#xff09;课程的实践总结&#xff0c;基于 Python 实现了文本分词、词频统计、词性分析、命名实体识别等核心功能&#xff0c;并通过可视化图表和 GUI 界面直观展示结果。技术栈涵盖jieba&#xff08;分词&#xff09;、matplotlib&#x…

作者头像 李华
网站建设 2026/7/14 17:24:59

MQTT 协议详解

MQTT&#xff08;Message Queuing Telemetry Transport&#xff0c;消息队列遥测传输&#xff09;是ISO/IEC 20922标准化的、基于发布 / 订阅&#xff08;Pub/Sub&#xff09;范式的轻量级消息传输协议&#xff0c;专为低带宽、高延迟、网络不稳定、算力 / 内存受限的物联网&am…

作者头像 李华