Skip to content

API Reference

This page contains links to the detailed API references for each module, alongside core wrapper structs.

📦 API Reference by Module

Select a module below to view all its classes, enums, and methods:

NOTE

OpenCV 5 Module Reorganization: If you're coming from OpenCV 4.x, note that the calib3d module has been split into three separate modules in OpenCV 5: Calib (camera calibration), Stereo (stereo correspondence), and Geometry (geometric algorithms). The features2d module has similarly been renamed to Features.


📦 Core Classes & Structs

1. Mat

The primary CPU matrix class, matching C++ cv::Mat. Holds an unmanaged pointer to image data on the CPU heap.

MemberTypeDescription
RowsintThe number of rows (height) of the matrix.
ColsintThe number of columns (width) of the matrix.
Type()intReturns the data type and channel layout as an integer constant (e.g. 0 for CV_8UC1, 16 for CV_8UC3).
Empty()boolReturns true if the matrix holds no data (0 rows or 0 columns).
Dispose()voidReleases the underlying C++ memory heap immediately.

2. CudaGpuMat

The GPU matrix class, matching C++ cv::cuda::GpuMat. Holds an unmanaged pointer to device memory in GPU VRAM.

MemberTypeDescription
RowsintHeight of the matrix in GPU VRAM.
ColsintWidth of the matrix in GPU VRAM.
Upload(Mat cpuMat)voidBlocking Call: Copies data from CPU memory (Mat) to GPU device memory.
Download(Mat cpuMat)voidBlocking Call: Copies data from GPU device memory back to CPU memory.
Dispose()voidDeallocates the device pointer in GPU VRAM immediately.
DefaultAllocator()IntPtrStatic Method: Returns the pointer to OpenCV's global default GPU memory allocator (required when instantiating CudaGpuMat to prevent Access Violations).

🎨 Enumerations & Structs

1. Mat Type Constants

OpenCV uses integer constants for matrix type identifiers. Common values:

ConstantValueDescription
CV_8UC108-bit unsigned integer, single channel (Grayscale)
CV_8UC3168-bit unsigned integer, three channels (BGR)
CV_32FC1532-bit float, single channel (Depth maps)
CV_32FC32132-bit float, three channels

Pass these as raw int values to methods that accept a type parameter.


2. Size

A lightweight C# struct mapping dimensions:

csharp
public struct Size
{
    public int Width;
    public int Height;
}

3. Scalar

A 4-element double vector, mapping color thresholds or background values:

csharp
public struct Scalar
{
    public double V0;
    public double V1;
    public double V2;
    public double V3;
}

🛡️ Error Handling & Interop

1. OpenCVException

When a native OpenCV C++ call fails (e.g., passing invalid dimensions to a filter), the wrapper detects the failure code and calls ErrorHelper.CheckError().

  • The wrapper catches C++ exceptions at the interop boundary, parses them, and throws a managed OpenCVException in C#.
  • This prevents memory leaks and ensures C++ crashes do not kill the C# process.

2. P/Invoke Memory Boundary

mermaid
sequenceDiagram
    participant C# Application
    participant C# Wrapper (OpenCV5Sharp)
    participant C++ Interop (opencv5sharp_native)
    participant OpenCV Engine (opencv_world)

    C# Application->>C# Wrapper: new CudaGpuMat(h, w, type, allocator)
    C# Wrapper->>C++ Interop: cuda_GpuMat_New_0(h, w, type, allocator)
    Note over C++ Interop: Resolves allocator pointer
    C++ Interop->>OpenCV Engine: new cv::cuda::GpuMat(...)
    OpenCV Engine-->>C++ Interop: Returns instance pointer
    C++ Interop-->>C# Wrapper: Returns IntPtr (handle)
    C# Wrapper-->>C# Application: Returns CudaGpuMat instance

Released under the Apache 2.0 and LGPL 2.1 Licenses.