OpenCV5Sharp 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package OpenCV5Sharp --version 1.0.0
                    
NuGet\Install-Package OpenCV5Sharp -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenCV5Sharp" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenCV5Sharp" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="OpenCV5Sharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenCV5Sharp --version 1.0.0
                    
#r "nuget: OpenCV5Sharp, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenCV5Sharp@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenCV5Sharp&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=OpenCV5Sharp&version=1.0.0
                    
Install as a Cake Tool

OpenCV5Sharp Library

Published by Qourex

Clean, automatic C# wrapper for OpenCV 5.0.0 with complete API coverage and robust unmanaged memory management.

Nuget NuGet Downloads License Platform


Installation

dotnet add package OpenCV5Sharp

Or via the NuGet Package Manager Console:

Install-Package OpenCV5Sharp

Key Features

  • OpenCV 5.0.0 Alignment: Matches the restructured OpenCV 5 API layout (Features module, new Stereo/Calib split, and DNN).
  • Auto-Generated Complete API: Over 2,600 methods from core, imgproc, videoio, highgui, features2d, calib3d, objdetect, photo, and dnn modules.
  • Thread-Safe Unmanaged Memory: Uses a specialized, atomic Interlocked.Exchange-based DisposableOpenCVObject base class to prevent double-free conditions and memory leaks.
  • Exceptions Boundary Protection: Zero raw native crashes. Every single C++ entry point is wrapped in standard try/catch handlers that map exceptions back to managed OpenCVException objects.
  • Zero-Configuration Bundling: Prebuilt opencv_world500.dll, opencv_videoio_ffmpeg500_64.dll, and opencv5sharp_native.dll are bundled and copied automatically to the build output.

Supported Platforms

  • Operating System: Windows 10 / 11 (x64)
  • Frameworks: .NET 8.0, .NET 9.0+

Quick Start Example

using System;
using OpenCV5Sharp;

class Program
{
    static void Main()
    {
        // 1. Read input image (BGR color)
        using (Mat src = Cv2.Imread("input.jpg", (int)ImreadModes.Color))
        using (Mat gray = new Mat())
        using (Mat blurred = new Mat())
        using (Mat edges = new Mat())
        {
            if (src.Handle == IntPtr.Zero)
            {
                Console.WriteLine("Error: Could not load image.");
                return;
            }

            // 2. Convert to Grayscale
            Cv2.CvtColor(src, gray, (int)ColorConversionCodes.Bgr2gray, 0, AlgorithmHint.Default);

            // 3. Apply a 5x5 Gaussian Blur
            Cv2.GaussianBlur(gray, blurred, new Size(5, 5), 1.5, 1.5, (int)BorderTypes.Default, AlgorithmHint.Default);

            // 4. Perform Canny Edge Detection
            Cv2.Canny(blurred, edges, 50, 150, 3, false);

            // 5. Write the processed image back to disk
            Cv2.Imwrite("output_edges.jpg", edges, IntPtr.Zero);
        } // All unmanaged memory is automatically freed here
    }
}

Type Conversion Guide

OpenCV C++ Type OpenCV5Sharp Managed Type Marshaling Details
cv::Mat OpenCV5Sharp.Mat Wrapped IntPtr to unmanaged C++ instance
cv::Size_<int> OpenCV5Sharp.Size Sequential layout struct
cv::Point_<int> OpenCV5Sharp.Point Sequential layout struct
cv::Rect_<int> OpenCV5Sharp.Rect Sequential layout struct
cv::Scalar_<double> OpenCV5Sharp.Scalar Sequential layout struct
cv::Range OpenCV5Sharp.Range Sequential layout struct
cv::TermCriteria OpenCV5Sharp.TermCriteria Sequential layout struct
cv::String / std::string System.String UTF-8 marshaled string

Error Handling

All C++ exceptions (including OpenCV's native cv::Exception) are caught on the native side and converted into thread-local error states. When a wrapper call fails:

  1. The native function returns a failure status.
  2. The managed side detects the failure and fetches the error details.
  3. An OpenCVException is thrown containing the native stack trace and error code.

Example:

try
{
    using (Mat src = new Mat()) // Empty image
    using (Mat dst = new Mat())
        {
            Cv2.CvtColor(src, dst, (int)ColorConversionCodes.Bgr2gray, 0, AlgorithmHint.Default);
        }
}
catch (OpenCVException ex)
{
    Console.WriteLine($"OpenCV Error: {ex.Message} (Code: {ex.ErrorCode})");
}

Memory Ownership and Thread Safety

  1. Explicit Disposal: Any object inheriting from DisposableOpenCVObject (e.g. Mat, VideoCapture, CLAHE) allocates unmanaged resources. You must dispose them when done, preferably using C# using blocks.
  2. Double-Free Prevention: Calling Dispose() multiple times, or having Dispose() race with the Garbage Collector Finalizer, is thread-safe and handled atomically.
  3. Thread Safety: P/Invoke calls are thread-safe and re-entrant. However, mutating a single Mat instance from multiple threads simultaneously is not synchronized by the library; you must manage instance-level synchronization in user code.

Support & Contact


License

  • Library Code: Licensed under the Apache License, Version 2.0.
  • FFmpeg Integration: Dynamically links to FFmpeg binaries which are licensed under the GNU LGPL v2.1 or later.
  • Trademark Notice: "OpenCV" is a registered trademark of the OpenCV Foundation. This project is a separate community wrapper and is not endorsed by or affiliated with the OpenCV Foundation.

<p align="center">Built with ❤️ by <a href="https://qourex.com">Qourex</a></p>

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on OpenCV5Sharp:

Package Downloads
OpenCV5Sharp.Mobile

Mobile native runtimes (Android arm64, iOS arm64) for OpenCV5Sharp.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.10 52 7/2/2026
1.0.9 53 7/2/2026
1.0.8 45 7/1/2026
1.0.7 46 7/1/2026
1.0.6 56 7/1/2026
1.0.3 99 6/22/2026
1.0.2 98 6/22/2026
1.0.1 102 6/22/2026
1.0.0 98 6/22/2026

See CHANGELOG.md for release notes.