OpenCVSharp is a .NET wrapper for the OpenCV computer vision library that allows us to use it conveniently from a .NET application on Windows, Linux, macOS, or WASM.
As we know, OpenCV is the most popular library for computer vision applications. It is Open Source under the BSD license, cross-platform, and written in C++. You can find more information on the project page https://opencv.org/ and all the code on Github.
However, there are different wrappers and adaptations that allow us to use OpenCV from other programming languages and integrate them into our applications. This ease of use comes at a cost, in the form of a performance penalty compared to using the C++ libraries directly.
Probably the most well-known wrapper is Opencv-python, a wrapper for Python, due to the abundance of tutorials available for it. However, it is not the only one, nor necessarily the best.
Today we are going to look at OpenCVSharp, which allows us to use OpenCV from a .NET application very easily, both in installation and usage. OpenCVSharp is also an Open Source project, distributed under the Apache 2.0 license, and its code is available at https://github.com/shimat/opencvsharp.
This has the advantage of being able to use a powerful language like C# in our computer vision applications, as well as easily integrating it into our .NET applications. Imagine what this means in terms of hardware access, communication, database writing, file management. Combining OpenCV with “anything” we can do in .NET (i.e., everything).
OpenCVSharp is available on all platforms compatible with .NET, meaning it can run on Windows (WinForms, WPF and UWP), Linux (Ubuntu), macOS, LinuxARM (Raspberry Pi and similar) and WASM (web assembly).
Installation is very simple as it is done through Nuget packages that include everything needed to run OpenCVSharp. These packages include the OpenCV binaries. We only need to download the Nuget package appropriate for our architecture. You can consult the complete list on the project page.
On the other hand, to achieve performance as close as possible to the direct use of the C++ libraries, OpenCVSharp makes intensive use of unmanaged code. This has the consequence that we must be especially careful when doing ‘Dispose’ of created objects to avoid memory leaks, as also explained in the project documentation.
Finally, there is this repository of examples for using OpenCVSharp. It is highly recommended to take a look, because it includes examples of almost all the common tutorials and use cases, including applications with camera capture in both WinForms and WPF.
Example Project in OpenCVSharp
Let’s see how to use OpenCVSharp in a simple project, a “Hello World” of computer vision. For this, we will use the “Lena” image, which for historical reasons, is the most used image in tests and prototypes of computer vision applications.

I recommend downloading a higher quality image from the internet. For the blog, I have to reduce the image quality so they weigh less and don’t penalize traffic.
In our case, we will use Windows and Visual Studio. First, we take our “Lena.jpg” image and copy it to a location, for example “C:\temp”. Next, we create a new solution for a desktop application in WPF Core.
To add OpenCVSharp to our solution, we only need to add the appropriate Nuget package for our architecture. In our case, we only need the OpenCvSharp4.Windows Nuget, which includes everything needed to function, without requiring any additional package or file.

Now, we open our window.cs file, which is the main window of the project we just created, and replace the code with this.
public partial class MainWindow : System.Windows.Window
{
public MainWindow()
{
InitializeComponent();
string imagePath = @"C:\temp\lena.jpg";
using var src = new Mat(imagePath, ImreadModes.Grayscale);
using var dst = new Mat();
Cv2.Canny(src, dst, 50, 200);
using (new OpenCvSharp.Window("src image", src))
using (new OpenCvSharp.Window("dst image", dst))
{
Cv2.WaitKey();
}
}
}
In WPF, the graphical windows are of the class Window. Similarly, the graphical windows in OpenCV are also called Window. For this reason, we had to fully qualify the names, respectively, to System.Window and OpenCVSharp.Window.
If instead of creating a WPF Core desktop application we had created a .NET Core console application, the code would have been the following.
static void Main(string[] args)
{
string imagePath = @"C:\temp\lena.jpg";
Mat src = new Mat(imagePath, ImreadModes.Grayscale);
Mat dst = new Mat();
Cv2.Canny(src, dst, 50, 200);
using (new OpenCvSharp.Window("src image", src))
using (new OpenCvSharp.Window("dst image", dst))
{
Cv2.WaitKey();
}
}
In this example code, we are simply loading the image from its location and loading it into the image (Mat) ‘src’ in grayscale. Next, we apply a Canny filter and save it in a new image ‘dst’.

Now, we run the program and we will see our two images appear, Lena in grayscale, and the result of the Canny filter, which is a simple edge detector (more information about the Canny filter on Wikipedia).

It’s that simple, just by adding a Nuget package, we can use OpenCV in a .NET application on any platform. Once again, I recommend looking at the examples repository, because it is the best way to learn how to use OpenCVSharp.

