opencv成长之路2:一段用来获取摄像头视频的代码

#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
  
using namespace cv;  
  
int main(int argc, char *argv[])
{
   namedWindow("win");
   CvCapture* capture = cvCreateCameraCapture(0);                            //表示从摄像头获取视频,如果有需要,需要重新写
   IplImage* frame = cvQueryFrame(capture);
   CvVideoWriter* writer = cvCreateVideoWriter("f:\\my.avi", CV_FOURCC('M','J','P','G'),
                          12, cvGetSize(frame));
   while(1){
           frame = cvQueryFrame(capture);
           if(!frame) break;


           cvWriteFrame(writer, frame);
           cvShowImage("win", frame);


           char c = cvWaitKey(1);//(<span style="color:#ff0000;">2</span>)
           if(c==27) break;         //表示按ESC退出
       }


       cvReleaseVideoWriter(&writer);
       cvReleaseCapture(&capture);
       cvDestroyWindow("win");
   return 0;


}

你可能感兴趣的:(opencv成长之路2:一段用来获取摄像头视频的代码)