opencv 从摄像头中读取视频并保存(c++版)

opencv中的视频操作函数如下表所列:

VideoCapture
VideoCapture::VideoCapture
VideoCapture::open
VideoCapture::isOpened
VideoCapture::release
VideoCapture::grab
VideoCapture::retrieve
VideoCapture::read
VideoCapture::get
VideoCapture::set
VideoWriter
VideoWriter::VideoWriter
ReleaseVideoWriter
VideoWriter::open
VideoWriter::isOpened
VideoWriter::write

感兴趣的可以自己查看官方文档。
这里主要介绍几个本文中使用到的函数。

C++: VideoWriter::VideoWriter()
C++: VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true)

Parameters:
filename – Name of the output video file.
fourcc – 4-character code of codec used to compress the frames. For example, CV_FOURCC(‘P’,’I’,’M’,’1’) is a MPEG-1 codec, CV_FOURCC(‘M’,’J’,’P’,’G’) is a motion-jpeg codec etc. List of codes can be obtained at Video Codecs by FOURCC page. (如果,这里输入-1.在windows下会弹出一个对话框让你选择视频解压格式。)
fps – Framerate of the created video stream.
frameSize – Size of the video frames.
isColor – If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames (the flag is currently supported on Windows only).

    //视频保存位置
    string outputVideoPath = "..\\images\\test.avi";  

    //打开摄像头
    VideoCapture capture0(0);  

    VideoWriter outputVideo;
    //获取当前摄像头的视频信息
    cv::Size S = cv::Size((int)capture0.get(CV_CAP_PROP_FRAME_WIDTH),
                          (int)capture0.get(CV_CAP_PROP_FRAME_HEIGHT));
    //打开视频路劲,设置基本信息 open函数中你参数跟上面给出的VideoWriter函数是一样的
    outputVideo.open(outputVideoPath, -1, 30.0, S, true);
    //CV_FOURCC('P','I','M','1')

    if (!outputVideo.isOpened()) {
        cout << "fail to open!" << endl;
        return -1;
    }


    cv::Mat frameImage;
    int count = 0;

    while(true) {
        //读取当前帧
        capture0 >> frameImage;

        if (frameImage.empty()) break;

        ++count;
        //输出当前帧
        cv::imshow("output", frameImage);
        //保存当前帧
        outputVideo << frameImage;

        if (char(waitKey(1)) == 'q') break;
    }

    std::cout << "TotalFrame: " << count << std::endl;
  • 8
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
这个问题需要分步来解决。 第一步是从摄像头读取视频流。我们可以使用OpenCV的VideoCapture类来实现这一步骤。代码如下: ```c++ VideoCapture cap(0); if(!cap.isOpened()){ cout << "Cannot open camera" << endl; return -1; } ``` 这段代码将打开摄像头,并检查是否成功打开。如果摄像头不能打开,程序将返回-1。 第二步是从视频读取帧,并在帧检测人脸。我们可以使用OpenCV的CascadeClassifier类来实现这一步骤。代码如下: ```c++ CascadeClassifier face_cascade; if(!face_cascade.load("haarcascade_frontalface_alt.xml")){ cout << "Cannot load face cascade" << endl; return -1; } Mat frame; cap >> frame; vector<Rect> faces; face_cascade.detectMultiScale(frame, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30)); ``` 这段代码将加载人脸检测器,并使用它来检测视频的人脸。我们使用detectMultiScale函数来检测人脸。这个函数将返回一个包含检测到的人脸矩形的向量。我们可以使用这些矩形来裁剪帧并截取人脸图像。 第三步是裁剪帧并保存人脸图像。代码如下: ```c++ for(size_t i = 0; i < faces.size(); i++){ Rect roi(faces[i].x, faces[i].y, faces[i].width, faces[i].height); Mat face = frame(roi); imwrite("face_" + to_string(i) + ".jpg", face); } ``` 这段代码将遍历所有检测到的人脸矩形,并使用它们来裁剪帧并保存人脸图像。我们使用imwrite函数保存图像。 完整的代码如下: ```c++ #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main() { VideoCapture cap(0); if(!cap.isOpened()){ cout << "Cannot open camera" << endl; return -1; } CascadeClassifier face_cascade; if(!face_cascade.load("haarcascade_frontalface_alt.xml")){ cout << "Cannot load face cascade" << endl; return -1; } while(true){ Mat frame; cap >> frame; vector<Rect> faces; face_cascade.detectMultiScale(frame, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30)); for(size_t i = 0; i < faces.size(); i++){ Rect roi(faces[i].x, faces[i].y, faces[i].width, faces[i].height); Mat face = frame(roi); imwrite("face_" + to_string(i) + ".jpg", face); } imshow("Camera", frame); if(waitKey(30) == 27){ break; } } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值