写下这段代码怎么绘制框doInference(*trt->context, trt->stream, trt->buffers, trt->data, trt->prob, BATCH_SIZE); std::vector<std::vectorYolo::Detection> batch_res(1); auto& res = batch_res[0]; nms(res, &trt->prob[0 * OUTPUT_SIZE], CONF_THRESH, NMS_THRESH); int len = res.size(); // 结果数组 float(*res_array)[6] = new float[len][6]; // 得到所有目标的数据 for (size_t j = 0; j < res.size(); j++) { cv::Rect r = get_rect(img, res[j].bbox); res_array[j][0] = r.x; res_array[j][1] = r.y; res_array[j][2] = r.width; res_array[j][3] = r.height; res_array[j][4] = res[j].class_id; res_array[j][5] = res[j].conf;
时间: 2024-01-01 18:05:27 浏览: 332
这段代码是目标检测算法输出结果的处理代码,如果需要在图像上绘制检测框,可以按照以下步骤进行:
1. 遍历结果数组中的每个目标,获取目标的坐标和大小信息:
```c++
for (size_t j = 0; j < res.size(); j++) {
cv::Rect r = get_rect(img, res[j].bbox);
float x = r.x;
float y = r.y;
float w = r.width;
float h = r.height;
int class_id = res[j].class_id;
float conf = res[j].conf;
// ...
}
```
2. 计算边界框的四个顶点坐标:
```c++
float left = x - w / 2;
float top = y - h / 2;
float right = x + w / 2;
float bottom = y + h / 2;
```
3. 使用 OpenCV 的 rectangle() 函数绘制边界框:
```c++
cv::rectangle(img, cv::Point(left, top), cv::Point(right, bottom), cv::Scalar(0, 255, 0), 2);
```
其中,cv::Scalar(0, 255, 0) 表示绘制框的颜色,2 表示绘制框的线宽。
完整代码如下:
```c++
doInference(*trt->context, trt->stream, trt->buffers, trt->data, trt->prob, BATCH_SIZE);
std::vector<std::vector<Yolo::Detection>> batch_res(1);
auto& res = batch_res[0];
nms(res, &trt->prob[0 * OUTPUT_SIZE], CONF_THRESH, NMS_THRESH);
int len = res.size();
// 结果数组
float(*res_array)[6] = new float[len][6];
// 得到所有目标的数据
for (size_t j = 0; j < res.size(); j++) {
cv::Rect r = get_rect(img, res[j].bbox);
float x = r.x;
float y = r.y;
float w = r.width;
float h = r.height;
int class_id = res[j].class_id;
float conf = res[j].conf;
// 计算边界框的四个顶点坐标
float left = x - w / 2;
float top = y - h / 2;
float right = x + w / 2;
float bottom = y + h / 2;
// 绘制边界框
cv::rectangle(img, cv::Point(left, top), cv::Point(right, bottom), cv::Scalar(0, 255, 0), 2);
}
```
阅读全文