Table of Contents

Class Cv2.Dnn

Namespace
OpenCvSharp
Assembly
OpenCvSharp.dll

cv::dnn functions

public static class Cv2.Dnn
Inheritance
Cv2.Dnn
Inherited Members

Methods

BlobFromImage(Mat, double, Size, Scalar, bool, bool)

Creates 4-dimensional blob from image. Optionally resizes and crops @p image from center, subtract @p mean values, scales values by @p scalefactor, swap Blue and Red channels.

public static Mat BlobFromImage(Mat image, double scaleFactor = 1, Size size = default, Scalar mean = default, bool swapRB = true, bool crop = true)

Parameters

image Mat

input image (with 1- or 3-channels).

scaleFactor double

multiplier for @p image values.

size Size

spatial size for output image

mean Scalar

scalar with mean values which are subtracted from channels. Values are intended to be in (mean-R, mean-G, mean-B) order if @p image has BGR ordering and @p swapRB is true.

swapRB bool

flag which indicates that swap first and last channels in 3-channel image is necessary.

crop bool

flag which indicates whether image will be cropped after resize or not

Returns

Mat

4-dimansional Mat with NCHW dimensions order.

Remarks

if @p crop is true, input image is resized so one side after resize is equal to corresponing dimension in @p size and another one is equal or larger.Then, crop from the center is performed. If @p crop is false, direct resize without cropping and preserving aspect ratio is performed.

BlobFromImageWithParams(InputArray, Image2BlobParams?)

Creates 4-dimensional blob from image with given params. This function is an extension of BlobFromImage(Mat, double, Size, Scalar, bool, bool) to meet more image preprocess needs.

public static Mat BlobFromImageWithParams(InputArray image, Image2BlobParams? param = null)

Parameters

image InputArray

input image (all with 1-, 3- or 4-channels).

param Image2BlobParams

params with preprocessing parameters. If null, default params are used.

Returns

Mat

4-dimensional Mat.

BlobFromImages(IEnumerable<Mat>, double, Size, Scalar, bool, bool)

Creates 4-dimensional blob from series of images. Optionally resizes and crops @p images from center, subtract @p mean values, scales values by @p scalefactor, swap Blue and Red channels.

public static Mat BlobFromImages(IEnumerable<Mat> images, double scaleFactor, Size size = default, Scalar mean = default, bool swapRB = true, bool crop = true)

Parameters

images IEnumerable<Mat>

input images (all with 1- or 3-channels).

scaleFactor double

multiplier for @p image values.

size Size

spatial size for output image

mean Scalar

scalar with mean values which are subtracted from channels. Values are intended to be in (mean-R, mean-G, mean-B) order if @p image has BGR ordering and @p swapRB is true.

swapRB bool

flag which indicates that swap first and last channels in 3-channel image is necessary.

crop bool

flag which indicates whether image will be cropped after resize or not

Returns

Mat

4-dimansional Mat with NCHW dimensions order.

Remarks

if @p crop is true, input image is resized so one side after resize is equal to corresponing dimension in @p size and another one is equal or larger.Then, crop from the center is performed. If @p crop is false, direct resize without cropping and preserving aspect ratio is performed.

BlobFromImagesWithParams(IEnumerable<Mat>, Image2BlobParams?)

Creates 4-dimensional blob from series of images with given params.

public static Mat BlobFromImagesWithParams(IEnumerable<Mat> images, Image2BlobParams? param = null)

Parameters

images IEnumerable<Mat>

input images (all with 1-, 3- or 4-channels).

param Image2BlobParams

params with preprocessing parameters. If null, default params are used.

Returns

Mat

4-dimensional Mat.

EnableModelDiagnostics(bool)

Enables detailed logging of the DNN model loading with CV DNN API. Diagnostic mode provides detailed logging of the model loading stage to explore potential problems (ex.: not implemented layer type).

public static void EnableModelDiagnostics(bool isDiagnosticsMode)

Parameters

isDiagnosticsMode bool

indicates whether diagnostic mode should be set.

GetAvailableBackends()

Returns the list of available (backend, target) pairs supported by this build.

