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

31 Comments to “OpenCV Seting and working with a camera”

  1. maxi 5 May 2009 at 1:54 pm #

    easy and beautiful, thank you very much!

  2. Muthu 1 October 2009 at 11:49 am #

    hi

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

    i am using proslica firewire camera

    pls help me.

  3. damiles 1 October 2009 at 11:53 am #

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

    Regards

  4. sotiris karavarsamis 19 October 2009 at 10:13 pm #

    can can i check if the cam’s working under v4l? i am getting the same problem as muthu…

  5. sotiris karavarsamis 19 October 2009 at 10:14 pm #

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

  6. vijai 28 October 2009 at 3:20 pm #

    Hi All,

    I need a help..

    I have a 1394 camera connected to my PC.Iam not able to capture the image from the camera.I was able to run the sample code of opencv.

    $testlibraw
    successfully got handle
    current generation number: 1
    1 card(s) found
    nodes on bus: 2, card name: ohci1394
    using first card found: 2 nodes on bus, local ID is 1, IRM is 1

    doing transactions with custom tag handler
    trying to send read request to node 0… completed with value 0xd911fe78
    trying to send read request to node 1… completed with value 0×5819fe78

    using standard tag handler and synchronous calls
    trying to read from node 0… completed with value 0xe924fe78
    trying to read from node 1… completed with value 0×4629fe78

    testing FCP monitoring on local node
    got fcp command from node 1 of 8 bytes: 01 23 45 67 89 ab cd ef
    got fcp response from node 1 of 8 bytes: 01 23 45 67 89 ab cd ef
    testing config rom stuff
    get_config_rom returned 0, romsize 64, rom_version 4
    here are the first 10 quadlets:
    0. quadlet: 0xc8850404
    1. quadlet: 0×34393331
    2. quadlet: 0×32a264e0
    3. quadlet: 0×087a0200
    4. quadlet: 0×0f820100
    5. quadlet: 0×32b90300
    6. quadlet: 0×7a020003
    7. quadlet: 0×02000081
    8. quadlet: 0xc083000c
    9. quadlet: 0×2a2c0600
    update_config_rom returned 0

    polling for leftover messages

    The libdc version is ..

    libdc1394-13 1.1.0-5ubuntu1

    libdc1394-13-dev 1.1.0-5ubuntu1

    opencv i got it from..

    svn co https://opencvlibrary.svn.sourceforge.net/svnroot/opencvlibrary/trunk

    ———-program output————–
    $gcc `pkg-config –cflags –libs opencv` -o my-opencv-prgm camdemo.c

    In file included from /usr/local/include/opencv/cxcore.h:70,
    from /usr/local/include/opencv/cv.h:58,
    from camdemo.c:6:
    /usr/local/include/opencv/cxtypes.h: In function ‘cvRound’:
    /usr/local/include/opencv/cxtypes.h:240: warning: incompatible implicit declaration of built-in function ‘lrint’

    Pls let me know if any other infl is required..

  7. [...] [1.] http://blog.damiles.com/?p=61 [...]

  8. virgoptrex 9 November 2009 at 5:52 pm #

    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?

  9. Abhishek 4 January 2010 at 1:20 am #

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

  10. damiles 4 January 2010 at 10:19 am #

    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.

    Regards.

  11. Hsu 5 February 2010 at 5:23 pm #

    Hi I’m trying to get OpenCV recognise multiple cameras. My code is below.

    When I run the code, two windows pops up and captures seem to work. But the windows blank out later and the program hanged. And I got the following error message

    “HIGHGUI ERROR: V4L: Could not SYNC to video stream. Input/output error”

    Do you know what might be the problem?

    thanks,
    Hsu

    ************************************************

    #include “highgui.h”
    #include “cv.h”
    #include “cvaux.h”
    #include “stdio.h”

    int main(int argc, char** argv) {
    int done = 0;

    cvNamedWindow(“Left Frame”,CV_WINDOW_AUTOSIZE);
    cvMoveWindow(“Left Frame”, 100, 100);
    cvNamedWindow(“Right Frame”,CV_WINDOW_AUTOSIZE);
    cvMoveWindow(“Right”, 420, 100);

    //CvCapture* capture;
    CvCapture* captureLeft;
    CvCapture* captureRight;

    if(argc == 1) {

    //capture = cvCreateCameraCapture(0);
    captureLeft = /*cvCaptureFromCAM(0);*/cvCreateCameraCapture(0);
    captureRight = /*cvCaptureFromCAM(1);*/cvCreateCameraCapture(1);

    cvSetCaptureProperty(captureLeft, CV_CAP_PROP_FRAME_WIDTH, 320);
    cvSetCaptureProperty(captureLeft, CV_CAP_PROP_FRAME_HEIGHT, 240);

    cvSetCaptureProperty(captureRight, CV_CAP_PROP_FRAME_WIDTH, 320);
    cvSetCaptureProperty(captureRight, CV_CAP_PROP_FRAME_HEIGHT, 240);

    if( !captureLeft )
    {
    printf( “Cannot open initialize WebCam 1\n” );
    return 1;
    }

    if( !captureRight )
    {
    printf( “Cannot open initialize WebCam 2\n” );
    return 1;
    }

    } else {
    captureLeft = cvCreateFileCapture(argv[1]);
    }

    IplImage* leftFrame;
    IplImage* rightFrame;
    while(1) {

    leftFrame = cvQueryFrame(captureLeft);
    rightFrame = cvQueryFrame(captureRight);

    if(!leftFrame) break;
    if(!rightFrame) break;

    cvShowImage(“Right Frame”,rightFrame);
    cvShowImage(“Left Frame”,leftFrame);

    char c = cvWaitKey(10);
    if(c==27){ done = 1; break;}

    if(done == 1) {
    cvWaitKey(0);

    cvReleaseImage(&leftFrame);
    cvReleaseImage(&rightFrame);
    cvDestroyWindow(“Left Frame”);
    cvDestroyWindow(“Right Frame”);
    }
    }
    cvWaitKey(0);

    cvReleaseCapture(&captureLeft);
    cvReleaseCapture(&captureRight);
    return(0);
    }

  12. prathik 6 February 2010 at 7:03 pm #

    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

  13. Fújur 17 February 2010 at 10:49 am #

    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?

  14. snaap 12 March 2010 at 4:00 am #

    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.

  15. somu 16 March 2010 at 4:32 am #

    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.

  16. damiles 16 March 2010 at 10:07 am #

    If there are no usb camera it works?

  17. Aqib 18 March 2010 at 9:50 am #

    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.

  18. uness 14 April 2010 at 4:41 pm #

    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!!

  19. damiles 14 April 2010 at 5:27 pm #

    Hi, what’s the error you get? if it work with v4l it must work under opencv linux…

  20. uness 14 April 2010 at 7:38 pm #

    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!

  21. uness 16 April 2010 at 4:36 pm #

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

  22. damiles 19 April 2010 at 9:06 am #

    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.

  23. uness 19 April 2010 at 4:55 pm #

    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 1280×1024 pixel!). For OpencCv I dont store any thing i just try to capture from cam and it gives those errors!

  24. damiles 19 April 2010 at 5:03 pm #

    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.

  25. uness 19 April 2010 at 6:00 pm #

    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?

  26. damiles 19 April 2010 at 6:20 pm #

    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.

  27. neeraj 1 May 2010 at 9:12 pm #

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

  28. damiles 3 May 2010 at 5:45 pm #

    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);

    Regards.
    David.

  29. neeraj 12 May 2010 at 6:57 am #

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

    http://s848.photobucket.com/albums/ab44/neerajvenator/

    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?

  30. damiles 12 May 2010 at 9:04 am #

    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.

  31. michele 21 May 2010 at 4:53 pm #

    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?


Leave a Reply