OpenCV学习之摄像头录制存储视频

从摄像头读取数据,显示视频,并保存视频

#include "cv.h"  
#include "highgui.h"  
#include "iostream"  
using namespace std;
int main(int argc, char* argv[])
{
    CvCapture *cap = cvCreateCameraCapture(0);//初始化相机捕获的指针  
    if (!cap)
    {
        cout << "create camera capture error..." << endl;
        system("pause");
        exit(-1);
    }
    IplImage *tempImg = cvQueryFrame(cap);
    double fps = 20;
    CvSize size = cvSize(
        (int)cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH),
        (int)cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT)
    );
    CvVideoWriter *writer = cvCreateVideoWriter("test.avi", CV_FOURCC('X', 'V', 'I', 'D'), fps, size);
    IplImage *img_out = cvCreateImage(size, 8, 3);
    //新建窗口
    cvNamedWindow("视频监控", CV_WINDOW_AUTOSIZE);
    while ((tempImg = cvQueryFrame(cap)) != NULL)
    {
        //中值滤波
        cvSmooth(tempImg, tempImg, CV_MEDIAN, 3, 3);
        //显示视频
        cvShowImage("视频监控", tempImg);
        //存储视频
        cvCopy(tempImg, img_out);
        //TL意思是:TopLeft,即左上0;BL意思是:BottomLeft,即左下1
        //摄像机origin为1,opencv显示origin为0,所以需要垂直翻转
        if (img_out->origin == IPL_ORIGIN_BL)
        {
            cvFlip(img_out, img_out);
            //cvFlip(img_out, NULL, 0);//-1表示水平、垂直翻转,0是垂直翻转,1是水平翻转
        }
        cvWriteFrame(writer, img_out);
        char c = cvWaitKey(10);
        if (c == 27) break;
    }
    //释放空间
    cvReleaseImage(&img_out);
    cvReleaseCapture(&cap);
    cvDestroyWindow("视频监控");
    cvReleaseVideoWriter(&writer);
    system("pause");
    return 0;
}

你可能感兴趣的:(OpenCV学习笔记)