OpenCV Seting and working with a camera

Oct 29, 2008 by     55 Comments    Posted under: OpenCV, Tutorials

Working in opencv with cameras is very easy, we only need a camera and our opencv libraries.

The structure that we store the camera link is CvCapture, here we store this link, then to get this link we need call to cvCaptureFromCAM function, this function need only one param, the index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed.

Then this is our basic line to retrive our camera link

CvCapture* capture = cvCaptureFromCAM( 0 );

Then we only need get each frame we can capture with cam, to do this we can use the cvQuetyFrame passing as param our camera link and it return a IplImage* struct that we can show.

IplImage* frame = cvQueryFrame( capture );

With this easy lines we can get all frames we need from our camera.

Demo source

55 Comments + Add Comment

  • [...] was get an error same like U, then I try dan_s's suggestion at [...]

  • I’m using windows 7 with visual studio 2010.
    The Microsoft lifecam vx-7000 is working under windows.

    I’m using your standard code without any modifications.

    Getting this error “Cant set width property”
    The life video is displaying on the screen and no frames get captured.

    Please help

  • If you see on CamShiftFrame window the video, there are no problems, only that your camera has no support for set porperties from opencv. But you can retrieve the frames and work with its, but can’t change the camera resolution for example.


  • Hi damiles
    I’m using OpenCV2.3 at ubuntu11.04
    But the camera fails to open when I try both my code and yours.

    The camera works well on cheese.
    I also install the v4lucp
    The /usr/lib/libv4l/ is also exist!
    What should I try now? Please any suggestion would be appreciated!

Got anything to say? Go ahead and leave a comment!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">