opencv realizes timing recording function

  • 2020-06-07 04:54:12
  • OfStack

As a powerful machine vision library, opencv is favored by image processing developers for its simplicity. Now I'll show you how to use opencv to implement timed video recording and save it in real time as the file name. There have been some similar codes on the Internet before, but most netizens reported that the program could not be executed, mainly for two reasons. There is no video encoder installed on the computer, XviD is recommended here, it is not easy to download on the Internet, you can send it to me privately or leave your email, I will send it all. Then I chose 'X', 'V', 'I', 'D', which is the common avi format.

  #include "cv.h"  
  #include "cxcore.h"  
  #include "highgui.h"  
  #include <iostream>  
  #include <time.h> 
  #include <Windows.h> 
  #include <Mmsystem.h> 
  #include "stdio.h"  
  int timea=100000;  
  using namespace std;  
/*void times() 
  SYSTEMTIME sys_time; 
  // Sets the variable value to local time  
  GetLocalTime( &sys_time ); 
  // The output of time  
  printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d  week %1d\n",sys_time.wYear, 
 // system("time"); 
 // system("pause"); 
  return 0; 
} */ 
  int main()  
    CvCapture* capture=cvCaptureFromCAM(0);  
    CvVideoWriter* video=NULL;  
    IplImage* frame=NULL;  
    int n;  
    if(!capture) // Give a warning if you can't turn on the camera   
     cout<<"Can not open the camera."<<endl;  
     return -1;  
     frame=cvQueryFrame(capture); // So let's get the one in the camera 1 frame   
    int c=0; 
    SYSTEMTIME sys_time; 
  // Sets the variable value to local time  
    GetLocalTime( &sys_time ); 
    char buf[1024]; 
    sys_time.wHour,sys_time.wMinute, sys_time.wSecond);  
      video=cvCreateVideoWriter(buf, CV_FOURCC('X', 'V', 'I', 'D'), 25,  
     cvSize(frame->width,frame->height)); // create CvVideoWriter Object and allocate space   
  // The file name is saved camera.avi The size is the size of the camera's video, and the frame rate is 32  
     if(video) // If I can create CvVideoWriter Object indicates success   
       cout<<"VideoWriter has created."<<endl;  
   cout<<"set the record time\n"<<endl;// Set recording time  
   int ti=timea*25; 
     cvNamedWindow("Camera Video",1); // new 1 A window   
      int i = 0;  
     while(i <= ti) //  Let it loop ti The admission is automatically suspended   
       frame=cvQueryFrame(capture); // from CvCapture gain 1 frame   
        cout<<"Can not get frame from the capture."<<endl;  
       n=cvWriteFrame(video,frame); // Determines whether the write was successful, if yes is returned 1 , indicates that the write was successful   
       // cout<<n<<endl;  
       cvShowImage("Camera Video",frame); // A picture showing the content of a video   
        break; // Exit if there is another keyboard response   
     cvReleaseVideoWriter(&video); // If you do not release, spring will not  
     cvDestroyWindow("Camera Video");  
    return 0;  

Related articles: