Opencv3.4.2+yolo3+目标检查与分类

写的比较好的一片文章,适合新手入门

link

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO (You Only Look Once) is an object detection algorithm that uses deep neural networks to detect objects in real-time. OpenCV is a popular computer vision library that provides various tools and functions for image processing and computer vision tasks. To use YOLO with OpenCV in C++, you need to follow these steps: 1. Download the YOLOv3 weights and configuration files from the official website. 2. Load the model using OpenCV's dnn module. 3. Read the input image and preprocess it. 4. Pass the image through the model to get the output. 5. Postprocess the output to get the object detection results. 6. Draw bounding boxes around the detected objects and display the output image. Here's a sample code that demonstrates how to use YOLO with OpenCV in C++: ``` #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { // Load YOLOv3 model String modelWeights = "yolov3.weights"; String modelConfiguration = "yolov3.cfg"; dnn::Net net = dnn::readNetFromDarknet(modelConfiguration, modelWeights); // Read input image Mat image = imread("input.jpg"); // Preprocess input image Mat blob = dnn::blobFromImage(image, 1/255.0, Size(416, 416), Scalar(0,0,0), true, false); // Set input for the network net.setInput(blob); // Get output from the network vector<Mat> outs; net.forward(outs, net.getUnconnectedOutLayersNames()); // Postprocess the output float confThreshold = 0.5; vector<int> classIds; vector<float> confidences; vector<Rect> boxes; for (size_t i = 0; i < outs.size(); ++i) { // Extract information from the output Mat output = outs[i]; for (int j = 0; j < output.rows; ++j) { Mat scores = output.row(j).colRange(5, output.cols); Point classId; double confidence; minMaxLoc(scores, 0, &confidence, 0, &classId); if (confidence > confThreshold) { // Get bounding box coordinates int centerX = static_cast<int>(output.at<float>(j, 0) * image.cols); int centerY = static_cast<int>(output.at<float>(j, 1) * image.rows); int width = static_cast<int>(output.at<float>(j, 2) * image.cols); int height = static_cast<int>(output.at<float>(j, 3) * image.rows); int left = centerX - width / 2; int top = centerY - height / 2; // Save detection results classIds.push_back(classId.x); confidences.push_back(static_cast<float>(confidence)); boxes.push_back(Rect(left, top, width, height)); } } } // Draw bounding boxes around the detected objects vector<String> classNames = {"person", "car", "bus", "truck"}; vector<Scalar> colors = {Scalar(0, 0, 255), Scalar(255, 0, 0), Scalar(0, 255, 0), Scalar(255, 255, 0)}; for (size_t i = 0; i < boxes.size(); ++i) { rectangle(image, boxes[i], colors[classIds[i]], 2); String label = format("%.2f", confidences[i]); label = classNames[classIds[i]] + ":" + label; int baseLine; Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine); rectangle(image, Point(boxes[i].x, boxes[i].y - labelSize.height - baseLine), Point(boxes[i].x + labelSize.width, boxes[i].y), colors[classIds[i]], FILLED); putText(image, label, Point(boxes[i].x, boxes[i].y - baseLine), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255,255,255)); } // Display the output image imshow("YOLO Object Detection", image); waitKey(0); return 0; } ``` Note that this sample code is for YOLOv3 and may need to be modified for other versions of YOLO. Also, make sure to download the correct weights and configuration files for the version you are using.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值