Creating First Emgu CV Project

Posted by in .NET Framework category on for Beginner level | Points: 250 | Views : 4593 red flag
Rating: 5 out of 5  
 2 vote(s)

This is the first project with emgu cv, it streams the webcam and shows in a imagebox and also processes the image and shows in another imagebox


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 at all. 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.

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

Now lets 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\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)

 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“.

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.


Now coming 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.

private void Form1_Load(object sender, EventArgs e)
                capturecam = new Capture();
            catch (NullReferenceException exception)
            Application.Idle += new EventHandler(ProcessFunction);
            CapturingProcess = true;
        void ProcessFunction(object sender, EventArgs e)
            imgOrg = capturecam.QueryFrame();
            if (imgOrg == null) return;
            imgProc = imgOrg.InRange(new Bgr(50, 50, 50), new Bgr(255, 255, 255));
            imgProc = imgProc.SmoothGaussian(9);
            original.Image = imgOrg;
            processed.Image = imgProc;

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: 

private void playorpause_Click(object sender, EventArgs e)
            if (CapturingProcess == true)
                Application.Idle -= ProcessFunction;
                CapturingProcess = false;
                playorpause.Text = "Play";
                Application.Idle += ProcessFunction;
                CapturingProcess = true;
                playorpause.Text = "Pause";

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: 

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Shubham Saxena
Member Level: Starter
Member Status: Member
Member Since: 12/26/2012 9:19:34 AM
Country: India
Shubham Saxena MSP, MVP - Mindcracker Founder - Developer Innovation Escalator
I started my interest in IT sector from class 11th and from then I never stopped, cover page designing was my first step towards it. Then I started my organization for PHP web development. After entering my college life, I was elected as Microsoft Student Partner which was a great turning point in my life, now I'm also a Most Valuavle Professional (MVP) for Mindcracker Network. I also write blog. Application development and web development can be counted as my hobby, my organization now deals in technical trainings also.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)