public static (Backend Backend, Target Target)[] GetAvailableBackends()

Returns

(Backend Backend, Target Target)[]

available backend/target pairs.

GetAvailableTargets(Backend)

Returns the list of available computation targets for the given backend.

public static Target[] GetAvailableTargets(Backend backend)

Parameters

backend Backend

backend identifier.

Returns

Target[]

available targets.

ImagesFromBlob(Mat)

Parse a 4D blob and output the images it contains as 2D arrays through a simpler data structure (std::vector<cv::Mat>).

public static Mat[] ImagesFromBlob(Mat blob)

Parameters

blob Mat

4 dimensional array (images, channels, height, width) in floating point precision (CV_32F) from which you would like to extract the images.

Returns

Mat[]

array of 2D Mat containing the images extracted from the blob in floating point precision (CV_32F).

NMSBoxes(IEnumerable<Rect2d>, IEnumerable<float>, float, float, out int[], float, int)

Performs non maximum suppression given boxes and corresponding scores.

public static void NMSBoxes(IEnumerable<Rect2d> bboxes, IEnumerable<float> scores, float scoreThreshold, float nmsThreshold, out int[] indices, float eta = 1, int topK = 0)

Parameters

bboxes IEnumerable<Rect2d>

a set of bounding boxes to apply NMS.

scores IEnumerable<float>

a set of corresponding confidences.

scoreThreshold float

a threshold used to filter boxes by score.

nmsThreshold float

a threshold used in non maximum suppression.

indices int[]

the kept indices of bboxes after NMS.

eta float

a coefficient in adaptive threshold formula

topK int

if >0, keep at most @p top_k picked indices.

NMSBoxes(IEnumerable<Rect>, IEnumerable<float>, float, float, out int[], float, int)

Performs non maximum suppression given boxes and corresponding scores.

public static void NMSBoxes(IEnumerable<Rect> bboxes, IEnumerable<float> scores, float scoreThreshold, float nmsThreshold, out int[] indices, float eta = 1, int topK = 0)

Parameters

bboxes IEnumerable<Rect>

a set of bounding boxes to apply NMS.

scores IEnumerable<float>

a set of corresponding confidences.

scoreThreshold float

a threshold used to filter boxes by score.

nmsThreshold float

a threshold used in non maximum suppression.

indices int[]

the kept indices of bboxes after NMS.

eta float

a coefficient in adaptive threshold formula

topK int

if >0, keep at most @p top_k picked indices.

NMSBoxes(IEnumerable<RotatedRect>, IEnumerable<float>, float, float, out int[], float, int)

Performs non maximum suppression given boxes and corresponding scores.

public static void NMSBoxes(IEnumerable<RotatedRect> bboxes, IEnumerable<float> scores, float scoreThreshold, float nmsThreshold, out int[] indices, float eta = 1, int topK = 0)

Parameters

bboxes IEnumerable<RotatedRect>

a set of bounding boxes to apply NMS.

scores IEnumerable<float>

a set of corresponding confidences.

scoreThreshold float

a threshold used to filter boxes by score.

nmsThreshold float

a threshold used in non maximum suppression.

indices int[]

the kept indices of bboxes after NMS.

eta float

a coefficient in adaptive threshold formula

topK int

if >0, keep at most @p top_k picked indices.

NMSBoxesBatched(IEnumerable<Rect2d>, IEnumerable<float>, IEnumerable<int>, float, float, out int[], float, int)

Performs batched non maximum suppression on given boxes and corresponding scores across different classes.

public static void NMSBoxesBatched(IEnumerable<Rect2d> bboxes, IEnumerable<float> scores, IEnumerable<int> classIds, float scoreThreshold, float nmsThreshold, out int[] indices, float eta = 1, int topK = 0)

Parameters

bboxes IEnumerable<Rect2d>

a set of bounding boxes to apply NMS.

scores IEnumerable<float>

a set of corresponding confidences.

classIds IEnumerable<int>

a set of corresponding class ids. Ids are integer and usually start from 0.

scoreThreshold float

a threshold used to filter boxes by score.

nmsThreshold float

