您的位置:首页 > 运维架构

opencv 2.4.6 摄像头实时保存视频

2014-09-01 16:44 531 查看
#include "stdafx.h"

#include <iostream>

#include <opencv2\opencv.hpp>

 #include "cv.h"

#include "cvaux.h"

#include "highgui.h"

#include <cmath>

using namespace cv;

using namespace std;

void main()

{IplImage* frame=0;

CvVideoWriter *writer = 0;

 cvNamedWindow("camera",1);

CvCapture* capture;
capture=cvCreateCameraCapture(0);

//cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 240);

//cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 320);

frame=cvQueryFrame(capture);
IplImage*b= cvCreateImage(cvGetSize(frame),
 IPL_DEPTH_8U,
 1);

int isColor = 1;

int fps     = 28;  // or 30

int frameW  = frame->width ; // 744 for firewire cameras

int frameH  = frame->height ; // 480 for firewire cameras

writer=cvCreateVideoWriter("out.avi",CV_FOURCC('D', 'I', 'V', 'X'),

                           fps,cvSize(frameW,frameH),0);

/*CV_FOURCC('P','I','M','1') = MPEG-1 codec 

CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well) 

CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec 

CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec 

CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec 

CV_FOURCC('U', '2', '6', '3') = H263 codec 

CV_FOURCC('I', '2', '6', '3') = H263I codec 

CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec 

将上面的改成 -1 将会打开一个编码器的选择窗口.*/

int i=0;

int n=0;

while(i<=50)

 {

  cvGrabFrame(capture); // capture a frame 

frame=cvRetrieveFrame(capture); // retrieve the captured frame 

 cvCanny(frame , b, 3, 3, 3);

  if(!frame)

   break;

  cvShowImage("camera",b);

n=cvWriteFrame(writer,b);

cout<<n<<endl;

  if(cvWaitKey(33)>=0)

   break;

 }

cvWaitKey(33);

 cvReleaseImage(&frame);

 cvReleaseImage(&b);

 cvDestroyWindow("camera");

 cvReleaseVideoWriter(&writer);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