Loading repository data…
Loading repository data…
qourex / repository
High-performance, cross-platform .NET wrapper for OpenCV 5 with CUDA and cuDNN GPU acceleration. Supports modern C# (IDisposable-safe) on Windows, Linux, macOS, Android, and iOS.

by Qourex — High-Performance, Cross-Platform Computer Vision for .NET 8.0, 9.0, & 10.0.
📖 Read the Documentation | 🚀 GPU Acceleration Guide | 🏃 C# Runnable Samples
OpenCV5Sharp is a production-ready C# wrapper for OpenCV 5.x. It provides a clean, automatic .NET API mapping of OpenCV's core computer vision algorithms, enabling high-performance image processing, feature detection, object tracking, and deep learning pipelines in modern C# without unmanaged memory leaks.
SafeHandle implementations and IDisposable wrappers that clean up native pointers.To comply with the NuGet.org 250 MB package size limit, OpenCV5Sharp is distributed via modular packages:
| Package | Platform | Focus |
|---|---|---|
OpenCV5Sharp | Desktop (Windows, Linux, macOS) | CPU-only image processing |
OpenCV5Sharp.Mobile | Mobile (Android, iOS) | CPU processing optimized for ARM64 |
OpenCV5Sharp.Gpu.Windows | Windows x64 | GPU / CUDA 12.8 & cuDNN 8.9.7 acceleration |
OpenCV5Sharp.Gpu.Linux | Linux x64 | GPU / CUDA 12.8 & cuDNN 8.9.7 acceleration |
Here is a copy-pasteable example of loading an image, converting it to grayscale, running a Canny filter, and saving the output using C#-idiomatic patterns.
using System;
using OpenCV5Sharp; // Provides classes and ToInt() enum extensions
class Program
{
static void Main()
{
// 1. Load an image from disk
using var src = Cv2.Imread("lena.jpg", ImreadModes.Color.ToInt());
if (src == null || src.Empty())
{
Console.WriteLine("Could not load image.");
return;
}
// 2. Prepare workspace matrices
using var gray = new Mat();
using var edges = new Mat();
// 3. Convert to grayscale and run Canny Filter
Cv2.CvtColor(src, gray, ColorConversionCodes.Bgr2gray.ToInt(), 0, AlgorithmHint.Default);
Cv2.Canny(gray, edges, 50, 150, 3, false);
// 4. Save the output
Cv2.Imwrite("edges.png", edges, IntPtr.Zero);
Console.WriteLine("Edge detection complete! Output saved to edges.png.");
}
}
Because OpenCV5Sharp wraps raw C++ pointers, you must follow the .NET IDisposable pattern to avoid native heap memory leaks:
using blocks: Ensure Mat, CudaGpuMat, VideoCapture, and other classes holding native handles are disposed immediately.using var variables.Displaying a raw unmanaged matrix pixel buffer inside .NET GUI frameworks is simple. Copy row-by-row using strided memory writes:
public void UpdateWpfImage(WriteableBitmap wpfBitmap, Mat frame)
{
if (frame == null || frame.IsDisposed || frame.Data == IntPtr.Zero)
return;
wpfBitmap.Lock();
try
{
int srcStride = (int)frame.Step;
int dstStride = wpfBitmap.BackBufferStride;
int bytesToCopyPerRow = frame.Cols * frame.Channels(); // Assuming 8-bit channels
unsafe
{
byte* srcPtr = (byte*)frame.Data;
byte* dstPtr = (byte*)wpfBitmap.BackBuffer;
int bytesToCopy = Math.Min(bytesToCopyPerRow, dstStride);
for (int y = 0; y < frame.Rows; y++)
{
Buffer.MemoryCopy(srcPtr + (y * srcStride), dstPtr + (y * dstStride), dstStride, bytesToCopy);
}
}
wpfBitmap.AddDirtyRect(new Int32Rect(0, 0, frame.Cols, frame.Rows));
}
finally
{
wpfBitmap.Unlock();
}
}
DllNotFoundExceptionIf you receive a DllNotFoundException when invoking Cv2 methods, check the following checklist:
PATH (Windows) or LD_LIBRARY_PATH (Linux).cudart64_12.dll and cudnn64_8.dll are loadable from command prompt/shell.win-x64, linux-x64, osx-x64, osx-arm64, android-arm64, ios-arm64). Check that your project does not build as x86 or Any CPU with "Prefer 32-bit" enabled.