0

我想在发现检测为真后写一个视频流。

我将此链接用作 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
4

3 回答 3

0

你应该释放视频作者(record.Release();)。它关闭文件。

于 2013-08-22T13:17:53.683 回答
0

我尝试像这样解决它:

但我有两个问题:

如果 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;

}
于 2013-08-22T15:09:32.920 回答
0

这可能是您的视频文件为空的原因。

bool detection = false;
...
if (detection == true)
{
    record.write(frame);
}
于 2022-01-14T11:59:29.990 回答