c++ - 录像太快

扫码查看

请看下面的代码:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <string>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/background_segm.hpp>

using namespace std;
using namespace cv;

double getMSE(const Mat& I1, const Mat& I2);

int main()
{
    Mat current;
    VideoCapture cam1;
    VideoWriter *writer = new VideoWriter();



    cam1.open(0);

    namedWindow("Normal");

    if(!cam1.isOpened())
    {
        cout << "Cam not found" << endl;
        return -1;
    }

    cam1>>current;
    Size *s = new Size((int)current.cols,current.rows);
    writer->open("D:/OpenCV Final Year/OpenCV Video/MyVideo.avi",CV_FOURCC('D','I','V','X'),10,*s,true);


    while(true)
    {
        //Take the input
        cam1 >> current;

        *writer << current;
        imshow("Normal",current);

        if(waitKey(30)>=0)
         {
               break;
         }


    }
}

这段代码运行正常,没有问题。但是,当我运行录制的视频时,它 super 快!就像它被快速转发。我真的不明白为什么。

最佳答案

检查从相机抓取帧的速率,并确保该速率与将帧记录到输出文件中的速率匹配。

写入文件的帧频指定为this functionfps参数:

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

对于相机fps,对于某些相机,您可以确定其帧速率as follows
double fps = cam1.get(CV_CAP_PROP_FPS);

或者,如果相机不支持此方法,则可以通过测量连续帧之间的平均延迟来找到其帧速率。

更新:如果您的相机不支持cam1.get(CV_CAP_PROP_FPS);,则可以通过实验估算帧速率。例如:
while(true) {
    int64 start = cv::getTickCount();

    //Grab a frame
    cam1 >> current;

    if(waitKey(3)>=0) {
        break;
    }

    double fps = cv::getTickFrequency() / (cv::getTickCount() - start);
    std::cout << "FPS : " << fps << std::endl;
}

另外,请确保已打开输出视频文件以进行写入
if ( !writer->isOpened())
{
    cout  << "Could not open the output video for write: " << endl;
    return -1;
}

关于c++ - 录像太快,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17575455/

10-11 22:10
查看更多