Emgu Integration with .NET: Integrating Images

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

OpenCV is a very popular library written in the native C and C++ programming languages for processing images. Programmers familiar with the C# and Visual Basic.NET languages often face challenges in using OpenCV. Luckily for C# developers, a cross platform .NET wrapper to the OpenCV image processing library is available; it’s named Emgu CV. This wrapper allows C# programs to communicate with native APIs of the underlying library that was written with unmanaged code. Emgu CV functions can be called from languages such as C#, VB, VC++, Python, and so forth. This wrapper can be compiled in Mono and run on Windows, Linux, Mac OS X, iPhone, iPad and Android devices.

Advantages of Using Emgu CV

Emgu CV libraries are cross language compatible and installation comes with example code for developers to reuse. An online discussion forum is also available if developers have any questions related to implementation. Emgu CV is capable of doing edge detection, grayscale conversion, and histogram equalization.

Installing and Adding the Emgu CV Library

Emgu CV could be downloaded and installed from Sourceforge. It’s also available in nuget package manager. At the time this article was written, the latest version of Emgu CV is 3.1, but remember it has an enormous amount of performance issues and bugs. Currently, the most stable release is version 2.4.

Emgu CV DLLs need to be available in your application’s bin directory, the output folder where executables reside. A developer can add the library to an existing .NET application by choosing the References → Add Reference → Emgu CV libraries folder (see Figure 1).

Emgu CV Library DLLs
Figure 1: Emgu CV Library DLLs

These libraries are required process the image in your .NET applications. They are required to be available when your application starts to execute, as shown in Figure 2.

Graph API Profile page URL setup
Figure 2: Graph API Profile page URL setup

Sample Application S/w Requirements

In this tutorial, I will create a sample Windows application to show you how to detect human faces using your computer camera/webcam and the Emgu CV library.

You need following software for this tutorial:

  • Microsoft Visual Studio 2010 or above
  • Emgu CV (OpenCV in .NET) library. You have to download and install this.

Step 1

Emgu CV tutorials have a face detection sample. You can find that face detection application example in following default path: C:\Emgu\emgucv-windesktop 3.2.0.2682\Emgu.CV.Example\FaceDetection. The path may change if you have changed something during installation. You can execute the FaceDetection.exe to check how it detect faces.

Step 2

Next, open Visual Studio and create a new Windows Project. Add Emgu CV references and make sure it’ll look like Figure 3. The references are required for image processing.

Adding the needed Emgu CV references
Figure 3: Adding the needed Emgu CV references

If you are running an x64 system, you will have to download separate DLLs. To change the build platform, right-click your project file in the solution explorer and select “Properties” at the bottom. Select the “Build” tab from the ribbon bar on the right of this window. There will be an option for Platform Target: with a drop-down menu change this from x86 to x64 (see Figure 4).

Changing the drop-down menu
Figure 4: Changing the drop-down menu

Step 3

Add a Windows Forms PictureBox control to display images. Also, and a Timer Control to raise events at a specific interval.

Step 4

Add the following namespaces in your Form.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.CV.Cuda;
using Emgu.Util;

Step 5

Initialize two objects Capture and HaarCascade and add the following code in Form1_Load & timer1_Tick events. Finally, adjust the path of the Haarcascade XML file.

namespace MyFaceDetectionForm
{
   public partial class MyFaceDetectionForm : Form
   {
      private Capture cap;
      private HaarCascade haar;

      public Form1()
      {
         InitializeComponent();
      }

      private void timer1_Tick(object sender, EventArgs e)
      {
         using (Image<Bgr, byte> nextFrame =
            cap.QueryFrame())
         {
            if (nextFrame != null)
            {

               Image<Gray, byte> grayframe =
                  nextFrame.Convert<Gray, byte>();
               var faces = grayframe.DetectHaarCascade(
                  haar, 1.4, 4,
                  HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                  new Size(nextFrame.Width / 8,
                     nextFrame.Height / 8)
               )[0];

               foreach (var face in faces)
               {
                  nextFrame.Draw(face.rect, new Bgr(0,
                     double.MaxValue, 0), 3);
               }
               pictureBox1.Image = nextFrame.ToBitmap();
            }
         }
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         cap = new Capture(0);
         haar = new HaarCascade(
            @"C:\Emgu\emgucv-windesktop 3.2.0.2682\etc
               \haarcascadeshaarcascade_frontalface
               _alt2.xml");
      }
   }

Step 6

Execute the program for face detection.

Summary

I hope you have enjoyed reading this article and that the preceding example has given you an idea of how you can use Emgu CV libraries in your .NET applications to detect human faces. Happy reading and wait for my next post on OpenCV.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read