请查看以下代码:
#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;
}
}
}
此代码运行正常,没问题。但是,当我运行录制的视频时,速度非常快!喜欢它快速转发。我真的不明白为什么。
答案 0 :(得分:2)
检查从相机中抓取帧的速率,并确保此速率与将帧记录到输出文件中的速率相匹配。
写入文件的帧速率指定为this function的fps
参数:
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;
}