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.

105 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
    4. Thanks to Dr oniha who helped me brought my ex lover back after eight months of leaving me with three kids and following another lady in Michigan, just because of little misunderstanding we had, but thanx to timely intervention of Dr oniha who uses his spell to bring him back to me, you can also contact him for a similar problem on his email on dronihaspell@yahoo.com or whatsap him on +2347089275769

      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. We are the best Security Camera System that gives their services at affordable prices.
    Fire Hydrant Fighting System

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

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

    ReplyDelete
  38. 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
  39. 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
  40. "I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.
    "

    ReplyDelete
  41. QUANTUM BINARY SIGNALS

    Get professional trading signals delivered to your mobile phone daily.

    Follow our signals today and gain up to 270% per day.

    ReplyDelete
  42. Older phones might not be compatible with these systems but good news is most new smartphone models can support the content.Penrith CCTV Systems

    ReplyDelete
  43. this contact form
    This information is magnificent. I understand and respect your clear-cut points. I am impressed with your writing style and how well you express your thoughts.

    ReplyDelete
  44. Hey! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a outstanding job! check it out

    ReplyDelete
  45. one motion sensor: something like a little CCTV that will send me message and picture when someone is close to it 8mp Hikvision

    ReplyDelete
  46. Thanks a ton for this, I am greatful for the info webpage

    ReplyDelete
  47. Thanks for your post. I’ve been thinking about writing a very comparable post over the last couple of weeks, I’ll probably keep it short and sweet and link to this instead if thats cool. Thanks.digital camera

    ReplyDelete
  48. This is such a good post. One of the best posts that I\'ve read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!
    python training in chennai
    python course institute in chennai

    ReplyDelete
  49. Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!
    python training in chennai
    python course institute in chennai

    ReplyDelete
  50. Good Post, I am a big believer in posting comments on sites to let the blog writers know that they ve added something advantageous to the world wide web.
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  51. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  52. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  53. Fabulous post, you have denoted out some fantastic points, I likewise think this s a very wonderful website. I will visit again for more quality contents and also, recommend this site to all. Thanks. Best Camera for YouTube Videos

    ReplyDelete
  54. For Python training in Bangalore, Visit:- Python training in Bangalore

    ReplyDelete
  55. I never believed that i will be happy again after much frustration. I have been rejected by my husband after Six(6) years of marriage just because he met another woman and he left me and the kids to suffer. One day when i was reading through the web i stumbled at a post on how chief dr lucky the great spell caster has helped people to restore broken marriages and relationships and curing of diseases. I emailed him immediately to help my situation via: chiefdrlucky@gmail.com He told me not to worry that my husband will return to me begging and shedding tears of forgiveness. I am so happy that my husband actually returned to me in 48 hours after i obeyed the words and instructions of chief dr lucky. contact him today in any of your problems for a lasting solution via: chiefdrlucky@gmail.com or call/whats app +2348132777335 Website: http://chiefdrluckysolutionhome.website2.me/  

    ReplyDelete
  56. Nice blog,I understood the topic very clearly,And want to study more like this.
    Data Scientist Course

    ReplyDelete
  57. Spell to fix your broken marriage or relationship problem or after a divorce or Breakup,I was recently scam by two of them, until one faithful day i meet a man called Dr Oselumen who help me to get back to the father of my kid after we have been separated for two years,I only pay for the items required for the spell and he cast the spell for me within 24hours my ex husband called me and beg me to forgive him for everything until the end of the world he will never leave me again we are back together.if you need a real and quick love spell or you are passing through pregnancy problem Dr Oselumen is the answer, Please if any body needs. LOVE SPELL,LOTTERY,PREGNANCY SPELL, DIVORCE SPELL,STOP COURT CASE AND WIN ANY COURT PROBLEM,DEATH SPELL,BUSINESS SPELL AND MANY MORE YOU MAY NEED. Email him now for your own help. via email droselumen@gmail.com add him on whatsapp line or call +2348054265852.  

    ReplyDelete
  58. my husband and I have been married for about 7 yrs now. We were happily married with two kids, a boy and a girl. 3 months ago, I started to notice some strange behavior from him and a few weeks later I found out that my husband is seeing someone. He started coming home late from work, he hardly care about me or the kids anymore, Sometimes he goes out and doesn't even come back home for about 2-3 days. I did all I could to rectify this problem but all to no avail. I became very worried and needed help. As I was browsing through the Internet one day, I came across a website that suggested that Dr jeffrey can help solve marital problems, restore broken relationships and so on. So, I felt I should give him a try. I contacted him and he did a spell for me. Two days later, my husband came to me and apologized for the wrongs he did and promise never to do it again. Ever since then, everything has returned back to normal. I and my family are living together happily again.. All thanks to Dr jeffrey . If you need a spell caster that can cast a spell that truly works, I suggest you contact him. He will not disappoint you. This is his E-mail:

    dctrjeffrey@gmail.com

    WhatsApp +2348162061202

    ReplyDelete
  59. My name is Valary West.I'm from Canada, My husband and I have been married for about 7 yrs now. We were happily married with two kids, a boy and a girl. 3 months ago, I started to notice some strange behaviour from him and a few weeks later I found out that my husband is seeing someone else. He started coming home late from work, he hardly care about me or the kids anymore, Sometimes he goes out and doesn't even come back home for about 2-3 days. I did all I could to rectify this problem but all to no avail. I became very worried and needed help. As I was browsing through I am very happy today with my family.

    My name is West Valary  living in Canada, My husband left me for good 3 months now, and i love him so much, i have been looking for a way to get him back since then. i have tried many options but he did not come back, until i met a friend that darted me to Dr. Odumeje a spell caster, who helped me to bring back my husband after 48 hours. Me and my husband are living happily together today, That man is great, you can contact him via email odumejetemple@gmail. com… Now i will advice any serious persons that found themselves in this kind of problem to contact him now a fast solution without stress..

    He always hello, now i call him my father. contact him now he is always online email odumejetemple@gmail.com or contact him on his whatsapp mobile line +2349012479806

    you can also contact me for more info about the DR here is my email. westvalary@gmail.com 

    ReplyDelete
  60. I want to thank Dr Emu a very powerful spell caster who help me to bring my husband back to me, few month ago i have a serious problem with my husband, to the extend that he left the house, and he started dating another woman and he stayed with the woman, i tried all i can to bring him back, but all my effort was useless until the day my friend came to my house and i told her every thing that had happened between me and my husband, then she told me of a powerful spell caster who help her when she was in the same problem I then contact Dr Emu and told him every thing and he told me not to worry my self again that my husband will come back to me after he has cast a spell on him, i thought it was a joke, after he had finish casting the spell, he told me that he had just finish casting the spell, to my greatest surprise within 48 hours, my husband really came back begging me to forgive him, if you need his help you can contact him with via email: Emutemple@gmail.com or add him up on his whatsapp +2347012841542 is willing to help any body that need his help.

    ReplyDelete
  61. I am very grateful to this great spell caster who brought my ex husband back to me. This testimony is an amazing one and my name is Renato Sanchez. When i came in contact with this man it was also through a testimony written about him and i have also encountered many testimonies about how he has been helping others with their life. To get back with an ex is one of the most inner most feeling many people would love to experience especially as those memories with our ex always cloud our mind when someone else does some of those things our ex used to do. I was a single parent for almost 6 years and though my ex husband was far away from my kids i still wish someday he will return back to me. This spell doctor i encountered known as Doctor UZOYA helped me with my wishes and i am happy to say that i am back with my ex husband and i am very happy sharing the testimony with everyone so that they too can meet this great doctor and solve their problems. his contact address; driraborspellcaster@gmail.com OR call his Whatsapp number on +1(301)909 8775. thank you so much Dr,UZOYA    

    ReplyDelete
  62. Thanks for the nice tutorial.Can we replicate same in windows using cmake?
    wireless home automation

    ReplyDelete
  63. I want to share my marriage experience with you people and also recommend Dr Jude to anyone who are having relationship or marriage issue Dr Jude is one of the best online love spell that I know who help people in there difficult relationship or marriage problem I almost give up on my marriage when my husband left me and the kids it was not easy for me and my kids if not for a friend of mine Jessica who direct me to this man called Jude I never knew someone like Jude still exists online my husband came back within 22hours like the way Dr Jude said it Jude is so much a nice and understandable to speak with Dr Jude personal live whatsAp number +2348140443360 or emaill writelovespell@gmail.com

    ReplyDelete
  64. Great Article
    Image Processing Projects


    Deep Learning Projects for Final Year


    JavaScript Training in Chennai

    JavaScript

    Training in Chennai


    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals,

    Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer.

    Angular Training Project Centers in Chennai

    ReplyDelete
  65. Hello everyone i know there is someone somewhere reading this,My name Maria Resende from Leicester city,England,I want to share a great work of a spell caster called Dr ogunsnya my husband and i had a fight which led to our divorce but when he left me a part of me left with him and i was to sad and cried all day and night i was searching something online when i saw people testifying about his great work and i just decided to give him a try i did everything he told me to do and he assured me that after 24 hours my husband will come back to me,the next morning to my greatest surprised a car stopped outside my house and it was my husband i'm so happy he can also help you email him at

    (drogunsnya@gmail.com)

    WhatsApp +2348137694646

    ReplyDelete
  66. Hey everyone!!!
    I am sharing this encounter about a spiritual herbalist Dr who helped me to become a military today.
    After all this years applying for military,all I get is rejections and disappointment.but one day I was going through military information when I saw in a comment section how Dr Olumba help a lot of people with his psychology magic powers.instantly I collected his email and contact him immediately,few minutes after he replied me and I told him my situation what I was going through to cut everything short that how he uses his spiritual powers to solve my problem and today am a full member of
    military.
    Get in touch with him for all kinds of spells or spiritual work his ready to help you.
    Email address {Olumbaspiritualspellcaster@gmail.com}
    WhatsApp {+2348124774363}
    website: http://drolumbaspellcaster.wordpress.com/
    https://www.facebook.com/olumbaspiritualspellcaster/

    ReplyDelete
  67. Thanks for posting the best information and the blog is very helpful.Data science course in Faridabad

    ReplyDelete
  68. I am out today to let the world know about this great man from Africa he is a good man and his spell work 💯 % according to your heart desire contacts him today and get your problem Solved because when I contacted him I thought it won't work but at last am with my husband today and with my kids all thanks to Dr Okosun you are indeed a great man God bless you' you can contact him via what's app +2348133974847 or Okosunspelltemple33@gmail.com

    ReplyDelete
  69. Thank you for nice information
    Please visit our website
    https://uhamka.ac.id

    ReplyDelete
  70. Hey there

    I have been looking for hours about this problem and I am glad that I just stumbled upon your weblog, Really helpful, Thanks for sharing it

    Best Software Development company
    Mobile app development company
    Best web development company

    ReplyDelete
  71. Great post. keep sharing such a worthy information.
    Jmeter Training

    ReplyDelete