保存的视频与来自摄像头的流式视频的持续时间不同

时间:2018-04-15 18:01:39

标签: c++ opencv save video-capture video-processing

我有一个关于用C ++中的openCV保存视频的问题(我正在使用Linux Ubuntu)。

我试图从开放式相机中保存一段时间。我终于成功了,但现在我真的没有多少想法为什么保存的流与我从相机中流式传输的持续时间不一样。当我流式传输10秒时,它只有2-3秒,看起来像是加速的。

有没有人知道可能是什么问题?我的代码或计算性能有问题,也许系统不能保存每一帧?

感谢您的帮助。

我的代码:

#include <stdio.h>
#include <iostream> // for standard I/O
#include <string>   // for strings
#include <opencv2/core.hpp>     // Basic OpenCV structures (cv::Mat)
#include <opencv2/videoio.hpp>  // Video write
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp"
    using namespace std;
    using namespace cv;

int main(int argc, char** argv)
{
    Mat capture;
    VideoCapture cap(0);
    if(!cap.isOpened())
{
        cout<<"Cannot connect to camera"<<endl;
        return -1;
    }

    namedWindow("Display",CV_WINDOW_AUTOSIZE); 

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); 
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); 

    Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));

    VideoWriter oVideoWriter ("/home/Stream_video/cpp/Cam/out.avi", CV_FOURCC('P','I','M','1'), 20, frameSize,true);

    if ( !oVideoWriter.isOpened() ) {
        cout << "ERROR: Failed to write the video" << endl;
        return -1;
    }

    while(true){
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video
        if (!bSuccess) {
            cout << "ERROR: Cannot read a frame from video file" << endl;
            break; //if not success, break loop
        }

        oVideoWriter.write(frame); //writer the frame into the file

        imshow("Display", frame); 

        if (waitKey(10) == 27) {
            cout << "esc key is pressed by user" << endl;
            break; 
        }
    }
}

0 个答案:

没有答案