Skip to content

shimat/opencvsharp

Repository files navigation

opencvsharp

Github Actions Windows Status Github Actions Ubuntu 22.04 Status Github Actions Ubuntu 24.04 Status GitHub license

OpenCvSharp is a cross-platform .NET wrapper for OpenCV, providing a rich set of image processing and computer vision functionality. It supports .NET Framework 4.8, .NET 8 and later, and .NET Standard 2.0.

Quick Start

Windows

dotnet add package OpenCvSharp4.Windows

Linux / Ubuntu

dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.official.runtime.ubuntu.24.04-x64

For more installation options, see the Installation section below.

Features

  • OpenCvSharp is modeled on the native OpenCV C/C++ API style as much as possible.
  • Many classes of OpenCvSharp implement IDisposable. Unsafe resources are managed automatically.
  • OpenCvSharp does not force object-oriented programming style on you. You can also call native-style OpenCV functions.
  • OpenCvSharp provides functions for converting from Mat to Bitmap (GDI+) or WriteableBitmap (WPF).

Target OpenCV

Requirements

PS1> Install-WindowsFeature Server-Media-Foundation

OpenCvSharp won't work on Unity and Xamarin platforms. For Unity, please consider using OpenCV for Unity or some other solutions.

OpenCvSharp does not support CUDA. If you want to use CUDA features, you need to customize the native bindings yourself.

Installation

Windows (except UWP)

Add OpenCvSharp4 and OpenCvSharp4.runtime.win NuGet packages to your project. Alternatively, you can use the OpenCvSharp4.Windows all-in-one package.

UWP

Add OpenCvSharp4 and OpenCvSharp4.runtime.uwp NuGet packages to your project. Note that OpenCvSharp4.runtime.win and OpenCvSharp4.Windows don't work for UWP.

Ubuntu 22.04

Add OpenCvSharp4 and OpenCvSharp4.official.runtime.ubuntu.22.04-x64 NuGet packages to your project.

dotnet new console -n ConsoleApp01
cd ConsoleApp01
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.official.runtime.ubuntu.22.04-x64
# -- edit Program.cs --- # 
dotnet run

Ubuntu 24.04

Add OpenCvSharp4 and OpenCvSharp4.official.runtime.ubuntu.24.04-x64 NuGet packages to your project.

dotnet new console -n ConsoleApp01
cd ConsoleApp01
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.official.runtime.ubuntu.24.04-x64
# -- edit Program.cs --- # 
dotnet run

Other Linux distributions

Add OpenCvSharp4 and OpenCvSharp4.official.runtime.linux-x64 NuGet packages to your project. This package is built on Ubuntu 24.04 and may work on other recent Linux distributions.

dotnet new console -n ConsoleApp01
cd ConsoleApp01
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.official.runtime.linux-x64
# -- edit Program.cs --- # 
dotnet run

Usage

For more details, refer to the samples and Wiki pages.

Always remember to release Mat and other IDisposable resources using the using syntax:

// C# 8
// Edge detection by Canny algorithm
using OpenCvSharp;

class Program 
{
    static void Main() 
    {
        using var src = new Mat("lenna.png", ImreadModes.Grayscale);
        using var dst = new Mat();
        
        Cv2.Canny(src, dst, 50, 200);
        using (new Window("src image", src)) 
        using (new Window("dst image", dst)) 
        {
            Cv2.WaitKey();
        }
    }
}
Advanced: Using ResourcesTracker for automatic resource management

As mentioned above, objects of classes such as Mat and MatExpr have unmanaged resources and need to be manually released by calling the Dispose() method. Additionally, the +, -, *, and other operators create new objects each time, and these objects need to be disposed to prevent memory leaks. Despite having the using syntax, the code can still look verbose.

Therefore, a ResourcesTracker class is provided. The ResourcesTracker implements the IDisposable interface, and when the Dispose() method is called, all resources tracked by the ResourcesTracker are disposed. The T() method of ResourcesTracker can track an object or an array of objects, and the NewMat() method is equivalent to T(new Mat(...)). All objects that need to be released can be wrapped with T(). For example: t.T(255 - t.T(picMat * 0.8)). Example code is as follows:

using (var t = new ResourcesTracker())
{
    Mat mat1 = t.NewMat(new Size(100, 100), MatType.CV_8UC3, new Scalar(0));
    Mat mat3 = t.T(255-t.T(mat1*0.8));
    Mat[] mats1 = t.T(mat3.Split());
    Mat mat4 = t.NewMat();
    Cv2.Merge(new Mat[] { mats1[0], mats1[1], mats1[2] }, mat4);
}

