Table of Contents

Class CvDnn

Namespace
OpenCvSharp.Dnn
Assembly
OpenCvSharp.dll

cv::dnn functions

public static class CvDnn
Inheritance
CvDnn
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.

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.

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.

ReadNet(string, string, string)

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 readNetFromCaffe, @ref readNetFromTensorflow,

public static Net ReadNet(string model, string config = "", string framework = "")

Parameters

model string

Binary file contains trained weights. The following file * extensions are expected for models from different frameworks: * * *.caffemodel (Caffe, http://caffe.berkeleyvision.org/) * * *.pb (TensorFlow, https://www.tensorflow.org/) * * *.t7 | *.net (Torch, http://torch.ch/) * * *.weights (Darknet, https://pjreddie.com/darknet/) * * *.bin (DLDT, https://software.intel.com/openvino-toolkit)

config string

Text file contains network configuration. It could be a * file with the following extensions: * * *.prototxt (Caffe, http://caffe.berkeleyvision.org/) * * *.pbtxt (TensorFlow, https://www.tensorflow.org/) * * *.cfg (Darknet, https://pjreddie.com/darknet/) * * *.xml (DLDT, https://software.intel.com/openvino-toolkit)

framework string

Explicit framework name tag to determine a format.

Returns

Net

ReadNetFromCaffe(byte[], byte[]?)

Reads a network model stored in Caffe model files from memory.

public static Net? ReadNetFromCaffe(byte[] bufferProto, byte[]? bufferModel = null)

Parameters

bufferProto byte[]

buffer containing the content of the .prototxt file

bufferModel byte[]

buffer containing the content of the .caffemodel file

Returns

Net

Remarks

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

ReadNetFromCaffe(Stream, Stream?)

Reads a network model stored in Caffe model files from Stream.

public static Net? ReadNetFromCaffe(Stream bufferProto, Stream? bufferModel = null)

Parameters

bufferProto Stream

buffer containing the content of the .prototxt file

bufferModel Stream

buffer containing the content of the .caffemodel file

Returns

Net

Remarks

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

ReadNetFromCaffe(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Reads a network model stored in Caffe model files from memory.

public static Net? ReadNetFromCaffe(ReadOnlySpan<byte> bufferProto, ReadOnlySpan<byte> bufferModel = default)

Parameters

bufferProto ReadOnlySpan<byte>

buffer containing the content of the .prototxt file

bufferModel ReadOnlySpan<byte>

buffer containing the content of the .caffemodel file

Returns

Net

Remarks

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

ReadNetFromCaffe(string, string?)

Reads a network model stored in Caffe model files.

public static Net? ReadNetFromCaffe(string prototxt, string? caffeModel = null)

Parameters

prototxt string

path to the .prototxt file with text description of the network architecture.

caffeModel string

path to the .caffemodel file with learned network.

Returns

Net

Remarks

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

ReadNetFromDarknet(byte[], byte[]?)

Reads a network model stored in Darknet (https://pjreddie.com/darknet/) model files from memory.

public static Net? ReadNetFromDarknet(byte[] bufferCfg, byte[]? bufferModel = null)

Parameters

bufferCfg byte[]

A buffer contains a content of .cfg file with text description of the network architecture.

bufferModel byte[]

A buffer contains a content of .weights file with learned network.

Returns

Net

Remarks

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

ReadNetFromDarknet(Stream, Stream?)

Reads a network model stored in Darknet (https://pjreddie.com/darknet/) model files from stream.

public static Net? ReadNetFromDarknet(Stream bufferCfg, Stream? bufferModel = null)

Parameters

bufferCfg Stream

A buffer contains a content of .cfg file with text description of the network architecture.

bufferModel Stream

A buffer contains a content of .weights file with learned network.

Returns

Net

Remarks

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

ReadNetFromDarknet(string, string?)

Reads a network model stored in Darknet (https://pjreddie.com/darknet/) model files.

public static Net? ReadNetFromDarknet(string cfgFile, string? darknetModel = null)

Parameters

cfgFile string

path to the .cfg file with text description of the network architecture.

darknetModel string

path to the .weights file with learned network.

Returns

Net

Network object that ready to do forward, throw an exception in failure cases.

Remarks

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

ReadNetFromOnnx(byte[])

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

public static Net? ReadNetFromOnnx(byte[] onnxFileData)

Parameters

onnxFileData byte[]

memory of the first byte of the buffer.

Returns

Net

ReadNetFromOnnx(Stream)

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

public static Net? ReadNetFromOnnx(Stream onnxFileStream)

Parameters

onnxFileStream Stream

memory of the first byte of the buffer.

Returns

Net

ReadNetFromOnnx(ReadOnlySpan<byte>)

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

public static Net? ReadNetFromOnnx(ReadOnlySpan<byte> onnxFileData)

Parameters

onnxFileData ReadOnlySpan<byte>

memory of the first byte of the buffer.

Returns

Net

ReadNetFromOnnx(string)

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

public static Net? ReadNetFromOnnx(string onnxFile)

Parameters

onnxFile string

Returns

Net

ReadNetFromTensorflow(byte[], byte[]?)

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

public static Net? ReadNetFromTensorflow(byte[] bufferModel, byte[]? bufferConfig = null)

Parameters

bufferModel byte[]

buffer containing the content of the pb file

bufferConfig byte[]

buffer containing the content of the pbtxt file (optional)

Returns

Net

Remarks

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

ReadNetFromTensorflow(Stream, Stream?)

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

public static Net? ReadNetFromTensorflow(Stream bufferModel, Stream? bufferConfig = null)

Parameters

bufferModel Stream

buffer containing the content of the pb file

bufferConfig Stream

buffer containing the content of the pbtxt file (optional)

Returns

Net

Remarks

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

ReadNetFromTensorflow(string, string?)

Reads a network model stored in Tensorflow model file.

public static Net? ReadNetFromTensorflow(string model, string? config = null)

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.

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.

ReadNetFromTorch(string, bool)

Reads a network model stored in Torch model file.

public static Net? ReadNetFromTorch(string model, bool isBinary = true)

Parameters

model string
isBinary bool

Returns

Net

Remarks

This is shortcut consisting from createTorchImporter 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

ReadTorchBlob(string, bool)

Loads blob which was serialized as torch.Tensor object of Torch7 framework.

public static Mat ReadTorchBlob(string fileName, bool isBinary = true)

Parameters

fileName string
isBinary bool

Returns

Mat

Remarks

This function has the same limitations as createTorchImporter().

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

ShrinkCaffeModel(string, string, IEnumerable<string>?)

Convert all weights of Caffe network to half precision floating point.

[SuppressMessage("Maintainability", "CA1508: Avoid dead conditional code")]
public static void ShrinkCaffeModel(string src, string dst, IEnumerable<string>? layersTypes = null)

Parameters

src string

Path to origin model from Caffe framework contains single precision floating point weights(usually has .caffemodel extension).

dst string

Path to destination model with updated weights.

layersTypes IEnumerable<string>

Set of layers types which parameters will be converted. By default, converts only Convolutional and Fully-Connected layers' weights.

Remarks

Shrinked model has no origin float32 weights so it can't be used in origin Caffe framework anymore.However the structure of data is taken from NVidia's Caffe fork: https://github.com/NVIDIA/caffe. So the resulting model may be used there.

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.