Wednesday, 11 May 2011

Take Video input from webcam and store it as a video file

 In the previous article we discussed how to install OpenCV and how to read an image and display it.
Now, let’s go through the code regaurding how to take input from webcam and save it to a video file.  
A video is a collection of images captured so fast that we cannot recognise the difference due to persistence of vision.

Code:

#include <opencv/cvaux.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
#include <stdio.h>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
 {
int coun=0;
int camera_index = 0;
IplImage* frame = NULL;
     CvCapture* capture = cvCaptureFromCAM( 0 );
     if( !capture )
     {
             fprintf( stderr, "ERROR: capture is NULL \n" );
             getchar();
             return -1;
     }
    
     double cam_w = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
      double cam_h = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
      printf("* Capture properties: %f x %f - %f fps", cam_w, cam_h, 18);
   
    cvNamedWindow("video", CV_WINDOW_AUTOSIZE);
    CvVideoWriter* writer = NULL;
    writer = cvCreateVideoWriter("out.avi", CV_FOURCC_DEFAULT, 18, cvSize((int)cam_w,(int)cam_h), 1);
    //Check for creation of writer
    if (writer == NULL)
     {
      printf("!!! ERROR: cvCreateVideoWriter");
      return -1;
     }
 while(1)
     {
        frame = cvQueryFrame( capture );
         if( !frame )
         {
                 fprintf( stderr, "ERROR: frame is null...\n" );
                 getchar();
                 break;
         }
 cvShowImage("video", frame);
 cvWriteFrame(writer, frame);
         if( (cvWaitKey(10) & 255) == 27 ) break;
     }
      cvReleaseVideoWriter(&writer);
      cvReleaseCapture( &capture );
      cvDestroyWindow( "mywindow" );
      return 0;
    }


Explanation:

CvCapture* capture = cvCaptureFromCAM( 0 );

This function takes as its argument the device ID of camera from which video is to be captured and then returns a pointer to a CvCapture structure. This structure contains all of the information about the video. When created in this way, the CvCapture structure is initialized to the beginning of video input capture.

 writer = cvCreateVideoWriter("out.avi",CV_FOURCC_DEFAULT,18, cvSize((int)cam_w,(int)cam_h),1);

This function is used to create a writer to write the captured video to the video file "out.avi".

 frame = cvQueryFrame( capture );

Once inside of the while(1) loop, we begin taking input from the camera.

if( (cvWaitKey(10) & 255) == 27 ) break;
Here the code waits for 10 ms and continues execution until "Esc" is pressed.

 cvReleaseVideoWriter(&writer);
Because there was no more video data or because the user hit the Esc key, after the video has been successfully written to "out.avi", we need to release the writer.

 cvReleaseCapture( &capture );
When we have exited the read loop (because there was no more video data or because the user hit the Esc key) we can free the memory associated with the CvCapture structure with the above function.

Now if only the processing of video is required and writting to video file is not required ,yhen we can remove the writer and code will be as follows:

#include <opencv/cvaux.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
 {
    IplImage* frame = NULL;
     CvCapture* capture = cvCaptureFromCAM( 0 );
     if( !capture )
     {
             fprintf( stderr, "ERROR: capture is NULL \n" );
             getchar();
             return -1;
     }

cvNamedWindow("video", CV_WINDOW_AUTOSIZE);
   
     while(1)
     {
        frame = cvQueryFrame( capture );
         if( !frame )
         {
                 fprintf( stderr, "ERROR: frame is null...\n" );
                 getchar();
                 break;
         }
 cvShowImage("video", frame);

 if( (cvWaitKey(10) & 255) == 27 ) break;
     }
 cvReleaseCapture( &capture );
      cvDestroyWindow( "mywindow" );
return 0;
}





1 comment:

  1. i hv tried these codes
    but found the error "abort() has been called"
    what should i do?
    plz help me

    ReplyDelete