OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉库,提供了丰富的图像处理和计算机视觉算法。它支持多平台(包括 Windows、Linux、macOS)和多种编程语言(如 C++、Python、Java),使其成为研究、开发和部署计算机视觉应用的重要工具之一。
步骤 1: 创建项目目录和必要的文件
-
创建项目目录并进入:
mkdir yolov8_project cd yolov8_project mkdir build
-
创建
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}")
-
创建
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,</
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » mac安装opencv并在vscode中配置c++环境调试推理YOLOv8网络模型
发表评论 取消回复