Language: EN

como-usar-opencv-en-net-con-opencvsharp

How to use OpenCV in .NET with OpenCVSharp

OpenCVSharp is a .NET wrapper for the OpenCV computer vision library that allows us to use it comfortably 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 and multi-platform, and it is written in C++. You can find more information on the project page at 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 of OpenCV for Python, due to the large number of tutorials available. However, it is not the only one, nor necessarily the best.

Today we are going to see OpenCVSharp, which allows us to use OpenCV from a .NET application very easily, both in installation and use. Additionally, OpenCVSharp is 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 that means in terms of hardware access, communication, database writing, and file management. Combining OpenCV with “anything” we can do in .NET (in other words, 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).

The 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 appropriate Nuget package for our architecture. You can find the complete list on the project page.

On the other hand, in order for the performance to be as close as possible to using the C++ libraries directly, OpenCVSharp makes intensive use of unmanaged code. This means that we must be especially careful when using ‘Dispose’ for created objects to avoid memory leaks, as also explained in the project documentation.

Finally, there is this repository of examples of using OpenCVSharp. It is highly recommended to take a look, as it includes examples of almost all common tutorials and use cases, including applications that include camera capture in both WinForms and WPF.

Example Project in OpenCVSharp

Let’s see how to use OpenCVSharp in a simple project, a computer vision “Hello World.” For this, we will use the “Lena” image, which is historically the most used image in computer vision application tests and prototypes.

lena

I recommend downloading the image from the internet in better quality. For the blog, I have to reduce the image quality to reduce the file size and not penalize the 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”. Then, 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 Nuget OpenCvSharp4.Windows, which includes everything needed to work, without the need for any additional packages or files.

csharp-opencvnet-nuget

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 graphic windows are of the Window class. Likewise, the graphic windows of OpenCV are also called Window. For this reason, the respective names have had to be completed, 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 as follows.

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 ‘src’ image (Mat) in grayscale. Then, we apply a Canny filter and save it to a new ‘dst’ image.

lena-grayscale

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

lena-canny

That’s how simple it is. By adding a Nuget package, we can use OpenCV in a .NET application on any platform. I recommend looking at the examples repository again, as it is the best way to learn how to use OpenCVSharp.