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

hi thanks for this tutorial
for me everything work fine but the start botton dont work when i click it to pause the cam the streaming doesnt stop

Mahvish Best tutorials I am creating attendance Solution with face recocgnization in which i have added more than 6 new features in whole system!

excelente, esta estupendo este tutorial

Is it possible to display stream of Multiple camera parallel at once. Also i followed the tutorial its amazingly simple to understand. One glitch which am facing in this tutorial is when i select other camera index from combo box it shows stream from same camera ? Wondering where did i go wrong.

Hey great tutorial!
i have some problems with this, i am trying to get an image from a particular usbwebcam, i've tried to join the level 1 and 2 toturials for my solution. i could get images from my defaul usbcam (incorpored in my computer) but when i change the camara like:

capture = new Capture(1);

it give me the error:

External component has thrown an exception.

any idea to resolve this?

Do Emgu.Cv have a camera Property? to configure you webcam ?

I do have question with this tutorial, i've already tried this tutorial but i cannot see other cameras eventhough i installed one USB Camera and 1 Laptop Camera.. I can only see the Laptop Integrated Camera. .Pls help me

Perfect Tutorial startup...thnks

good keep the good work going !!!

Hi i want to set a path for captured images from the camimage frame.please can you guide me.how to set a path for motion pictures

thank you so much for tutorial... It was really helpful. If possible can u guide me to track finger movements and OCR...



Comment