Thursday, May 9, 2013

How to use Emgu CV with Visual Studio 2010 C# Application

What is Emgu CV?


Emgu CV is a cross platform .Net wrapper to the OpenCV image processing library.
You can find further details about Emgu CV here.


How to install Emgu CV to your machine?


First you need to download Emgu CV.
We are using Emgu CV 2.4.9 version

Once the setup is downloaded, you need to run and install Emgu CV.
If it is correctly installed you will get a folder name "Emgu" in your C Drive.

Next you need to install Microsoft Visual C Run-Time Library (MSVCRT) to avoid platform dependency issues. You will need to install MSVCRT 10.0 SP1 x86 or MSVCRT 10.0 SP1 x64 to resolve the dependency issue.

For more help on download and installation click here.


Configure VS project to test installation


Create a new C# windows form application. And name it "TestEmguCV".

Go to Build => Configuration Manager.
Select "Debug" in the Active solution configuration drop down list.
Select "x86" in Active solution platform drop down list.
Close configuration manager window.






Next go to Project => TestEmguCV Properties.
Select Build Property.
Change Target Platform to "x86".
Save changes.



Now open the bin folder of your Emgu folder in the following path.
C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin

Next you have to copy the following DLL files to your project's debug folder.

  • Emgu.CV.dll
  • Emgu.CV.UI.dll
  • Emgu.Util.dll
  • ZedGraph.dll

Then you have to open the x86 folder inside bin folder
C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin\x86

Again you have to copy the following DLL files to your project's debug folder.

  • cudart32_50_35.dll
  • cvextern.dll
  • npp32_50_35.dll
  • opencv_calib3d249.dll
  • opencv_contrib249.dll
  • opencv_core249.dll
  • opencv_features2d249.dll
  • opencv_flann249.dll
  • opencv_gpu249.dll
  • opencv_highgui249.dll
  • opencv_imgproc249.dll
  • opencv_legacy249.dll
  • opencv_ml249.dll
  • opencv_nonfree249.dll
  • opencv_objdetect249.dll
  • opencv_video249.dll
After copying these DLLs, the debug folder should look like this

Ok now go to your project and select Project => Add Reference
Select the browser tab.
Add the following DLLs as references. (You can find the inside bin\debug folder)
  • Emgu.CV.dll
  • Emgu.CV.UI.dll
  • Emgu.Util.dll

Add Emgu CV Tools to Visual Studio


We will be using Emgu CV Tools throughout the project, so here's the official guide to install those tools.

Once you have added the tools to the VS tool box, add an ImageBox to the form and resize it.


Add a button to the form and do some modifications to the properties as below:

ImageBox Properties:
(Name): CamImageBox
BorderStyle: FixedSingle

Button Properties:
(Name):  btnStart
Text: Start

Form Properties
(Name): CameraCapture
Text: Camera Output


Go to the code view of your form and add the following Emgu CV references.
           using Emgu.CV;
           using Emgu.CV.Structure;
           using Emgu.Util;

Inside the class declare two global variables
           private Capture capture;        //takes images from camera as image frames
           private bool captureInProgress; // checks if capture is executing


Next add a user defined function.
          private void ProcessFrame(object sender, EventArgs arg)
         {
                           /*create an EmguCv type Image called ImageFrame and capture a frame from camera and save it in ImageFrame. Load this into CamImageBox to show it to the user*/
                         Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
                        CamImageBox.Image = ImageFrame;
         }

Double click on the button and add this code for button onClick event
            #region if capture is not created, create it now
            if (capture == null)
            {
                try
                {
                    capture = new Capture();
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }
            #endregion

            if (capture != null)
            {
                if (captureInProgress)
                {  //if camera is getting frames then stop the capture and set button Text
                    // "Start" for resuming capture
                    btnStart.Text = "Start"; //
                    Application.Idle -= ProcessFrame;
                }
                else
                {
                    //if camera is NOT getting frames then start the capture and set button
                    // Text to "Stop" for pausing capture
                    btnStart.Text = "Stop";
                    Application.Idle += ProcessFrame;
                }

                captureInProgress = !captureInProgress;
            }

Finally add this method, which closes the aplication in a safe way. 
        private void ReleaseData()
        {
            if (capture != null)
                capture.Dispose();
        }

Now, your form should look like this.




Debug and Run your application
Output will be like this.



If you get "Type initializer for Emgu.CV.Invoke threw an exception" please follow these steps.
Make sure you followed all the steps correctly. 
Stick to x86 DLLs because it will not cause you much trouble.

You  can get the source code from here. You will have to add the DLLs.

You can also see Emgu CV examples available in your installation folder
C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\Emgu.CV.Example

If you have any questions, contact me via email.
wpiyumanthi@gmail.com

This is just a simple app to check how Emgu CV can be used with Visual Studio
Hoping to continue more posts on other Emgu CV functions.

Best Regards,
Piyumanthi Wickramarathna

Notes:
This post was based on the tutorial available at http://fewtutorials.bravesites.com/tutorials




5 comments:

  1. hi there...

    any idea how to save emgu.cv.ui.imagebox capture image from webcam save to access database...

    ReplyDelete
  2. akke publish karama error akak anawane... project ake weda pubilis karama error akak anawa

    ReplyDelete
  3. in line Image ImageFrame = capture.QueryFrame();
    i get "cannot implicitly convert type "Emgu.CV.Mat" to "Emgu.CV.Image<...>"
    I have tried everything, but I still can't find what am I doing wrong.

    ReplyDelete
  4. in line Image ImageFrame = capture.QueryFrame();
    i get "cannot implicitly convert type "Emgu.CV.Mat" to "Emgu.CV.Image<...>"
    I have tried everything, but I still can't find what am I doing wrong.

    ReplyDelete