Search code examples
c#webcamemgucv

Web Camera Capture


As the question here States, I am trying to capture an image with my web-camera. I am running Windows 7 (bootcamp) and have a camera connected as seen in the following screen capture.

Screen shot showing devices

But for some reason I can't seem to get the following code to work (it's from the link given in the other question):

Capture capture = new Capture(CaptureType.ANY); //fezzes on this line
viewer.Image = capture.QueryFrame();

It dies with out telling me anything about what happened (after I force close the window)


Solution

  • Your code looks questionable take a look at the Cameracapture example provided with EMGU I do not now where you get CaptureType.ANY from but your code should be

    Capture capture = new Capture(); 
    viewer.Image = capture.QueryFrame();
    

    While the link is valid it's old and outdated (2009).

    Here is the slightly edited code from the example that you will need:

    private Capture _capture;
    
    public YOUR_PROJECT()
    {
        InitializeComponent();
        try
        {
            _capture = new Capture();
        }
        catch (NullReferenceException excpt)
        {
            MessageBox.Show(excpt.Message);
        }
    
        if (_capture != null)
        {
            Application.Idle += ProcessFrame;
        }
    }
    
    
    private void ProcessFrame(object sender, EventArgs arg)
    {
        Image<Bgr, Byte> frame = _capture.QueryFrame();
        Picturebox1.Image = frame.ToBitmap(); //Display image in standard Picture Box
    }
    

    You only need to add a variable to the Capture capture = new Capture() method call if you have more than one camera or if your reading from a video file

    //For example if I had two video Cameras
    Capture capture = new Capture(0); //CAM1
    Capture capture = new Capture(1); //CAM2
    
    //For a Video File
    Capture capture = new Capture(@"C:/..../Myvideofile.avi"); 
    

    I hope this helps you out if not try with a USB camera to ensure it's not a conflict with EMGU (OpenCV) not being able to access the device.

    Cheers,

    Chris