Creating First Emgu CV Project

In the last post we read about Starting with Emgu CV, now here we will start our first Emgu CV project. Emgu CV is not so difficult all we have to do is to add certain references and make use of Emgu Controls. Lets get started.

Lets start creating a blank windows form application.

My First Emgu CV Project

You will get a new project created for you, before starting further enable “show all settings” under tools, this enables a lots of features like form designer layout, snap to grid and many.

tools visual studio

Options page visual studio









Now lets start, first thing we will do is Add References, browse for the Emgu bin folder (by default it is located at C:Emguemgucv-windows-x86 ), in the bin folder there must be some dlls add all those starting with “Emgu.CV” (choose only one among Emgu.CV.DebuggerVisualizers.VS2008.dll and Emgu.CV.DebuggerVisualizers.VS2010.dll depending on the visual studio you are using, in my case it is Emgu.CV.DebuggerVisualizers.VS2010.dll)

Add Reference

Emgu CV Reference DLL

 Now we need to add some existing items, Goto Project>Add Existing Items and now again browse for bin directory of Emgu CV and this time choose all the dll files starting with “opencv_“, these dll files are required for each time the output is generated via Emgu that is why we added them to our project directory, we will also change there property so that they get copied always to the output folder. So, select all the dll added and select properties and change the “Copy to Output Directory” to “Copy Always“.

Add existing items emgu cv












We already have added the Emgu custom controls to our toolbox, now lets design our form, we will be using two ImageBox (Emgu Control), one Button and a Textbox, design the form as below.

Emgu CV Form Design


Now come to the form1.cs code view, and add the following namespaces;

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;

Next create some member variables :

Capture capturecam = null; //instance for capture using webcam
bool CapturingProcess = false; //boolean stating the capturing process status
Image<Bgr, Byte> imgOrg; //image type RGB (or Bgr as we say in Open CV)
Image<Gray, Byte> imgProc; //processed image will be grayscale so a gray image

Now its time to add a form load event, we will start capturing via webcam under it.

capturecam = new Capture();

this will associate the default webcam with capturecam object.

code snippet















We have added the associated to capture object in try catch block in order to avoid the error if the webcam is already in use.

We added a event handler to Application.Idle, so that it performs task when idle, and the task is to get the next frame. ProcessFunction is called at every idle state of application.

QueryFrame gets the next frame from the webcam, if it is null it means there is some problem and hence we stop our app there.

InRange function takes two parameter min range and max range of Bgr.

SmoothGaussian applies the Gaussian smoothing with the x,y length = 9, we can pass different parameters for x and y also.

Now lets come to the coding part of playorpause button:

playorpause code










this is the simplest part, it checks the boolean value of CapturingProcess and accordingly changes the button text and stops streaming from webcam.

Sample Output: 

emgu cv output


Starting with Emgu CV

What is Emgu CV ?

Before learning what is Emgu CV, one should know what is Open CV.

Open CV

It stands for Open Source Computer Vision, it was designed especially for computational efficiency with strong focus on real time applications.  It is written in optimized C/C++, and can take advantage of multi-core processing. In Image processing it has been a great boon for the developers.

Emgu CV

Its is essentially a huge library of “wrapper” functions that allows calling OpenCV funtions from Visual Studio Windows Form Application. It is necessary because Visual Studio/.NET is an “interpreted” environment that cannot directly call functions written in native C/C++.

In this tutorial we will start configuring our Visual Studio and environment for developing applications using EmguCV.

Firstly we need to download some the essential tools, we will be using the most stable versions though the new versions are also available.

  1. Visual Studio 2010 Express Edition
  2. Emgu CV (version 2.4.2 is available but had various issues so I’m doing it with the most stable version found 2.3.0)

Once you have installed all this it should work fine, but one common exception found is

The type initializer for ‘Emgu.CV.CvInvoke’ threw an exception.

If you see this exception, do following :

  • For Version 2.4+, the bundled OpenCV binary is build with Visual Studio 2010, you will needs to installed MSVCRT 10.0 SP1 x86 or MSVCRT 10.0 SP1 x64 to resolve the dependency issue.
  • For Version 2.0+, the bundled OpenCV binary is build with Visual Studio 2008, you will needs to installed MSVCRT 9.0 SP1 to resolve the dependency issue.
  • For Version 1.5, the bundled OpenCV pre1.1 binary is build with Visual Studio 2005, you will needs to installed MSVCRT 8.0 SP1 to resolve the dependency issue.

This is the most effective solution to the problem for the above exception, for others and detailed troubleshooting click here.

Now start the configuration part :

  1. We need to add a environment variable to the system variables.
    Right Click My Computer>Advanced System Settings>Environment Variables
    Under “System Variables” edit variable “path”
    add a semicolon(;) and the path to the emgu installation directory bin folder(by default it is C:Emguemgucv-windows-x86
    system variables
  2. Now the environment is ready and we need to add the emgu custom controls to Visual Studio ToolBox
    In the toolbox, right click on General tab and select “choose items”
    Visual Studio ToolBox
  3. Select browse on bottom right corner and browse for “Emgu.CV.UI.dll” (it is located in bin folder of installation directory i.e C:Emguemgucv-windows-x86
  4. Now the Visual Studio will have the tools for emgu control
    Emgu Controls

 This is all required for setting up the environment for working with Emgu CV, in the next article we will learn how to create our first project with Emgu CV. Cheers :)