无法在OpenCV中写入输出视频,程序仅写入单帧
unable to write the output video in OpenCV, program is writing only single frame
谁能帮我写输出的视频文件?我已经阅读了许多关于如何编写程序的类似问题,并按照确切的步骤以.avi格式编写视频文件,但我无法找出我错在哪里。将创建face_output.avi文件,但它仅包含一个帧。我的程序没有将所有帧添加到视频文件中。以下是完整的代码:
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay( Mat frame);
/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
double fps;
/** @function main */
int main( int argc, const char** argv )
{
VideoCapture cap("/home/pradeep/Downloads/President Obama Lectures Romney.mp4"); // open the video file for reading
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loadingn"); return -1; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loadingn"); return -1; };
fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Frame per seconds : " << fps << endl;
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
Size S(dWidth,dHeight);
while(1)
{
Mat frame;
int skip_frame = 4;
while(skip_frame)
{
bool bSuccess = cap.read(frame); // read a new frame from video
skip_frame--;
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
}
//-- 3. Apply the classifier to the frame
if( frame.empty() )
{ printf(" --(!) No captured frame -- Break!"); break; }
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.3, 5, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 0, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t j = 0; j < eyes.size(); j++ )
{
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
VideoWriter Video("face_output.avi", CV_FOURCC('M','J','P','G'), fps, S, true);
if(!Video.isOpened())
{
printf("unable to write video file");
}
Video.write(frame);
//-- Show what you got
imshow( window_name, frame );
int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
return 0;
}
您将在
while(1)
中创建VideoWriter Video("face_output.avi", CV_FOURCC('M','J','P','G'), fps, S, true);
,因此在每次迭代中都会创建一个新Video
。由于每次迭代只写入一帧,因此这将是face_output.avi文件的唯一内容。
尝试在while(1)
之前移动该行:
// ...
Size S(dWidth,dHeight);
VideoWriter Video("face_output.avi", CV_FOURCC('M','J','P','G'), fps, S, true);
while(1)
{
// ...
相关文章:
- Opencv 如何从相机读取单帧
- 在Visual Studio中单实例Qt应用程序版本5.11.1中,在所有其他窗口的顶部打开Qt MainWindow
- win 表单应用程序字符串^ 到 int
- 如何将我的程序添加到文件和文件夹的macOS右键单击菜单?
- 如何提高 OpenCV 实时检测器应用程序的帧率?
- 表单未显示在凭据提供程序中
- 如何使用 ALSA API 在应用程序中在单声道和立体声之间切换声音输出?
- C++单例模板类使我的程序崩溃
- 当某些应用程序处于全屏模式时,DesktopDuplication API会产生黑帧
- 基于 IOKit 的 kext 驱动程序中的单例类
- 如何在后台模拟鼠标单击到 DirectX 应用程序?
- 单声道嵌入在 qt 5.3 应用程序中
- 无论如何,是否可以确定MFC应用程序是通过PC重新启动或用户单击应用程序快捷方式开始的
- 应用程序最小化时单击显示桌面/最小化全部
- 在无窗口 GLX 程序中读取 OpenGL 的默认帧缓冲
- 为什么调用单例类 Qt 消息处理程序成员函数会出现错误:缺少参数列表
- 如何单击wxwidgets处理程序中的按钮
- 无法在OpenCV中写入输出视频,程序仅写入单帧
- 我如何获得和改变OGRE应用程序的帧率?
- visual studio 2010-C++Windows窗体应用程序单击按钮即可访问另一个窗体