opencv+c++YOLO
时间: 2023-08-30 14:11:02 浏览: 119
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.
阅读全文