Tapping into OpenCV to Process Images

Introduction to OpenCvSharp

OpenCvSharp is a cross-platform .NET wrapper of the OpenCV library used for image processing and computer vision algorithms from languages such as C#, VB.NET, and others. OpenCvSharp works on the Windows and Linux (using Mono) platforms. It's modeled on the native OpenCV C/C++ API style; that's why it doesn't force developers to write object-oriented code and allows you to make native-style OpenCV function calls. Comparing OpenCvSharp with EmguCV, I believe OpenCvSharp is the simpler and better supported platform.

Installing OpenCvSharp


OpenCvSharp for the Unity platform also can be downloaded and added in your solution.


  1. Install via NuGet Package Manager from Visual Studio editor, if you are adding the OpenCvSharp libraries in a .NET solution.
  2. For a Windows installation, download the OpenCvSharp C# wrapper source code from Github and build it. Add an OpenCvSharp.dll reference in your Visual Studio Project (Project -> Add Reference, Browse, find DLL).
  3. For a Unix installation, follow the steps mentioned in the this URL

Library Documentation

For OpenCvSharp namespace documentation, check this URL.

Sample OpenCvSharp Windows Application

Step 1

Download the OpenCV latest version from this URL and execute the setup file.

Run the setup file as administrator and extract files to the "C:/Program Files" directory. (It will create an OpenCV folder its with version name in the Program Files directory.)

Step 2

Set up Environment Variables. In System Variables, add "C:\Program Files\OpenCV2.3\build\bin\;C:\Program Files\OpenCV2.3\build\x64\vc10\bin\;C:\Program Files\OpenCV2.3\build\x64\vc10\lib;C:\Program Files\OpenCV2.3\build\x64\vc10\staticlib".

Step 3

Open Visual Studio File-> New Project (or with the shortcut CTRL+Shift+N). Select 'Windows Forms Application' and name it 'SampleOpenCvSharpApp', as shown in Figure 1.

Visual Studio New Windows Form Project
Figure 1: Visual Studio New Windows Form Project

Step 4

From References-> Add reference, browse to "C:\Program Files\OpenCV2.3\build\x86\vc10\bin\" and select all .dll files except those starting with OpenCvSharp.

Step 5

Add an OpenCvSharp reference from the NuGet package manager (see Figure 2).

Add OpenCvSharp NuGet package
Figure 2: Add OpenCvSharp NuGet package

Make sure the following assemblies are added in your project after installating the OpenCvSharp package, as shown in Figure 3.

OpenCvSharp References added to project
Figure 3: OpenCvSharp References added to project

Step 6

Add OpenCvSharp namespaces in your code.

Using OpenCvSharp;

Step 7

Now, add the following code in the Main () method and execute the program. An image will be added in the main window.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using OpenCvSharp;

namespace SampleOpenCvSharpApp
   static class Program
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      static void Main()
         Application.Run(new Form1());
         IplImage myimage = new IplImage(128, 128,
            BitDepth.U8, 1);

         for (int x = 0; x < myimage.Width; x++)
            for (int y = 0; y < myimage.Height; y++)
               int offset = y * myimage.WidthStep + x;
               byte value = (byte)(x + y);
               Marshal.WriteByte(myimage.ImageData, offset, value);
         CvWindow window = new CvWindow(myimage);


OpenCvSharp Blob

cvBlob is a library for computer vision to detect connected regions in binary digital images. The dDeveloper needs to add an OpenCvSharpExtern.dll in the bin/Debug or bin/Release directory and add it as a reference to access Blob functions.

The following code snippet shows OpenCvSharp's blob functionality.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;

namespace SampleOpenCvSharpApp
   class BlobSample
      static void Main(string[] args)
         var ImageSource = new Mat(@"\TapasImages\Sample.Jpg");
         Cv2.ImShow("Source", ImageSource);
         var myImage = new Mat(ImageSource.Size(),
         Cv2.CvtColor(ImageSource, myImage,
         Cv2.Threshold(myImage, myImage, thresh: 100, maxval: 255,
            type: ThresholdTypes.Binary);

         var ImageParams = new SimpleBlobDetector.Params

            FilterByArea = false,
            FilterByCircularity = false,
            FilterByConvexity = true,
            FilterByInertia = true,
            FilterByColor = true
         var myBlobDetector =
         var ImagekeyPoints = myBlobDetector.Detect(myImage);

         Console.WriteLine("Image keyPoints: {0}",
         foreach (var keyPoint in ImagekeyPoints)
            Console.WriteLine("X: {0}, Y: {1}", keyPoint.Pt.X,

         var imageWithKeyPoints = new Mat();
            image: myImage,
            keypoints: ImagekeyPoints,
            outImage: imageWithKeyPoints,
            color: Scalar.FromRgb(255, 0, 0),
            flags: DrawMatchesFlags.DrawRichKeypoints);

         Cv2.ImShow("Key Points", imageWithKeyPoints);


The following are a few OpenCvSharp references for the developer community.

About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date