[英]OpenCV VideoWriter Assertion Failed img.cols == width && img.rows == height*3
I am trying to use the class VideoWriter(and VideoCapture of course) to use the camera and save a video, and if the video is longer than 10 seconds the last 10 seconds will be saved in the output file. 我正在尝试使用类VideoWriter(当然还有VideoCapture)来使用摄像机并保存视频,如果视频长于10秒,则最后10秒将保存在输出文件中。 There are two instances of VideoWriter in the code.
代码中有两个VideoWriter实例。 However when running I always got the following error in the console window:
但是,在运行时,控制台窗口始终会出现以下错误:
OpenCV Error: Assertion failed (img.cols == width && img.rows == height*3) in cv::mjpeg::MotionJpegWriter::write, file C:\\build\\master_winpack-build-win64- vc14\\opencv\\modules\\videoio\\src\\cap_mjpeg_encoder.cpp, line 842
Here is my code:(C++) 这是我的代码:(C ++)
void main()
{
VideoCapture vdc(0);
VideoWriter vw("REC.avi", CV_FOURCC('M', 'J', 'P', 'G'), 100.0, Size(vdc.get(CAP_PROP_FRAME_WIDTH), vdc.get(CAP_PROP_FRAME_HEIGHT)),true);
VideoWriter vw1("REC2.avi", CV_FOURCC('M', 'J', 'P', 'G'), 100.0, Size(vdc.get(CAP_PROP_FRAME_WIDTH), vdc.get(CAP_PROP_FRAME_HEIGHT)),true);
char cmd = 'a';
Mat frm;
DWORD st, ed;
st = GetTickCount();
while (cmd != 27 && vdc.isOpened())
{
vdc >> frm;
vw << frm;
vw1 << frm;
imshow("Camera", frm);
cmd = waitKey(10);
}
ed = GetTickCount();
destroyAllWindows();
cout << ed - st << endl;
vdc.release();
vw.release();
vw1.release();
if (ed - st > 10000)
{
VideoCapture vdc2("REC2.avi");
VideoWriter vw2("REC.avi", CV_FOURCC('M', 'J', 'P', 'G'), 100.0, Size(vdc2.get(CAP_PROP_FRAME_WIDTH), vdc2.get(CAP_PROP_FRAME_HEIGHT)),true);
DWORD st2, ed2;
st2 = getTickCount();
Mat frame;
while (vdc2.isOpened())
{
ed2 = getTickCount();
vdc2 >> frame;
if (ed2 - st2 > ed - st - 10000)
{
vw2 << frame;
waitKey(10);
if (vw2.get(CAP_PROP_POS_AVI_RATIO) == 1)break;
}
}
}
}
I am sure the number of the channels is three and I met no problem with vw(In fact REC2.avi can be opened normally but REC.avi can't). 我确定通道数为三个,并且vw没问题(实际上REC2.avi可以正常打开,但REC.avi无法打开)。
I use VS2017 and the latest version of OpenCV in Win10. 我在Win10中使用VS2017和最新版本的OpenCV。
I am a complete novice at OpenCV and out of things to try. 我是OpenCV的一名新手,可以尝试。
There are a couple of problems that I notice: 我注意到几个问题:
You are opening the same file in both vw1 and vw2. 您正在vw1和vw2中打开相同的文件。 I am not sure if vw1.release() closes the file properly.
我不确定vw1.release()是否正确关闭了文件。
Using the tick count to calculate the video duration of 10 seconds is a bit tricky. 使用滴答计数来计算10秒的视频持续时间有些棘手。 How about using a frame counter and the frame rate instead?
如何使用帧计数器和帧速率呢?
I am not sure but the dimensions of the frame and the size mentioned in vw2 does not match. 我不确定,但是框架的尺寸和vw2中提到的尺寸不匹配。 Try resizing the frame and then writing it.
尝试调整框架的大小,然后编写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.