Emgu Integration with .NET: Integrating Images

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.



About the Author

Tapas Pal

Tapas Pal is a Microsoft Platform technical professional with Tata Consultancy Services, India. He has with seven years of experience, holds Microsoft certifications in .NET 1.1 and .NET 2.0.

Related Articles

Comments

  • Emgu CV Library

    Posted by proloy on 07/22/2017 07:06am

    Is there any feature available for this library to authenticate on the basis of the face?

    Reply
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