OpenCV 摄像头录制视频

// video.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include 
#include 
#include 

int main( int argc, char** argv )
{
	//声明IplImage指针
	IplImage* pFrame = NULL;
	IplImage* img;

	//获取摄像头
	CvCapture* pCapture = cvCreateCameraCapture(-1);

	//创建窗口
	cvNamedWindow("video", 1);
        CvVideoWriter *writer = NULL;
	int isColor = 1;
	int fps = 25; // or 30
	int frameW = 640; // 
	int frameH = 480;
	
	writer=cvCreateVideoWriter("out.avi",CV_FOURCC('X','V','I','D'),fps,cvSize(frameW,frameH),isColor);

	//显示视屏
	while(1)
	{
		pFrame=cvQueryFrame( pCapture );
		if(!pFrame)break;
		cvShowImage("video",pFrame);
		cvGrabFrame(pCapture); // capture a frame
		img = cvRetrieveFrame(pCapture); // retrieve the captured frame
		cvWriteFrame(writer,img); // add the frame to the file
		char c=cvWaitKey(33);
		if(c==27)break;
	}
        cvReleaseImage(&pFrame);
        cvReleaseImage(&img);
        cvReleaseVideoWriter(&writer);
        cvReleaseCapture(&pCapture);
        cvDestroyWindow("video");
}


你可能感兴趣的:(OpenCV)