Overview of the DNN Module

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).

Basic Workflow

Steps to use DNN module:

  1. Load the model using cv::dnn::readNet.
  2. Convert input image to a blob using cv::dnn::blobFromImage.
  3. Set input to the network via net.setInput().
  4. Run inference using net.forward().
  5. Post-process the results.

#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;
}
    

Supported Model Formats

Commonly Supported Formats:

Note: Use readNetFromONNX, readNetFromTensorflow, readNetFromCaffe, and readNetFromTorch accordingly.

Working with Backends and Targets

Available Backends:

Available Targets:


Net net = readNetFromONNX("model.onnx");
net.setPreferableBackend(DNN_BACKEND_CUDA);
net.setPreferableTarget(DNN_TARGET_CUDA);
    

Blob Conversion Options

blobFromImage Parameters:


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.

Object Detection Example (YOLOv5 ONNX)

Postprocessing:

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.

Model Optimization Tips

Useful Tools and Resources