Display Video from Webcam

Nov 4, 2008 | Tags: OpenCV | del.icio.us del.icio.us | digg Digg

If you have a webcam attached to your computer, you can display video stream using OpenCV easily. Here's the basic code.

Listing 1: Display Video from Webcam

  1. /**
  2.  * Display video from webcam
  3.  *
  4.  * Author  Nash
  5.  * License GPL
  6.  * Website http://nashruddin.com
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "cv.h"
  11. #include "highgui.h"
  12.  
  13. int main( int argc, char **argv )
  14. {
  15.     CvCapture *capture = 0;
  16.     IplImage  *frame = 0;
  17.     int       key = 0;
  18.  
  19.     /* initialize camera */
  20.     capture = cvCaptureFromCAM( 0 );
  21.  
  22.     /* always check */
  23.     if ( !capture ) {
  24.         fprintf( stderr, "Cannot open initialize webcam!\n" );
  25.         return 1;
  26.     }
  27.  
  28.     /* create a window for the video */
  29.     cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
  30.  
  31.     while( key != 'q' ) {
  32.         /* get a frame */
  33.         frame = cvQueryFrame( capture );
  34.  
  35.         /* always check */
  36.         if( !frame ) break;
  37.        
  38.         /* display current frame */
  39.         cvShowImage( "result", frame );
  40.  
  41.         /* exit if user press 'q' */
  42.         key = cvWaitKey( 1 );
  43.     }
  44.  
  45.     /* free memory */
  46.     cvDestroyWindow( "result" );
  47.     cvReleaseCapture( &capture );
  48.  
  49.     return 0;
  50. }

Save it to webcam.c and compile with command like this:

gcc webcam.c -o webcam
    -I"C:\OpenCV\cv\include"
    -I"C:\OpenCV\cxcore\include"
    -I"C:\OpenCV\otherlibs\highgui"
    -L"C:\OpenCV\lib" -lcxcore -lcv -lhighgui

Be sure to change C:\OpenCV with your OpenCV base directory. To run the program, type webcam from your command line.

What's next?

After you have successfully displayed video from your webcam, here are some possible things you can do to play around:

  • Create your own photobox by saving a frame to JPEG.
  • Save the video to AVI or MPEG files, and send it to YouTube!
  • Create a security system.
  • etc.

Related Articles

Recommended Book

The Downloads

26 Comments

Tien Loc on Jul 10, 2008:

Hello
After I read I don't understand how to do it. How to put the code inside CamVideo.

Nash on Jul 11, 2008:

what do you mean? just compile the code and run it. it will display a window with streaming video in it.

lancer on Oct 5, 2008:

If i would like to do some image processing for the video, can i just add-in together with image processing tools provided by OpenCV?

Nash on Oct 6, 2008:

Yes. See this post for an example.

Jose on Nov 7, 2008:

I cannnot display anything...it shows a black window...anybody know why?

Nash on Nov 8, 2008:

make sure your webcam properly connected

Yosef on Dec 14, 2008:

How to use 2 webcam's with OpenCV simultaneously?

jackson on Dec 14, 2008:

Hello. Me again. I wish you did the commentary of the code line by line http://www.nashruddin.com/display-video-from-webcam-with-opencv.html can I use it for a school project.

Nash on Dec 14, 2008:

Yes you can use the code for any purpose. However, some credits for me will be highly appreciated

jackson on Dec 14, 2008:

this code is working very good in my pc. tank you very Nashruddin. please i need your response about my other questions.

Nash on Dec 14, 2008:

Glad to hear it works. what is your question? send your mail to me @ nashruddin.com

jackson on Dec 15, 2008:

I need a C language code that captures images of the webcam and when the person moving the
program will play a beep but it should be any different in the position of the person. can be used
to capture this part via OpenCV. Thanks and I hope that you gona help me to implement this application.

lucky on Jan 6, 2009:

We are making a robot and the problem statement is that the robot must be able to steer itself in a virtual city marked with colors for specific operations. Can you suggest some code for real time image capturing, color detection and comparison to mainpulate the robot?

Nash on Jan 7, 2009:

can anyone help?
btw, cool project

Tab Khan on Jan 27, 2009:

Hey Nash, you have a great collection of examples and I highly appreciate that. Too good, I had a query, I am working for motion detection, am using Vista enterprise and MFC application is in progress for that. I wanted to save the frames(read from an avi file)as bmp or some other format images. I think, I need to add some code in the while loop to save those frames as images. Please do help me out.

