Camera Capture

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Revision as of 10:39, 20 November 2012 by Chris Johnson (talk | contribs) (Initial Save)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Camera Capture




EMGU Reference
EMGU CV_PROP Reference
OpenCV Reference


Source Code V2.0


The following example shows the use of the Capture function within EMGU. The function of this library is to allow video streaming for web camera type devices and video files. This example will show an example of web camera capture as well as reading and setting properties of the camera. Only some function is available in this version to demonstrate the methods available to the user. A video file example is available here (Pending...).




The code provided should run straight out of the Emgu.Example folder (V2.4.2), extract it to this location. If the code fails to execute re-reference the EMGU libraries and include the required opencv dlls in the bin directory. Note that the project is set to build to the output path "..\..\..\bin\" you may wish to change this if you don't extract to the EMGU.Example folder.

A Web camera is required for this example.

EMGU Coding Level: While the coding is not advanced the rated level for this example is Intermediate/Beginner. This is not designed as a full on tutorial and general knowledge of the EMGU is expected. While the coding is basic the are several methods involved that may be of putting the newcomers of EMGU.

The Code

The code provided in this sample is basic there is only someo error checking. Support is available through the Forums but please try and examine the code before saying it doesn't work for you. The code is not optimised instead is better formatted to provided an understanding of the stages involved.

Each method is split up to do it's own function while there are several methods in the code the code is not advanced and is often repetitive. The new features introduced in this code is the ability to select the camera source through Direct Show (dll supplied). The code shows the use of two functions for acquiring the frame the others are legacy based and will not work see the Methods Available section. This example also shows how to get and set camera features such as Brightness, Contrast, and Sharpness. While other settings are available only these three attributes are changeable from the form interface. It is easy to add additional support for other settings, these will be made available in future iterations of the example.

The Code: Variables

There are only a few variables in the example separated into two regions the first Camera Capture Variables contains the variables for camera information and the capture device. The Video_Device[] array is a custom structure to store video information this is discussed bellow.

        #region Camera Capture Variables
        private Capture _capture = null; //Camera
        private bool _captureInProgress = false; //Variable to track camera state
        int CameraDevice = 0; //Variable to track camera device selected
        Video_Device[] WebCams; //List containing all the camera available

The second region Camera Settings simply stores the original setting of the camera when it is loaded this is useful in case we need to reset the values to the way we had them.

        #region Camera Settings
        int Brightness_Store = 0;
        int Contrast_Store = 0;
        int Sharpness_Store = 0;

The Code: Methods

As discussed there are several method in the code, they will be decribed in the order that they appear in the code.

The method CameraCapture() is the form initialiser here we disable all our sliders on the form with the method call of Slider_Enable(false). We gather all the video devices available on the system using the Direct Show library (Supplied in the 'Lib' Folder). We populate both an array of Video_Devices and the combo box on the form to enable user selection. Finally we set the combo box to the first index '0' which will be the default device of the OS.

        public CameraCapture()
            Slider_Enable(false); //Disable sliders untill capturing

            //-> Find systems cameras with DirectShow.Net dll
            //thanks to carles lloret
            DsDevice[] _SystemCamereas = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            WebCams = new Video_Device[_SystemCamereas.Length];
            for (int i = 0; i < _SystemCamereas.Length; i++)
                WebCams[i] = new Video_Device(i, _SystemCamereas[i].Name, _SystemCamereas[i].ClassID); //fill web cam array
            if (Camera_Selection.Items.Count > 0)
                Camera_Selection.SelectedIndex = 0; //Set the selected device the default
                captureButton.Enabled = true; //Enable the start

Methods Available




  1. Numbered list If any