如果检测结果为true,则写入/保存视频流
Write/save videostream if detection true
我想在检测结果为true后编写一个视频流。
我使用此链接作为Videowrite示例
我的代码实现看起来是这样的:
int main(int argc, const char** argv) {
bool detection = false;
VideoCapture cap(-1);
if (!cap.isOpened())
{
printf("ERROR: Cannot open the video file");
}
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize (static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter record ("/home/hacker/MyVideo.avi", CV_FOURCC('P','I','M','1'),
30, frameSize, true);
if (!record.isOpened())
{
printf("Error: Failed to write the video");
return -1;
}
while (true)
{
Mat frame;
if (!frame.empty())
{
detectAndDisplay(frame);
}
else
{
printf(" --(!) No captured frame -- Break!"); break;
}
if (detection == true)
{
record.write(frame);
}
char c = cvWaitKey(33);
if (c == 27) { break; }
}
return 0;
}
在我的主目录中,我可以看到Myvideo.avi,但它是空的。
我在命令行上得到以下错误:
VIDIOC_QUERMENU: Invalid argument
VIDIOC_QUERMENU: Invalid argument
Frame size: 640x480 Output #0, avi, to '/home/hacker/MyVideo.avi":
Stream #0.0: Video: mpeg1video (hq), yvu420p, 640x480, q=2-31, 19660
kb/s, 9ok tbn, 23,98 tbc
--(!) No captured frame -- Break! Process returned 0 (0x0) execution time: 0,75 s
您应该发布videowriter(record.release();)。它关闭文件。
我试着这样解决它:
但我有两个问题:
如果detecAndDisplay(frame)==true,我想保存MyVideo.avi。但他还是保存了它(用一个空的视频记录)。如果保存,视频录制会运行得更快。
int main( int argc, const char** argv ) {
Mat frame;
VideoCapture capture(-1); // open the default camera
if( !capture.isOpened() )
{
printf("Camera failed to open!n");
return -1;
}
capture >> frame; // get first frame for size
for(;;)
{
// get a new frame from camera
capture >> frame;
//-- 3. Apply the classifier to the frame
if( !frame.empty() )
{
detectAndDisplay( frame );
}
if(detectAndDisplay(frame)==true)
{
// record video
VideoWriter record("MyVideo.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);
if( !record.isOpened() )
{
printf("VideoWriter failed to open!n");
return -1;
}
// add frame to recorded
record << frame;
}
if(waitKey(30) >= 0) break;
}
return 0;
}
/** @function detectAndDisplay */
/** this function detect face draw a rectangle around and detect eye & mouth and draw a circle around */
bool detectAndDisplay( Mat frame ) {
...
...
return true;
}
这可能是视频文件为空的原因。
bool detection = false;
...
if (detection == true)
{
record.write(frame);
}
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 如何使用Qt多媒体和C++将.mp4视频从OpenGL纹理中保存出来
- Opencv 保存视频
- 使用 LIBVLC 保存视频
- 保存OpenGL物理模拟作为视频
- 在Ubuntu中使用VideOwriter(OpenCV)保存视频作为MP4时出错
- OpenCV 4.0 无法保存视频
- 视频编写器功能不会使用 OpenCV-3.0.0 保存文件
- 将视频文件的帧总数保存到.txt文件中
- OpenCV将视频保存到文件
- 如何将视频帧保存到缓冲区中
- 视频捕获并使用OpenCV保存
- visual如何在C++中将捕获的流媒体视频保存到avi文件中
- 保存帧时冻结视频程序
- 不保存任何图像。保存视频帧时
- 如何保存视频的任何帧