Powered by Blogger.

Wednesday, October 29, 2008

Tag: , ,

OpenCV Seting and working with a camera

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

About David Millán Escrivá

David completed his studies in Universidad Politecnica de Valencia in IT with a Master's degree in artificial intelligence, computer graphics, and pattern recognition, focusing on pattern recognition and Computer Vision. David has more than 15 years of experience in IT, with more than ten years of experience in Computer Vision, computer graphics, and pattern recognition, working on different projects and start-ups, applying his knowledge of Computer Vision, optical character recognition, and augmented reality. Co-Author of two OpenCV books and reviewer of few more.


  1. easy and beautiful, thank you very much!

  2. hi

    i am using ur code but the frame it is returning is NULL.

    i am using proslica firewire camera

    pls help me.

  3. Are you on Linux or windows? If you are on linux please, check if your camera run under v4l.


  4. can can i check if the cam's working under v4l? i am getting the same problem as muthu...

  5. how can i check if the cam is working under v4l under ubuntu? i am getting the same error!

  6. I have already been successful by using prosilica GIGE cam and OpenCV. Infact now I am viewing real time edge detected video. However the computation seems to overload and freeze my processor. Hence I am looking for ways to ensure previous frame get captured and computation is done successfully before display. Presently I am using delay through timers. but thats not the best way I know So I am thinking of POSIX threads based realization or have flags check. Anyone has tried this before?

  7. Hey i wanted to know if all cameras are compatible with openCV so that i can decide on which one to buy.
    Pls reply on my id shadowabhishek@gmail.com..

  8. Hi, opencv have support to v4l and firewire cameras, if in your system works your camera sure it works in opencv.

    If you are looking for for profesional camera, i can't help to select one.


  9. Hello,

    I am trying to work with 2 cameras. But I am working in windows and open cv 1.1. It doesn't have cvcam library and I have no clue how to get it working!!! Do any one have an idea how to call two cameras in windows in opencv 1.1

    Thank you

  10. Hi, Hsu, I think your code should work. May be an USB host bandwith problem? Try rising the delay on your first cvWaitKey from 10 to 30.

    Also, check the location of the break sentence on your program exit code. I think it is misplaced and you are not releasing resources properly. I think it would be easier to break on ESC key and release all resources out of the while brackets.

    By the way, left and right cameras? Are you on some stereo vision aplication?

  11. hi i seem to be having the same problem as Hsu.desptire the fact that am using python.
    when i change the device parameter from cvCaptureFromCAM(0) to cvCaptureFromCAM(1).
    the program crashes.
    is there anyway that i can make it use more than one webcamera.

  12. Hello , I am doing a project , I am using a webcam for object detection
    the object is a company logo , I am doing it in my laptop . Now when I
    use an USB web camera it is working properly , but when I am going to
    access the internal web cam of the laptop then nothing is showing on
    the screen but I have assured that it is recognizing the camera . I am
    using cvCaptureFromCAM function . PLEASE HELP ME.

  13. If there are no usb camera it works?

  14. This works great for me,actually I am working on a project;its objective is to capture and also store the frames as .jpg so i can then do image recognition on the required image.
    I'll appreciate if you could help me out and I will elaborate more.

  15. Hello!
    I have a sumix camera (smx-150M) that cannot work with OpenCV!!! It woked in linux with xawtv(v4l) and in windows with its own installed application! i cant figure out what's the matter! please help me!!

  16. Hi, what's the error you get? if it work with v4l it must work under opencv linux...

  17. Hello damiles!
    I got the error! incoming pixel formats is unsupported in OpenCV! Bad File descriptor! cannot end stream.. and some times when running my sources from shell it gives another error: /dev/video0 doesnt support memory mapping:s..
    Thank you for interst!

  18. I would be very grateful if you can answer me a little soon! I'm stuck in my project!

  19. Please, check camera run correctly under v4l, if it go ok and don't have any problem as memory mapping or similar, if all is ok then check the input format, there are opencv functions for camera settings, then try to chek the camera settings and then try run with correct settings for your project. The incoming pixel format can be you try read a camera image 8 bit gray pixels and try to store in 32 pixel gray image.

  20. I checked it! it works with some V4L utilities and fails with others. The error seems to talk about some grey format (My cam is a professional USB camera high resolution 1280x1024 pixel!). For OpencCv I dont store any thing i just try to capture from cam and it gives those errors!

  21. Ok then you must check this error, and check this gray format, opencv over linux works with v4l then perhaps your camera don't work, or need a driver for work correctly with v4l, if you can run correctly with xawtv and it don't have error then sure you can run in opencv. but if you have error try to get a corret driver for your camera or create your v4l access...

    The camera is profesional, then sure have an sdk, check and try to use it instead opencv camera settings, then only need convert image camera sdk to opencv image.

  22. The manufacturer doesnt offer a lot of support for Linux, i just got a small driver from them.Trying to capture with VLC or tools like gives this error with dmesg:"smxcam_do_ioctl(): v4l2_standard.index=1 != 0
    smxcam_do_ioctl(): v4l2_fmtdesc.fmt.pix.pixelformat=877807426 != V4L2_PIX_FMT_GREY or V4L2_PIX_FMT_RGB24 "
    Knowing that function smxcam-do-ioctl is in the driver implements V4L2 ant tests for palettes! Any idea please?

  23. Im't not expert in v4l, you must send mail to v4l users to help in this because you have a bad driver or using one you don't need because you don't have a correct pixel format from camera.

    I cant help in this. Sorry.

  24. i just gone through your blog..its amazing...i am building a protocol model of hawk-eye as my university project.my question is that can i save the individual frame to a disk..?i tried out with cvSaveImage but its saving only one frame,and that to starting of the video.please can you help me in this??thanks in advance.....

  25. Hi. With cvSaveImage you can save all frames you want. In loop you must call to

    frame = cvQueryFrame( capture );

    And in loop save this frame with cvSaveImage

    cvSaveImage("name_"<< frame_num << ".jpg", frame);


  26. Hey thanx alot for help...it works fine now.
    u may view my captured frame at :-


    now my task is to get the co-ordinates of the ball and line and judge whether the ball is inside the line or outside.how can i get the co-ordinates?i am not able to get it.please can you help me?

  27. You must determine what is background and wath is not background for detect the ball. Then you can get the ball positions.

    For the line you can search with simple image segmentation looking for white colors.

    Regards. David.

  28. Hi

    I'm workin with 2 cameras, but I can't identify with one...

    for example, how can I know that index 0 references camera A and index 1 references the camera B?

    Someone help?

  29. Hey master can you help me?
    I'm using python bindings for opencv trying yo get a photo with my webcam. I have opencv2.1 and python 2.6 over a PowerBookG4 MAC OSX 10.5.8 (originally with python2.5). Well, I'm not sure why I have this error when I try to make this code:
    capture_size = (320,200)
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, capture_size[0])
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, capture_size[1])
    loop = True
    frame = cv.QueryFrame(capture)
    if (frame == None):
    cv.ShowImage("Example2", frame)
    char = cv.WaitKey(33)
    if (char != -1):
    if (ord(char) == 27):
    loop = False


    2010-09-11 01:46:50.968 Python[12265:c60b] ***_NSAutoreleaseNoPool(): Object 0x7168540 of class NSThread autoreleased with no pool in place - just leaking
    Stack: (0x9320ddac 0x9313ae14 0x931403e0 0x736af14 0x736fe60 0x736e5a4 0x7372958 0x73715e0 0x94c10560 0x94ad4564 0x94c133d8 0x94adb8f4 0x94c115f0 0x94ac21f4 0x94ac75b8 0x9501f4c0 0x6fad750 0x7042538 0x7042644 0x959460d4)

    2010-09-11 01:46:53.285 Python[12265:c22f] *** QTCaptureSession warning: Session received the following error while decompressing video: Error Domain=NSOSStatusErrorDomain Code=-157 "Operation could not be completed. (OSStatus error -157.)" (invalid pixel depth ). Make sure that the formats of all video outputs are properly configured.

    Please help me

  30. hi
    how can i take pic from web cam by opencv
    thanx in advance

  31. Hi, each frame of this code is an image. If you want save the image to disk you call cvSaveImage function. If you only want the first image then remove while loop.

    Regards David.

  32. Hi

    Im finding it difficult to access my CCTV camera with OpenCv using its normal API it detects my web cam but not CCTV cam (CCTV works fine with its provided DVR software application)

    Its great if you can help

  33. Hi Thushan, opencv support usb cams, v4l and firewire, here you can check a list of cameras: http://opencv.willowgarage.com/wiki/Welcome/OS

  34. Thanks for your input

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

    Im having problems using it under Opencv. Your demo program build successful. The capture screen is open for a few seconds.

    this is my code.
    #ifdef _CH_
    #pragma package

    #ifndef _EiC
    #include "cv.h"
    #include "highgui.h"

    void showCaptureProperties(CvCapture* cap){
    int w,h,f;
    printf("Capture properties (widthxheight - frames): %dx%d - %d\n",w,h,f);

    int main( int argc, char** argv )
    CvCapture* capture = 0;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
    capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
    else if( argc == 2 )
    capture = cvCaptureFromAVI( argv[1] );

    if( !capture )
    fprintf(stderr,"Could not initialize capturing...\n");
    return -1;

    printf( "Hot keys: \n"
    "\tESC - quit the program\n"
    "\tDavid Millán Escrivá | Damiles\n");

    cvNamedWindow( "CamShiftDemo", 1 );

    cvQueryFrame(capture);//Need to get correct data
    if(cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,320))
    printf("Cant set width property\n");

    if(cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 240))
    printf("Cant set height property\n");

    IplImage* frame = 0;
    int c;

    frame = cvQueryFrame( capture );
    if( !frame )

    cvShowImage( "CamShiftDemo", frame );

    c = cvWaitKey(10);
    if( (char) c == 27 )

    cvReleaseCapture( &capture );

    return 0;

    #ifdef _EiC

  36. Please check you don't have any segfault and you can capture images, you can use the if(!frame){ printf("You can't capture frames\n"); break;}


  37. Hi,

    How can I capture frames if I have a GigE camera? Can I still use

    CvCapture* capture = cvCaptureFromCAM( 0 )

    Thanks for your help.

  38. I have a GigE camera. How can i capture image from it with opencv? it works with a normal USB camera

  39. Hi, check this list: http://opencv.willowgarage.com/wiki/Welcome/OS

  40. hi...
    how to save images using webcam and save the images frame by frame through opencv?

  41. i've U project..so I need some help from you guys regarding opencv project.

    how to capture images frame-by-frame and save it to the array?

  42. Hi you can save image inside the loop with cvSaveImage function check the function definition on opencv wiki documentation

  43. You can save all info you want with xml/YaML opencv functions or see my tutorial about save structures on opencv

  44. we are not able to take image using opencv. we are using python binding.
    we try this.... CvCapture* capture = cvCaptureFromCAM( 0 );
    IplImage* frame = cvQueryFrame( capture );

    but when we run the code error comes like this...
    "can't assign to operator"

    can u plz help us.........

  45. I don't work with python, but first check your camera works fine with opencv, on linux you can check with xawtv.

    You can check before cvQueryFrame with if(!capture) exit.

    Regards David.

  46. [...] was get an error same like U, then I try dan_s's suggestion at http://blog.damiles.com/?p=61. [...]

  47. 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

  48. 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.


  49. Do't we need to release the extracted image and release the camera ?

  50. 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/v4l2convert.so is also exist!
    What should I try now? Please any suggestion would be appreciated!

  51. I got this. But I don't know how to do.

  52. Hello, I am using a kinect with openni.
    Is there any way to get number of frames from streaming camera?

  53. Is there any way to get number of frame from camera (kinect) using opencv?

  54. cvCaptureFromCAM(0); how to make this work in java

  55. http://matureadultfilms.com is your number 1 stop for all your porn dvd requirements. We are the cheapest online, guaranteed or your money back adult dvds.

  56. OpenCV returns a non-NULL capture structure with my camera, but when i do cvQueryFrame() on the structure, it gives me a NULL frame.
    How to solve this?

  57. the whole is compiled without any erron..... but i get a black image instead.......all of this working fine in emgucv in visual c#.....please help..


  58. Hi damiles,

    As per the code " CvCapture* capture = cvCaptureFromCAM( 0 );" opencv function will detect built in camera .But in my case i am using "dell Vostro" laptop with "windows 7" and visual studio 2008, the code not detecting camera .Kindlyprovide me some solution .Help would be appreciated greatly.


  59. How to use the camera of Kinect?