a threshold used in non maximum suppression.

indices int[]

the kept indices of bboxes after NMS.

eta float

a coefficient in adaptive threshold formula

topK int

if >0, keep at most @p top_k picked indices.

NMSBoxesBatched(IEnumerable<Rect>, IEnumerable<float>, IEnumerable<int>, float, float, out int[], float, int)

Performs batched non maximum suppression on given boxes and corresponding scores across different classes.

public static void NMSBoxesBatched(IEnumerable<Rect> bboxes, IEnumerable<float> scores, IEnumerable<int> classIds, float scoreThreshold, float nmsThreshold, out int[] indices, float eta = 1, int topK = 0)

Parameters

bboxes IEnumerable<Rect>

a set of bounding boxes to apply NMS.

scores IEnumerable<float>

a set of corresponding confidences.

classIds IEnumerable<int>

a set of corresponding class ids. Ids are integer and usually start from 0.

scoreThreshold float

a threshold used to filter boxes by score.

nmsThreshold float

a threshold used in non maximum suppression.

indices int[]

the kept indices of bboxes after NMS.

eta float

a coefficient in adaptive threshold formula

topK int

if >0, keep at most @p top_k picked indices.

ReadNet(string, string, string, EngineType)

Read deep learning network represented in one of the supported formats.

This function automatically detects an origin framework of trained model and calls an appropriate function such @ref readNetFromTensorflow, @ref readNetFromONNX, or @ref readNetFromModelOptimizer. The Caffe, Darknet and Torch parsers were removed in OpenCV 5.

public static Net ReadNet(string model, string config = "", string framework = "", EngineType engine = EngineType.Auto)

Parameters

model string

