这段代码怎么拔X Y坐标放数组里for (size_t j = 0; j < res.size(); j++) { cv::Rect r = get_rect(img, res[j].bbox); cv::rectangle(img, r, cv::Scalar(0x27, 0xC1, 0x36), 2); cv::putText(img, std::to_string(r.x) + "," + std::to_string(r.y), cv::Point(r.x, r.y - 15), cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(0xFF, 0xFF, 0xFF), 2); }
时间: 2024-03-05 11:47:32 浏览: 19
可以定义两个数组,一个用于存放X坐标,另一个用于存放Y坐标。在循环中,每次取得矩形框的坐标,将X坐标和Y坐标分别存入对应的数组中。
代码示例:
```
std::vector<int> xCoords;
std::vector<int> yCoords;
for (size_t j = 0; j < res.size(); j++) {
cv::Rect r = get_rect(img, res[j].bbox);
cv::rectangle(img, r, cv::Scalar(0x27, 0xC1, 0x36), 2);
cv::putText(img, std::to_string(r.x) + "," + std::to_string(r.y), cv::Point(r.x, r.y - 15), cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(0xFF, 0xFF, 0xFF), 2);
xCoords.push_back(r.x);
yCoords.push_back(r.y);
}
```
在上述代码中,xCoords和yCoords分别为存放X坐标和Y坐标的数组。每次循环将矩形框的坐标存入对应的数组中。
相关问题
这段代码怎么把类名和X Y坐标放数组里for (size_t j = 0; j < res.size(); j++) { cv::Rect r = get_rect(img, res[j].bbox); cv::rectangle(img, r, cv::Scalar(0x27, 0xC1, 0x36), 2); cv::putText(img, std::to_string(r.x) + "," + std::to_string(r.y), cv::Point(r.x, r.y - 15), cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(0xFF, 0xFF, 0xFF), 2); }
可以定义一个结构体,包含类名、X坐标和Y坐标三个成员变量,将每个检测到的对象的类名和坐标存入一个该结构体的实例中,再将该实例存入一个数组中。
代码示例:
```
struct ObjectInfo {
std::string className;
int xCoord;
int yCoord;
};
std::vector<ObjectInfo> objects;
for (size_t j = 0; j < res.size(); j++) {
cv::Rect r = get_rect(img, res[j].bbox);
cv::rectangle(img, r, cv::Scalar(0x27, 0xC1, 0x36), 2);
cv::putText(img, std::to_string(r.x) + "," + std::to_string(r.y), cv::Point(r.x, r.y - 15), cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(0xFF, 0xFF, 0xFF), 2);
ObjectInfo obj;
obj.className = res[j].class_name;
obj.xCoord = r.x;
obj.yCoord = r.y;
objects.push_back(obj);
}
```
在上述代码中,ObjectInfo为定义的结构体,包含三个成员变量:className、xCoord和yCoord。每次循环将检测到的对象的类名和坐标存入一个ObjectInfo实例中,再将该实例存入objects数组中。最终,objects数组中存放了所有检测到的对象的类名和坐标信息。
写下这段代码怎么绘制框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;
这段代码是目标检测算法输出结果的处理代码,如果需要在图像上绘制检测框,可以按照以下步骤进行:
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);
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)