无法在OpenCV中写入输出视频,程序仅写入单帧

unable to write the output video in OpenCV, program is writing only single frame

本文关键字:程序 单帧 视频 输出 OpenCV      更新时间:2023-10-16

谁能帮我写输出的视频文件?我已经阅读了许多关于如何编写程序的类似问题,并按照确切的步骤以.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)
{
    // ...