header photo

Level 2 - Use Multiple Cameras in one application

October 5, 2011

THE PROBLEM:
Can I connect more than one, like 2 or 3 cameras to my application using EmguCV? How? ”

THE SOLUTION:
Yes you can and there’s nothing to it really, I mean it’s pretty easy!
If you’ve made a camera capture application, then all you need is to give your desired value to the capture object when initializing it. This is what we’re going to be doing today.

LEARNING OBJECTIVES:

    level 2 objectives

    SNAPSHOT OF FINAL OUTPUT:

    NOTE BEFORE YOU BEGIN!

    • This application is an enhancement to the simple camera capture application made in Level 1 tutorial. For details and cautions on getting started, please see level 1.

    • This application has been checked for Visual Studio 2008 and 2010 and works perfectly in both. So use the one you like.

     
    ====================================
    START TUTORIAL!

    OK, so hope you have a working simple camera capture application made. Get ready to modify it now because we move on to adding a new feature to it: trying to operate more than one camera with Emgu CV …in a Windows Form using C# of course ^_~   

    I won’t be changing the code much, just a few changes will be made, and those I will tell as we proceed through the steps.

    STEP 1:

    Now we want something that could control the camera number selection, right? One easiest and safest way is to use a ComboBox control. A ComboBox allows you to select an item from a predefined set of items in a “drop down menu” way. So we’ll start by adding this functionality.

    As shown in figure.2.1, to your windows form (which is CameraCapture.cs here) add following from the toolbox:
    i.) A Label whose Text property is set to ‘Select Camera:’

    ii.) A ComboBox control. Set its following properties as below:
    Name = cbCamIndex
    Text = NONE
    Items =
    0
    1
    (NOTE: these items are actually the camera options we had wanted. It shall specify the Camera Index number, e.g. camera 0, camera 1 etc, you can add more indices depending on your requirements.)

    Fig.2.1

    STEP 2:
    I prefer that when the user selects a camera option from the ComboBox, automatically the application should start that camera and show a stream of captured frames into the imagebox.

    For this, we have to generate an event that occurs when the comboBox index is changed. Fortunately a ComboBox has this EXACT event property! We will use that.

    On that note, pardon me for the off-track talk, but now you know why my tutorials focus on .NET and C# ; because things in it are so ready-made, easy to use AND visual! :D Anyhow, back to business…

    As shown in figure 2.2 below,
    Select cbCamIndex .
    Go to its properties.
    Select its ‘Events’ by clicking the thunder bolt icon.
    Then scroll down to the SelectedIndexChanged event and Double click it to generate its event handler (function) just like you generate a button click event handler by double clicking a button.

    Fig2.2

    Now you must be in the code view and more precisely at the
    private
    void cbCamIndex_SelectedIndexChanged( object sender, EventArgs e)
    {

    }

    Now in this function, add the following code:

                //Set the camera number to the one selected via combo box
                int CamNumber = 0;
                CamNumber = int.Parse(cbCamIndex.Text);
    
                //Start the selected camera
                #region if capture is not created, create it now
                if (capture == null)
                {
                    try
                    {
                        capture = new Capture(CamNumber);
                    }
                    catch (NullReferenceException excpt)
                    {
                        MessageBox.Show(excpt.Message);
                    }
                }
                #endregion
    
                //Start showing the stream from camera
                btnStart_Click(sender, e);            btnStart.Enabled = true;   //enable the button for pause/resume
    

    NOTE: To handle exception, its better to have the button disabled at application start-up. once the camera is selected and turned on, then we should allow the pause or resume, otherwise there will be unhandled exception, say, no camera was selected but we tried to show frames in imageBox--ERROR!

    STEP 3:
    Now that the option thing has been taken care of, lets add code to our start button.
    We want our button(btnStart here) to pause the stream from camera or to resume it depending on the streaming condition when clicked.

    One logic could be to change the text of the button at click and use it as a check to pause or resume the streaming.
    Lets say,  

    BtnStart CLICKED
    if streaming is ON
         then
    button shows "Pause"
    else button show "Resume".

    This implies:
    BtnStart CLICKED
    if 
    btnStart.Text == "Resume"
         then turn stream on because it is paused  AND Then  set btnStart.Text= "Pause"
    else pause stream because it is on AND Then  set btnStart.Text= "Resume"

    This is exactly what i have implemented. there could be better ways, use one if you have it :)

    Now go Start button's click event code.  there paste the following:

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

     

    NOTE: if you are adding code to level 1's application we had made,  then REPLACE all your existing code behind btnStart with the one given above.

    TIP:

    To ensure that you can see the whole frame captured by the camera, make sure your Imagebox’s SizeMode property has been set to StretchImage

    and that's about it! happy coding!
    ===========================================================================
    SOURCE CODE  (Visual Studio 2010 files) 

    -------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    IF YOU LIKE THIS TUTORIAL. PLEASE LEAVE A COMMENT BELOW :)

    BUT ASK YOUR QUESTIONS ONLY AT THE FOLLOWING LINK. QUESTIONS IN COMMENTS WILL BE IGNORED.


    SEE YOU IN THE NEXT TUTORIAL :)
    **********************
    Tutorial by: Mahvish


    

Go Back

any begginers level tutorials?? i mean m in nust seecs 1st sem studying c++ nw we hav to do sem projct of programming any tutorials of file i/o or database handling fr programs like banking system or motorway system in console will b appreciated..

How do you run multiple cameras at the same time in EMGU?

Hello,

Thank you so much for this tutoriel, really it's very greatful !!

a very nice tutorial =) keep it up sir x)

Perfect Tutorial startup...thnks

can I open and display two cameras at the same time using C#.net. Is that possible?



Comment