Accessing Camera using OpenCV

OpenCV is fast and customizable. So one can use it for real time image processing. Thus OpenCV comes out to be very useful for using in image processing Robotics applications. In many image processing based robotics applications, a camera is mounted in robot. Using input from mounted camera behavior of robot is controlled.

In this post we will access the camera of laptop or any webcam using opencv and produce live video stream. I am using opencv 2.1 with CodeBlocks IDE on Ubuntu 11.04. If you want help in installation and configurations, then refer here.

We will create a window using highgui, capture steam form webcam using cvCaptureFromCAM and convert it to image frames using IplImage. Compile and run the following code to get live stream from your webcam.

61 comments:

  1. Its unable to detect my internal webcam.I am using opencv 2.2 in windows 7. Please help...

    ReplyDelete
    Replies
    1. Make sure you have properly installed webcam drivers. Are you able to detect webcam in your device manager? If so then try passing any of 0, 1, 2 etc in cvCaptureFromCAM function in code snippet in above post.

      Delete
    2. windows sucks for these kinds of things. You really need to use a Linux OS to program applications that use system calls and other behind the scenes operations that windows simply doesn't allow topically

      Delete
    3. I have read your blog its very attractive and impressive. I like it your blog.

      Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

      Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

      Delete
  2. Hi. I am trying to livestream from a camera(IP Webcam) in http format. The connection is established as it shows it to be 1. But I want to capture frames from the video, this is not happening. Once connection is established, it just gets stuck and no further processing is happening. Kindly help.

    ReplyDelete
    Replies
    1. Can you provide me details of your camera? Also please show me any code snippet you are using. I have never used IP webcam before so I cannot say much about compatibility. However I can help you with the code debugging if that might be the problem.

      Delete
    2. Hi there, I'm also using an IP camera (wireless). But i don't know how to connect the wireless ip camera to the open cv. Can you help me? thank you

      Delete
  3. Please tel me how can I stop this working camera capture

    ReplyDelete
    Replies
    1. just hit ESC key on your keyboard to stop video.

      Delete
  4. Replies
    1. I just checked and it is working for me. You can edit ascii number of any keyboard character in line 16 of code in above blogpost. You can refer http://www.theasciicode.com.ar/ascii-control-characters/escape-ascii-code-27.html for ascii number of any keyboard character.

      Delete
    2. huhu.. i copy the code in above blogpost but the Camera_Output is color gray. Their is no output. i think, my camera is not detected. please help me. The camera i use is CANON DS126211. please email me: takeyasu091693@gmail.com

      Delete
  5. I'm getting a real time video and setting it to a picture box using BitMap.
    So do you have any solution to stop the camera.

    ReplyDelete
    Replies
    1. camera is requested using 'cvQueryFrame(capture)' function. If you want to stop the camera, you have to break the 'while(1)' loop. You can create desired event and break the loop to stop the camera.

      Delete
    2. Ok but is there a way I can remove last captured frame from the imageBox. simply I want to make it empty.

      Delete
    3. I am not able to understand what exactly is imageBox you are referring to. I might be able to help more if you could elaborate more on what exactly you are doing and maybe try to post some code snippet.
      Logically speaking if you stop calling 'IplImage* frame = cvQueryFrame(capture);' you should be able to stop frames from camera. If you break while loop then you can terminate the whole process.

      Delete
  6. This is video capturing code

    captureVideo is name of the PictureBox

    // Find if the capture is loaded successfully or not.

    while(1)
    {
    frameCount++;

    frame = cvQueryFrame( capture );

    if( !frame )
    {

    break;
    }


    IplImageToBitmap(frame, captureVideo);

    //temp


    //}


    char c = cvWaitKey(33); // 33

    if( c == 27 )
    {
    break;
    }

    }

    cvReleaseImage( &frame );
    cvReleaseCapture( &capture );

    cvDestroyWindow("result");


    And when I click on stop button it Perform this
    stopVideo(System::Windows::Forms::PictureBox^ captureVideo){
    CvCapture* captureNew=NULL;
    while(capture){
    cvReleaseImage( &frame );
    cvReleaseCapture( &capture );

    // captureTrainVideo(captureVideo);

    break;
    }
    cvReleaseImage( &frame );
    cvReleaseCapture( &capture );
    //return captureNew;
    }

    then camera will be stop
    Then again it goes to while loop and take existing image in the PictureBox

    ReplyDelete
  7. SO please tel me how can I break the while loop

    ReplyDelete
    Replies
    1. A quick googling got me these link which might be helpful to you. I will try to post some highgui tutorial to work with video as I get some free time.
      http://www.laganiere.name/opencv1Tut/section7.shtml
      http://www.codeproject.com/Articles/339206/An-Introduction-to-OpenCV-Displaying-and-Manipulat

      Hope this helps!

      Delete
  8. I solved the problem.
    Thank you for the links I'l refer them. they may helpful for my future work.

    ReplyDelete
  9. i'm using VS2010 conbined with OpenCV 2.2 ! i've tryied programming to capturing camera but the resulst as the link below:
    http://s1052.beta.photobucket.com/user/small_chip/media/result_zps5ed64a79.png.html?sort=3&o=0
    I've configed property as follow:
    *VC++ Directories:
    +Include Directories... add: 'C:\OpenCV2.1\include\opencv;'
    +Library Directories... add: 'C:\OpenCV2.1\lib;'
    +Source Directories... add folders in path "...\\include\\Opencv2"
    *C/C++ =>General=>add “C:\OpenCV2.2\include”.
    *Linker:
    +General=>Additional Library Directories => add “C:\OpenCV2.2\lib”.
    *Input=>Additional Dependencies:
    opencv_imgproc220d.lib
    opencv_core220d.lib
    opencv_highgui220d.lib
    opencv_ml220d.lib
    opencv_video220d.lib
    opencv_features2d220d.lib
    opencv_calib3d220d.lib
    opencv_objdetect220d.lib
    opencv_contrib220d.lib
    opencv_legacy220d.lib
    opencv_flann220d.lib

    And here is the code:

    #include "stdafx.h"
    #include "highgui.h"

    int _tmain(int argc, _TCHAR* argv[])
    {
    CvCapture* capture = cvCaptureFromCAM(0);
    IplImage* src = NULL;
    cvNamedWindow("Webcam",0);
    while(1)
    {
    src = cvQueryFrame(capture);
    if(!src) break;
    char c = cvWaitKey(30);
    if(c == 27) break;
    cvShowImage("Webcam",src);
    }
    cvReleaseImage(&src);
    cvDestroyWindow("Webcam");
    return 0;
    }

    Hope for help !

    ReplyDelete
  10. How Can I use my laptop camera? is there any function?

    ReplyDelete
  11. How to capture images from multiple webcameras in opencv?please post some sample code.

    ReplyDelete
    Replies
    1. You will have to create multiple CvCapture* variables. Just put numbers like 0, 1, 2 for different cameras in the place of CV_CAP_ANY variable in line 10 of the code and run multiple loops to capture images from multiple cameras. Let me know if you need any further help.

      Delete
  12. hello! can u help me?
    " Unable to start program 'C:\Users\Administrator\Documents\Visual studio 2010\projects\webcam\Debug\webcam.exe'
    The system cannot find the file specified"

    ReplyDelete
  13. The code works very well when I run it with the external USB webcam but does not work for the lenovo internal webcam. The capture is set up but query does not work when I use internal webcam. My laptop model number is Lenovo z560. do I need to make any additional settings?

    ReplyDelete
    Replies
    1. http://opencv-srf.blogspot.com/2011/09/capturing-images-videos.html

      Delete
  14. How can I access a video capture device. I have a analog camera attached to the video capture device. I want to access that camera through opencv. When I run the code with only this device attached, the system shuts down.
    I have identified that the video capture device is a video controller and a regular webcam is a imaging device. I am thinking that capture command only looks at the imaging device and not at video controllers. Is this the reason.
    If there is a way to do this please let me know.

    ReplyDelete
    Replies
    1. Which video capture device are you using? Is this device working properly on your system without opencv? What is camera encoding type NTSC or PAL?

      Delete
    2. I am using EasyCap video capture device. Its working properly without opencv. I can capture video in windows media player. It is a PAL video.

      Delete
  15. Hi. I am trying to livestream from a gopro camera, is possible to enable it with the wifi conection of the camera? Kindly help.

    ReplyDelete
    Replies
    1. I am not sure if opencv supports gorpro camera over adhoc wifi. However you should be able to use gopro with python hack given at https://github.com/joshvillbrandt/GoProController.

      Delete
  16. My capture window closes immediately without any error despite having waitkey() command in the code. It happens most of the time whenever i try to run my own code for webcam streaming. Please help!

    ReplyDelete
  17. Hi the code worked but how can i capture an object?

    ReplyDelete
  18. How to get stream from wireless camera? (Ricoh wg-m1 or gopro)

    ReplyDelete
  19. Shard’s IP Camera Installation, wireless cameras and cloud cameras allow you set up a security system without the need to run wires around your home or business.

    And

    We provide Phone Systems to businesses across the UK, working with world-class manufacturers to offer a full range of telephony systems that can be tailored to your needs. We cater for businesses of any size, and offer a diverse product range from basic two-user phone systems through to multi-feature systems.

    ReplyDelete
  20. This looks absolutely perfect. All these tiny details are made with lot of background knowledge. I like it a lot.
    lenevo coupon code


    ReplyDelete
  21. Hi. How if I'm using camera link type. I use frame grabber from National Instrument.

    ReplyDelete
    Replies
    1. Have you been able to find a solution to using a Camera Link camera with OpenCV?

      Delete
  22. Camera_Output window is showing only gray color .
    Plz help me solve this problem,
    my webcam is SC-031139N

    ReplyDelete
  23. hey i am using opencv 3.0. but it is showing error like undefined refference to cvNameWindow.

    ReplyDelete
  24. When using this method with GTK. Because of the while loop it doesnt come back to gtk mode. Any solution?

    ReplyDelete
  25. CCTV cameras in silver screen anteroom is genuinely transforming into a faultless one against the piracy.... It really incredibly obliging for that.

    ReplyDelete
  26. Do you prepare for hunting season? Do you want to get nice quality and cheap price hunting camera? Now the chance is coming, Wosports Waterproof Hunting Camera for 50% discount off, only $54.49, waiting for you here: Hunting Camera 50% discount off

    Discount Coupons: GUGP-NZ8XXT-J4WGWJ; GUXZ-5NZUNE-UCNYUS; GUV4-2RCMBD-VZ7YUL; GUG5-3AHTP7-RWUSJZ; GUFA-4PQJTX-8KUCJY

    WOlife Wireless Doorbell
    Safety Power Strip
    AC Power Plug Adapter
    Smart Mini Wifi Router
    Smart Wireless Video Doorbell
    Portable Smart Phone Charger

    ReplyDelete
  27. Did you use Code Composer Studio (CCS) to run this code? If so, which version of CCS?

    ReplyDelete
  28. Hi,
    I want to access an external webcam connected to the C6748 DSP processor. I'm using Code Composer Studio (CCS) v5.5.0 to run the program (CCS is a IDE developed by Texas Instruments). Since I'm quite familiar with only C language, I need a code (in C language) to access the webcam.

    ReplyDelete
  29. I am awed by your post. It contains astoundingly satisfying information and i get a far reaching measure data from it. It is especially helpful for me. Appreciative for sharing and continue sharing that kind of posts.

    ReplyDelete
  30. "Camera_Output" window shows only gray

    ReplyDelete
  31. Hello,

    I am able to capture live streaming from webcam. But after stoping streaming I don't want to process last frame.

    So please tell how to do this?

    Any help appreciated!!

    ReplyDelete
  32. Do you like watching live sex cams? Then visit BongaCams.

    ReplyDelete
  33. Hi

    I've run the given code above on Linux Ubuntu 14.04 LTS using Qt Creator 5.6.2, but while building the code it gives an error "cv.h: No such file or directory". Please help me to fix the error.

    Any help will be appreciated in this regard.

    Thanks

    ReplyDelete
  34. Anyone, who can help me please to fix the error as I mentioned in my previous comment.

    Thanks for kind cooperation.

    ReplyDelete
  35. Great post with very useful information to all thanks for sharing with all of us
    buy wireless mouse online | buy wired mouse online

    ReplyDelete
  36. This comment has been removed by the author.

    ReplyDelete
  37. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.

    Home Automation in Chennai
    smart home in Chennai
    Home security in Chennai
    Burglar alarm in Chennai
    Wifi Camera Chennai

    ReplyDelete
  38. It is evident that the this blog captures very intensive C++ programming skills. The Proofreading Help the blogger could have used must have caused him to generate such useful content. I hope that the author would keep it in mind to be updating us on such issues.

    ReplyDelete