opencv C++ dnn模块调用yolov5以及Intel RealSense D435深度相机联合使用进行目标检测

一、代码

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API

using namespace cv;
using namespace dnn;
using namespace std;
using namespace rs2;

// 类名数组,这里需要替换为实际YOLO模型所检测的对象的类名
const char* classNames[] = {"object1", "object2", "object3", "object4"};

int main(int argc, char** argv)
{
    // 模型权重和配置文件路径,这些文件包含了训练好的YOLO模型参数和网络配置
    String model = "yolov8.onnx";  // 替换为实际模型文件路径

    // 加载预训练的模型和配置到DNN网络中
    Net net = readNetFromONNX(model);
    // 设置推理引擎后端为OpenCV,目标设备为CPU
    net.setPreferableBackend(DNN_BACKEND_OPENCV);
    net.setPreferableTarget(DNN_TARGET_CPU);

    // Declare depth colorizer for pretty visualization of depth data
    colorizer color_map;

    // Declare RealSense pipeline, encapsulating the actual device and sensors
    pipeline p;
    // Start streaming with default recommended configuration
    p.start();

    // 循环直到用户按下键盘上的任意键
    while (waitKey(1) < 0) {
        // Wait for the next set of frames from the camera
        frameset frames = p.wait_for_frames();
        // Get a frame from the RGB camera
        frame color = frames.get_color_frame();

        // Create OpenCV matrix of size (color_height, color_width)
        Mat frame(Size(640, 480), CV_8UC3, (void*)color.get_data(), Mat::AUTO_STEP);

        Mat blob;   // 用于存储处理后的图像,以适应网络输入

        // 将帧图像转换为网络输入所需格式
        blobFromImage(frame, blob, 1/255.0, cv::Size(416, 416), Scalar(0,0,0), true, false);

        // 将blob设置为网络的输入
        net.setInput(blob);

        // 运行前向传递以获取网络的输出层
        vector<Mat> outs;
        net.forward(outs, net.getUnconnectedOutLayersNames());

        // 遍历网络输出的每一层结果
        for (size_t i = 0; i < outs.size(); ++i) {
            for (int j = 0; j < outs[i].rows; ++j) {
                Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
                Point classIdPoint;
                double confidence;

                minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);

                if (confidence > 0.5) {
                    int centerX = (int)(outs[i].at<float>(j, 0) * frame.cols);
                    int centerY = (int)(outs[i].at<float>(j, 1) * frame.rows);
                    int width = (int)(outs[i].at<float>(j, 2) * frame.cols);
                    int height = (int)(outs[i].at<float>(j, 3) * frame.rows);
                    int left = centerX - width / 2;
                    int top = centerY - height / 2;

                    rectangle(frame, Rect(left, top, width, height), Scalar(0, 255, 0), 2);
                    int classIdx = static_cast<int>(classIdPoint.x);
                    string classLabel = string(classNames[classIdx]);
                    string label = classLabel + ":" + format("%.2f", confidence);
                    
                    int baseLine;
                    Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
                    top = max(top, labelSize.height);
                    rectangle(frame, Point(left, top - labelSize.height), Point(left + labelSize.width, top + baseLine), Scalar::all(255), FILLED);
                    putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
                }
            }
        }

        // 展示处理后的帧
        imshow("YoloV8", frame);
    }

    return 0;
}

注意:由于手头上没有该摄像头,本人只是查询资料,以及文档之后写的代码,并没有实操

二、安装包

需要安装opencv、librealsense2库

链接:Intel.RealSense.SDK.zip资源-CSDN文库

相关推荐
喜欢吃燃面6 分钟前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked939 分钟前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
虚拟之1 小时前
36、stringstream
c++
我很好我还能学1 小时前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
南岩亦凛汀3 小时前
在Linux下使用wxWidgets进行跨平台GUI开发
c++·跨平台·gui·开源框架·工程实战教程
曦月逸霜3 小时前
第34次CCF-CSP认证真题解析(目标300分做法)
数据结构·c++·算法
galaxy_strive3 小时前
绘制饼图详细过程
开发语言·c++·qt
Unpredictable2224 小时前
【VINS-Mono算法深度解析:边缘化策略、初始化与关键技术】
c++·笔记·算法·ubuntu·计算机视觉
jndingxin5 小时前
OpenCV CUDA模块图像处理------创建一个模板匹配(Template Matching)对象函数createTemplateMatching()
图像处理·人工智能·opencv
PingdiGuo_guo5 小时前
C++智能指针的知识!
开发语言·c++