【C++ & Opencv】【第十六天】int a[]的输入输出,char a[]的输入输出,string a的输入输出,string a[]的输入输出的区别与联系

int a[],string a[]只能靠for循环对各个元素a[i]进行遍历来实现输入或输出;

char a[]与string a都叫做“字符串”,既可以通过for循环来实现输入输出,也可以通过cin和cout来实现输入输出。

要使用OpenCV C++解析Yolov5的推理输出结果,需要遵循以下步骤: 1.读取推理输出文件:Yolov5的推理输出通常是保存在一个文本文件中。使用C++的文件操作函数,可以读取该文件并将其存储到内存中。 2.解析输出信息:将存储在内存中的推理输出信息解析为一个包含所有检测目标信息的结构体或向量。每个目标的信息包括目标边界框的位置和大小、目标类别、以及目标置信度等信息。 3.绘制检测结果:使用OpenCV的绘图函数,将解析出的每个目标的信息在图像或视频中绘制出来。绘制的内容包括目标边界框的矩形框、目标类别标签、以及置信度等信息。 以下是一个简单的代码示例,展示了如何使用OpenCV C++解析Yolov5的推理输出结果: ```c++ #include <fstream> #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; struct DetectionObject { Rect bbox; string label; float confidence; }; vector<DetectionObject> parse_yolov5_output(const string& output_file, float confidence_threshold = 0.5) { vector<DetectionObject> detection_objects; ifstream ifs(output_file); string line; while (getline(ifs, line)) { vector<string> fields; stringstream ss(line); string field; while (getline(ss, field, ' ')) { fields.push_back(field); } if (fields.size() == 6) { float confidence = stof(fields[4]); if (confidence > confidence_threshold) { Rect bbox(stoi(fields[0]), stoi(fields[1]), stoi(fields[2]), stoi(fields[3])); string label = fields[5]; detection_objects.push_back({bbox, label, confidence}); } } } return detection_objects; } void draw_detection_result(Mat& image, const vector<DetectionObject>& detection_objects) { for (const auto& obj : detection_objects) { rectangle(image, obj.bbox, Scalar(0, 0, 255), 2); putText(image, obj.label, Point(obj.bbox.x, obj.bbox.y - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 1); putText(image, to_string(obj.confidence), Point(obj.bbox.x + obj.bbox.width, obj.bbox.y - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 1); } } int main(int argc, char** argv) { string output_file = "yolov5_output.txt"; Mat image = imread("test.jpg"); vector<DetectionObject> detection_objects = parse_yolov5_output(output_file); draw_detection_result(image, detection_objects); imshow("Detection Result", image); waitKey(0); return 0; } ``` 在上述示例代码中,`parse_yolov5_output`函数用于解析Yolov5的推理输出文件,返回一个包含所有检测目标信息的结构体向量;`draw_detection_result`函数用于将解析出的检测目标信息在图像中绘制出来。最后在`main`函数中,读取图像,并调用上述两个函数进行解析和绘图,并显示结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值