Binary file contains trained weights. The following file * extensions are expected for models from different frameworks: * * *.pb (TensorFlow, https://www.tensorflow.org/) * * *.onnx (ONNX, https://onnx.ai/) * * *.bin (OpenVINO, https://software.intel.com/openvino-toolkit)

config string

Text file contains network configuration. It could be a * file with the following extensions: * * *.pbtxt (TensorFlow, https://www.tensorflow.org/) * * *.xml (OpenVINO, https://software.intel.com/openvino-toolkit)

framework string

Explicit framework name tag to determine a format.

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

ReadNetFromONNX(byte[], EngineType)

Reads a network model ONNX https://onnx.ai/ from memory

public static Net? ReadNetFromONNX(byte[] onnxFileData, EngineType engine = EngineType.Auto)

Parameters

onnxFileData byte[]

memory of the first byte of the buffer.

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

ReadNetFromONNX(Stream, EngineType)

Reads a network model ONNX https://onnx.ai/ from stream.

public static Net? ReadNetFromONNX(Stream onnxFileStream, EngineType engine = EngineType.Auto)

Parameters

onnxFileStream Stream

memory of the first byte of the buffer.

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

ReadNetFromONNX(ReadOnlySpan<byte>, EngineType)

Reads a network model ONNX https://onnx.ai/ from memory

public static Net? ReadNetFromONNX(ReadOnlySpan<byte> onnxFileData, EngineType engine = EngineType.Auto)

Parameters

onnxFileData ReadOnlySpan<byte>

memory of the first byte of the buffer.

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

ReadNetFromONNX(string, EngineType)

Reads a network model ONNX https://onnx.ai/ from memory

public static Net? ReadNetFromONNX(string onnxFile, EngineType engine = EngineType.Auto)

Parameters

onnxFile string
engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

ReadNetFromTFLite(byte[], EngineType)

Reads a network model stored in TFLite framework's format from memory.

public static Net? ReadNetFromTFLite(byte[] bufferModel, EngineType engine = EngineType.Auto)

Parameters

bufferModel byte[]

buffer containing the content of the tflite file.

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

ReadNetFromTFLite(Stream, EngineType)

Reads a network model stored in TFLite framework's format from stream.

public static Net? ReadNetFromTFLite(Stream bufferModelStream, EngineType engine = EngineType.Auto)

Parameters

bufferModelStream Stream

stream containing the content of the tflite file.

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

ReadNetFromTFLite(ReadOnlySpan<byte>, EngineType)

Reads a network model stored in TFLite framework's format from memory.

public static Net? ReadNetFromTFLite(ReadOnlySpan<byte> bufferModel, EngineType engine = EngineType.Auto)

Parameters

bufferModel ReadOnlySpan<byte>

buffer containing the content of the tflite file.

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

ReadNetFromTFLite(string, EngineType)

Reads a network model stored in TFLite (https://www.tensorflow.org/lite) framework's format.

public static Net? ReadNetFromTFLite(string model, EngineType engine = EngineType.Auto)

Parameters

model string

path to the .tflite file with binary flatbuffers description of the network architecture.

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

ReadNetFromTensorflow(byte[], byte[]?, EngineType)

Reads a network model stored in Tensorflow model file from memory.

public static Net? ReadNetFromTensorflow(byte[] bufferModel, byte[]? bufferConfig = null, EngineType engine = EngineType.Auto)

Parameters

bufferModel byte[]

buffer containing the content of the pb file

bufferConfig byte[]

buffer containing the content of the pbtxt file (optional)

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

Remarks

This is shortcut consisting from createTensorflowImporter and Net::populateNet calls.

ReadNetFromTensorflow(Stream, Stream?, EngineType)

Reads a network model stored in Tensorflow model file from stream.

public static Net? ReadNetFromTensorflow(Stream bufferModel, Stream? bufferConfig = null, EngineType engine = EngineType.Auto)

Parameters

bufferModel Stream

buffer containing the content of the pb file

bufferConfig Stream

buffer containing the content of the pbtxt file (optional)

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

Remarks

This is shortcut consisting from createTensorflowImporter and Net::populateNet calls.

ReadNetFromTensorflow(string, string?, EngineType)

Reads a network model stored in Tensorflow model file.

public static Net? ReadNetFromTensorflow(string model, string? config = null, EngineType engine = EngineType.Auto)

Parameters

model string

path to the .pb file with binary protobuf description of the network architecture

config string

path to the .pbtxt file that contains text graph definition in protobuf format.

engine EngineType

DNN engine to use. Auto tries the new engine first and falls back to the classic one.

Returns

Net

Resulting Net object is built by text graph using weights from a binary one that let us make it more flexible.

Remarks

This is shortcut consisting from createTensorflowImporter and Net::populateNet calls.

ReadTensorFromONNX(string)

Creates blob from .pb file.

public static Mat? ReadTensorFromONNX(string path)

Parameters

path string

path to the .pb file with input tensor.

Returns

Mat

ResetMyriadDevice()

Release a Myriad device is binded by OpenCV.

Single Myriad device cannot be shared across multiple processes which uses Inference Engine's Myriad plugin.

public static void ResetMyriadDevice()

SoftNMSBoxes(IEnumerable<Rect>, IEnumerable<float>, out float[], float, float, out int[], int, float, SoftNMSMethod)

Performs soft non maximum suppression given boxes and corresponding scores. Reference: https://arxiv.org/abs/1704.04503

public static void SoftNMSBoxes(IEnumerable<Rect> bboxes, IEnumerable<float> scores, out float[] updatedScores, float scoreThreshold, float nmsThreshold, out int[] indices, int topK = 0, float sigma = 0.5, SoftNMSMethod method = SoftNMSMethod.SOFTNMS_GAUSSIAN)

Parameters

bboxes IEnumerable<Rect>

a set of bounding boxes to apply Soft NMS.

scores IEnumerable<float>

a set of corresponding confidences.

updatedScores float[]

a set of corresponding updated confidences.

scoreThreshold float

a threshold used to filter boxes by score.

nmsThreshold float

a threshold used in non maximum suppression.

indices int[]

the kept indices of bboxes after NMS.

topK int

keep at most @p top_k picked indices.

sigma float

parameter of Gaussian weighting.

method SoftNMSMethod

Gaussian or linear.

WriteTextGraph(string, string)

Create a text representation for a binary network stored in protocol buffer format.

public static void WriteTextGraph(string model, string output)

Parameters

model string

A path to binary network.

output string

A path to output text file to be created.