Skip to main content

Overview

OpenCV DNN provides utility functions for preparing inputs, processing outputs, and managing inference workflows.

Blob Creation

blobFromImage

Convert single image to 4D blob:
Parameters:
  • image: Input image (any size, any channels)
  • scalefactor: Multiplier for pixel values
  • size: Target spatial dimensions
  • mean: Values to subtract from channels
  • swapRB: Swap red and blue channels (BGR to RGB)
  • crop: Crop image after resize
  • ddepth: Output depth (typically CV_32F)
Example:

blobFromImages

Convert multiple images to single blob (batching):
Example:

imagesFromBlob

Convert blob back to images:
Example:

Blob Utilities

getPlane

Extract single plane from blob:
Parameters:
  • m: 4D blob [N, C, H, W]
  • n: Batch index
  • cn: Channel index
Example:

getMatFromNet

Get layer activations:

NMS (Non-Maximum Suppression)

NMSBoxes

Filter overlapping bounding boxes:
Parameters:
  • bboxes: Bounding boxes
  • scores: Confidence scores
  • score_threshold: Minimum score to keep
  • nms_threshold: IoU threshold (typically 0.4-0.5)
  • indices: Output indices of kept boxes
  • eta: Adaptive NMS parameter
  • top_k: Keep top K boxes (0 = all)
Example:

NMSBoxesBatched

NMS for batched detections:

Softmax

softmax

Apply softmax activation:
Example:

Backend Queries

getAvailableBackends

Query available backends:
Example:

getAvailableTargets

Query targets for backend:
Example:

Model Diagnostics

enableModelDiagnostics

Enable verbose model loading:
Example:

Complete Examples

Image Classification

Object Detection (YOLO)

Best Practices

Normalize Inputs

Match preprocessing used during training

Batch Processing

Use blobFromImages for multiple images

Apply NMS

Remove overlapping detections

Check Backend Support

Query available backends for optimization

See Also