Nash on Jan 28, 2009:

Here's an example to save the first 10 frames from the code above.

  1. IplImage *frames[10];
  2. int      i = 0;
  3.  
  4. while( key != 'q' ) {
  5.     /* get a frame */
  6.     frame = cvQueryFrame( capture );
  7.  
  8.     /* always check */
  9.     if( !frame ) break;
  10.        
  11.     /* display current frame */
  12.     cvShowImage( "result", frame );
  13.  
  14.     /* copy first 10 frames to buffer */
  15.     if( i < 10 ) {
  16.         frames[i++] = cvCloneImage( frame );
  17.     }
  18.                
  19.     /* exit if user press 'q' */
  20.     key = cvWaitKey( 1 );
  21. }
  22.  
  23. /* save images in buffer to disk */
  24. char *filename = "";
  25. for( i = 0 ; i < 10 ; i++ ) {
  26.     sprintf( filename, "%d.jpg", (i+1) );
  27.     cvSaveImage( filename, frames[i] );
  28. }
The code above will save the frames to 1.jpg, 2.jpg, ..., 10.jpg

vishal on Mar 12, 2009:

i use "dev-c++/c" on windows. how can i make this code work on this platform. i m doing a project on 'displaying videos from a webcam connected wireless to my pc." .... do u have any suggestions on how to do this ... thanx in advance

Nash on Mar 13, 2009:

Hi,
Here's how to configure Dev-C++ to compile OpenCV programs:

http://opencv.willowgarage.com/wiki/DevCpp

I don't have wireless cameras, so I don't have any experience with that. However, we'll wait for your code here

Robert on Mar 16, 2009:

Hi! Is it possible to easily send video stream to other computer on the Internet using openCV?

Nash on Mar 16, 2009:

Hi Robert,
I've made a small project about web based surveillance system using OpenCV, PHP, and Javascript. check it out.

Kent on Mar 18, 2009:

Does OpenCV support USB Video Class (UVC) Device?

Nash on Mar 18, 2009:

I think so. For more information, see Linux OpenCV Camera Compability at OpenCV wiki.

nel on Mar 22, 2009:

Hello ,
I've got a problem about executing the code. Actually I added all necessary libraries,headers etc. as told in tutorials. But I coudn't capture frame from webcam. It just returns 0 and stop executing, I mean there is no actually an error but I couldn't understand what the problem is. It reports a lot of thing but I write the last 2 lines:
'cap_cam.exe': Unloaded 'C:\Windows\System32\iyuv_32.dll'
The program '[6028] cap_cam.exe: Native' has exited with code 0 (0x0).
Thanks for helping.

Deena on Apr 19, 2009:

hi again,
i try your source code in display the video but i can not preview the video from my laptop webcam (built in), only the black screen. is there any configuration to be done unto my webcam, or something like that?
tq!

Nash on Apr 20, 2009:

Actually there is no configuration or such. Check if other application can display your webcam too.

Deena on Apr 20, 2009:

I mean, it seem does work, becos i can see the light coming from the webcam, but the screen that suppose to show the video stand still black or sometimes grey.
i try check the other application to make sure the webcam is functioning. It 's in a good condition.
so what am i going to do next?

- others can give advise me too..tq

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000

Tags

Newsletter

Send me your new post on:
OpenCV
PHP
Projects
Networking
Regex
to my email:
No, I won't spam your email.

Recent Posts

  1. PHP Script for Converting Relative to Absolute URL
  2. Streaming OpenCV Videos Over the Network
  3. Password Protected Images with PHP
  4. OpenCV Region of Interest (ROI)
  5. Web Based Surveillance System with OpenCV, PHP and Javascript

Popular Posts

  1. OpenCV Eye Detection
  2. OpenCV Examples Part 1
  3. OpenCV Face Detection
  4. OpenCV Eye Tracking
  5. Display Video from Webcam
  6. Phase Correlation in OpenCV
  7. Simple File Server and Downloader Script
  8. Building Dynamic SQL String from Associative Array
  9. Template Matching with OpenCV
  10. Fetching a Web Page From Your PHP Code

About the Author

avatar Cool PHP programmer writing cool PHP scripts. Feel free to contact
Tel. +62 31 8662872
+62 856 338 6017
ICQ 489571630
Skype dede_bl4ckheart
Yahoo dede_bl4ckheart
Google nashruddin.amin