Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / OpenCV

Creating First Emgu CV Project

5.00/5 (3 votes)
15 Jan 2013CPOL2 min read 28.5K  
About how to create your 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. Let's get started.

Let's start creating a blank Windows Form application.

My First Emgu CV Project

You will get a new project created for you. Before going 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 let's start, first thing we will do is Add References, browse for the Emgu bin folder (by default, it is located at C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin ). 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 the property so that they get copied always to the output folder. So, select all the DLLs 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 let's design our form, we will be using two ImageBoxes (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:

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

Next, create some member variables:

C#
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 it's time to add a form load event, we will start capturing via webcam under it.

C#
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 an 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 parameters 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, let's 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)