OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉库,提供了丰富的图像处理和计算机视觉算法。它支持多平台(包括 Windows、Linux、macOS)和多种编程语言(如 C++、Python、Java),使其成为研究、开发和部署计算机视觉应用的重要工具之一。

步骤 1: 创建项目目录和必要的文件

  1. 创建项目目录并进入:

    mkdir yolov8_project
    cd yolov8_project
    mkdir build
    
  2. 创建 CMakeLists.txt 文件并添加内容:

    touch CMakeLists.txt
    

    内容如下:

    cmake_minimum_required(VERSION 3.10)
    project(yolov8)
    
    set(CMAKE_CXX_STANDARD 14)
    # Set OpenCV_DIR to the directory where OpenCVConfig.cmake is located
    set(OpenCV_DIR /usr/local/opt/opencv/lib/cmake/opencv4)
    
    # Find OpenCV package
    find_package(OpenCV REQUIRED)
    
    # Include OpenCV directories
    include_directories(${OpenCV_INCLUDE_DIRS})
    
    # Link OpenCV libraries
    add_executable(yolov8 yolov8.cpp)
    target_link_libraries(yolov8 ${OpenCV_LIBS})
    
    # Print OpenCV include directories and libraries
    message(STATUS "OpenCV include directories: ${OpenCV_INCLUDE_DIRS}")
    message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
    
  3. 创建 yolov8.cpp 文件并添加您的代码:

    touch yolov8.cpp
    

    内容如下:

    #include <iostream>
    #include <opencv2/opencv.hpp>
    #include <opencv2/dnn/all_layers.hpp>
    #include <fstream>
    
    using namespace std;
    using namespace cv;
    
    struct DCSP_RESULT {
         
        int classId;
        float confidence;
        cv::Rect box;
    };
    
    void process(cv::Mat& blob, cv::dnn::Net& net, std::vector<cv::Mat>& outputs) {
         
        net.setInput(blob);
        net.forward(outputs, net.getUnconnectedOutLayersNames());
    }
    
    void pre_process(cv::Mat& image, cv::Mat& blob) {
         
        cv::dnn::blobFromImage(image, blob, 1. / 255., cv::Size(640, 640), cv::Scalar(0, 0, 0), true, false);
    }
    
    void saveTensor(const std::string& filename, const cv::Mat& tensor) {
         
        std::ofstream file(filename, std::ios::binary);
        if (file.is_open()) {
         
            file.write(reinterpret_cast<const char*>(tensor.data), tensor.total() * tensor.elemSize());
            file.close();
        }
    }
    
    void post_process(cv::Mat& image, const std::vector<cv::Mat>& outs, float confThreshold,</

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部