The cv::dnn
module in OpenCV enables loading and running deep learning models in C++. It supports frameworks such as TensorFlow, Caffe, ONNX, and PyTorch (via ONNX export).
CPU
, OpenCL
, and CUDA
backends.
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace cv::dnn;
int main() {
// Load model
Net net = readNetFromONNX("model.onnx");
// Read image
Mat img = imread("example.jpg");
// Convert image to blob
Mat blob = blobFromImage(img, 1.0/255.0, Size(224,224), Scalar(), true, false);
// Set blob as input
net.setInput(blob);
// Perform inference
Mat output = net.forward();
// Print top class index
Point classIdPoint;
double confidence;
minMaxLoc(output.reshape(1, 1), 0, &confidence, 0, &classIdPoint);
int classId = classIdPoint.x;
std::cout << "Class: " << classId << ", Confidence: " << confidence << std::endl;
return 0;
}
.pb
(TensorFlow).onnx
(ONNX format from PyTorch, TensorFlow, etc.).caffemodel
+ .prototxt
(Caffe).t7
(Torch)Note: Use readNetFromONNX
, readNetFromTensorflow
, readNetFromCaffe
, and readNetFromTorch
accordingly.
DNN_BACKEND_OPENCV
DNN_BACKEND_INFERENCE_ENGINE
(OpenVINO)DNN_BACKEND_CUDA
DNN_TARGET_CPU
DNN_TARGET_OPENCL
DNN_TARGET_CUDA
Net net = readNetFromONNX("model.onnx");
net.setPreferableBackend(DNN_BACKEND_CUDA);
net.setPreferableTarget(DNN_TARGET_CUDA);
blobFromImage(image, scaleFactor, size, mean, swapRB, crop, ddepth)
Note: Use swapRB=true
for RGB models, and set mean subtraction for normalization like Scalar(104,117,123)
for ImageNet models.
YOLO-style models require output parsing into bounding boxes, confidences, and class IDs, followed by non-maximum suppression.
// postprocess output of net.forward()
for (int i = 0; i < detections.rows; ++i) {
float conf = detections.at<float>(i, 4);
if (conf > threshold) {
// Extract class scores and apply NMS
}
}
Note: Use NMSBoxes()
for final selection of bounding boxes.
net.enableWinograd(false)
for better accuracy if needed.net.setPreferableBackend()
and setPreferableTarget()
to optimize for GPU or CPU.blobFromImages()
.