Hi guys, this is the final post in our configuration of Visual Studio with EmguCV. Of course, you should have looked at the first and second part of this tutorial but if you haven’t, you can find them here and here.

Step 5: It’s coding time!

**IMPORTANT ** Avoid problem with cvextern.dll

Even though the configuration found in the post number 2 allows you to run the Example projects, we may have some problems when running our own applications. Therefore, we need to follow the next steps:

  1. Execute the new Solution.This will open the blank Windows Form as we haven’t code anything yet. Once open, close it. This will generate an specific folder we’ll need in the next step.
  2. Copy the file cvextern.dll, found in C:\Emgu\emgucv3.1\bin\x64, and paste it in …\Documents\Visual Studio 2015\Projects\MyFirstProject\MyFirstProject\bin\x64\Debug. This folder would not exist if we hadn’t executed the Application for the first time.
  3. Now we are ready to code!

u_

v_

Add elements to the Windows Form

  1. Go to the left side of the IDE, into the Toolbox Menu and select Image Box. Create a new Image Box in the empty Windows Form.
  2. Now go again to the Toolbox and select a Button. Add this element ino the Windows Form.
  3. Select the Image Box and name it imageCamera.
  4. Select the Button and name it btnStart. Also, set the text to Start.

s_

Code the functionality

  1. Go to the righ side of the IDE, and select the Solution. Press F7 and this will display the actual code of the application.t_
  2. We must declare which classes we are going to use. At the top of the code, add the following lines:

    using Emgu.CV;
    using Emgu.CV.Structure;
    using Emgu.Util;

  3. Then in the actual class, declare the following variables

    private Capture capture;
    private bool captureInProgress;

  4. Create a new private method, that  we are going call ProcessFrame, which is the one that will do the capture and displaying of the image

    private void processFrame(object sender, EventArgs args)
    {

    Mat imageCaptured = capture.QueryFrame();

    frameCamera.Image = imageCaptured;

    }

  5. Implement the functionality to the button. Go to the Windows Form tab, double click on the button, and implement the following code in the callback function:

    private void btnStart_Click(object sender, EventArgs e)
    {

    if (capture == null)
    {

    try
    {

    capture = new Capture();

    }
    catch(NullReferenceException excpt)
    {

    MessageBox.Show(excpt.Message);

    }

    }
    if (capture != null)
    {

    if (captureInProgress)
    {

    btnStart.Text = “Start”;
    Application.Idle -= processFrame;

    }
    else
    {

    btnStart.Text = “Pause”;
    Application.Idle += processFrame;

    }
    captureInProgress = !captureInProgress;

    }

    }

  6. Now we are ready to test the application! Press the Run button at the top of the IDE and the application should start executing.

Testing the application

Once the Application starts running, a new window will come up. When you press the Start button, the application will look up for a webcam and start displaying the captured image. If you click again the now-called Pause button, the application will stop displaying what the webcam is capturing. You can play with the button starting and stoping the webcam.

x_

Now you know how to setup your Visual Studio environment and start developing your own applications. The oncoming post will teach you how to convert the captured image to grayscale. If you had any problem with this first tutorial, please contact me; any feedback will help me to improve the future posts!

 

Advertisements