using (var t = new ResourcesTracker())
{
    var src = t.T(new Mat(@"lenna.png", ImreadModes.Grayscale));
    var dst = t.NewMat();
    Cv2.Canny(src, dst, 50, 200);
    var blurredDst = t.T(dst.Blur(new Size(3, 3)));
    t.T(new Window("src image", src));
    t.T(new Window("dst image", blurredDst));
    Cv2.WaitKey();
}      

Code samples

https://github.com/shimat/opencvsharp_samples/

API Documents

http://shimat.github.io/opencvsharp/api/OpenCvSharp.html

NuGet

Managed libraries

Package Description Link
OpenCvSharp4 OpenCvSharp core libraries NuGet version
OpenCvSharp4.Extensions GDI+ Extensions NuGet version
OpenCvSharp4.WpfExtensions WPF Extensions NuGet version
OpenCvSharp4.Windows All-in-one package for Windows (except UWP) NuGet version

Native bindings

Package Description Link
OpenCvSharp4.runtime.win Native bindings for Windows x64/x86 (except UWP) NuGet version
OpenCvSharp4.runtime.uwp Native bindings for UWP (Universal Windows Platform) x64/x86/ARM NuGet version
OpenCvSharp4.official.runtime.linux-x64 Native bindings for Linux x64 (built on Ubuntu 24.04) NuGet version
OpenCvSharp4.official.runtime.ubuntu.22.04-x64 Native bindings for Ubuntu 22.04 x64 NuGet version
OpenCvSharp4.official.runtime.ubuntu.24.04-x64 Native bindings for Ubuntu 24.04 x64 NuGet version
OpenCvSharp4.runtime.linux-arm Native bindings for Linux Arm NuGet version
OpenCvSharp4.runtime.wasm Native bindings for WebAssembly NuGet version

Native binding (OpenCvSharpExtern.dll / libOpenCvSharpExtern.so) is required for OpenCvSharp to work. To use OpenCvSharp, you should add both OpenCvSharp4 and OpenCvSharp4.runtime.* packages to your project. Currently, native bindings for Windows, UWP, Ubuntu, Linux ARM, and WebAssembly are available.

Packages named OpenCvSharp3-* and OpenCvSharp-* are deprecated.

OpenCvSharp3-AnyCPU / OpenCvSharp3-WithoutDll / OpenCvSharp-AnyCPU / OpenCvSharp-WithoutDll

Downloads

If you are not using NuGet, you can download the DLL files from the release page.

Docker images

https://github.com/shimat?tab=packages

OpenCvSharp Build Instructions

Windows

  • Install Visual Studio 2022 or later
    • VC++ features are required.
  • Run download_opencv_windows.ps1 to download OpenCV libs and headers from https://github.com/shimat/opencv_files. Those lib files are precompiled by the owner of OpenCvSharp using GitHub Actions.
.\download_opencv_windows.ps1
  • Build OpenCvSharp
    • Open OpenCvSharp.sln and build

How to customize OpenCV binaries yourself

If you want to use OpenCV features that are not included by default in OpenCvSharp (e.g., GPU support), you will need to build OpenCV yourself. The binary files of OpenCV for OpenCvSharp for Windows are created in the opencv_files repository. See the README for details.

  • git clone --recursive https://github.com/shimat/opencv_files
  • Edit build_windows.ps1 or build_uwp.ps1 to customize the CMake parameters
  • Run the PowerShell script

Ubuntu

git clone https://github.com/shimat/opencvsharp.git
cd opencvsharp
git fetch --all --tags --prune && git checkout ${OPENCVSHARP_VERSION}
  • Build native wrapper OpenCvSharpExtern
cd opencvsharp/src
mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=${YOUR_OPENCV_INSTALL_PATH} ..
make -j 
make install

You should add a reference to opencvsharp/src/build/OpenCvSharpExtern/libOpenCvSharpExtern.so

export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/home/shimat/opencvsharp/src/build/OpenCvSharpExtern"
  • Add OpenCvSharp4 NuGet package to your project
dotnet new console -n ConsoleApp01
cd ConsoleApp01
dotnet add package OpenCvSharp4
# -- edit Program.cs --- # 
dotnet run

Donations

If you find the OpenCvSharp library useful and would like to show your gratitude by donating, here are some donation options. Thank you.

https://github.com/sponsors/shimat