How to use OpenCV with C#

This article will explain how you can get started programming OpenCV with .NET in C#.

The problem is, that OpenCV is built with C/C++ and while comes with python bindings out of the box, it doesn't have any official .NET bindings. That's why there are lots of articles on how to get started using python but not a lot about dotnet.

TLDR;

Install the nugets OpenCvSharp4 and either OpenCvSharp4.runtime.win or OpenCvSharp4.runtime.ubuntu.18.04-x64, depending on your platform or download the example solution here.

Getting started

First you need to setup a new solution and project with the OpenCV Libraries. To access the native OpenCV libraries, you need a managed wrapper. I am using OpenCvSharp4, which you can use with dotnet core and works on both windows and ubuntu and is licensed under the BSD, just like OpenCV itself.

  1. Create a new solution named OpenCvTest and move to that directory.
    dotnet new sln -o OpenCvTest
    cd OpenCvTest
    
  2. Create a new console project also named OpenCvTest and add it to the solution.
    dotnet new console -o OpenCvTest
    dotnet sln add OpenCvTest
    
  3. Add the nuget OpenCvSharp4 to the project.
    dotnet add OpenCvTest package OpenCvSharp4
    
  4. Add the native runtime depending on your platform (either for windows or for ubuntu 18.04) Either OpenCvSharp4.runtime.win for windows
    dotnet add OpenCvTest package OpenCvSharp4.runtime.win
    
    or OpenCvSharp4.runtime.ubuntu.18.04-x64 for Ubuntu 18.04.
    dotnet add OpenCvTest package OpenCvSharp4.runtime.ubuntu.18.04-x64
    
    or both.

Note:
There are alternative .NET wrappers for OpenCV, but a word of caution: While OpenCV is licensed under the BSD, which makes it useable for closed source commercial applications, some wrapper libraries are using a different license. For example EmguCV uses the GPL by default, making it useless for any commercial closed source software.

This creates the inane situation that the library that does all the heavy lifting is free to use, but the wrapper library that only forwards the calls can cost quite a fortune - without any of the original authors even getting any compensation.
So make sure you check the license of the library before you start using it in a project!

Using OpenCV in C#

Now it's time we actually use OpenCV!

  1. We will test the library by creating a really simple application that grayscales images with the following code:

    using System;
    using System.IO;
    using OpenCvSharp;
    
    namespace OpenCvTest
    {
      class Program
      {
        static void Main(string[] args)
        {
          if (args.Length < 1)
            Console.WriteLine("Please specify a filename");
          else
          {
            string inputFileName = args[0];
            string outputFileName = $"{Path.GetFileNameWithoutExtension(inputFileName)}-gray.jpg";
            using (var image = new Mat(inputFileName))
              using (var gray = image.CvtColor(ColorConversionCodes.BGR2GRAY))
                gray.SaveImage(outputFileName);
          }
        }
      }
    }
    

    Let's take a closer look at the important parts:

    First, we import the namespace of the library:

    using OpenCvSharp;
    

    Secondly, we load the image from a file into a matrix:

    using (var image = new Mat(fileName))
    

    Thirdly, we convert the image to grayscale:

    using (var gray = image.CvtColor(ColorConversionCodes.BGR2GRAY))
    

    Finally, we save the grayscaled image back to disk:

    gray.SaveImage(outputFileName);
    
  2. Now we put an example.jpg in our solution directory, for example: example.jpg

  3. After we run the application like so:

    dotnet run --project OpenCvTest\OpenCvTest.csproj example.jpg
    
  4. ...we can find the resulting example-gray.jpg in the same folder: example-gray

Download the Example Solution

You can download a Zip-File with the complete solution here.

Summary

Getting started with OpenCV in C# is easy, thanks to the nice wrapper library OpenCvSharp4.

References