Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果检测结果为真,则写入/保存视频流_C++_Opencv_Video_Face Detection - Fatal编程技术网

C++ 如果检测结果为真,则写入/保存视频流

C++ 如果检测结果为真,则写入/保存视频流,c++,opencv,video,face-detection,C++,Opencv,Video,Face Detection,我想在检测结果为真后写一个视频流 我使用此链接作为 我的代码实现如下所示: 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",

我想在检测结果为真后写一个视频流

我使用此链接作为

我的代码实现如下所示:

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;
}

您应该释放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;

}
int main(int argc,const char**argv){
垫架;
视频捕获(-1);//打开默认摄像机
如果(!capture.isOpened())
{
printf(“相机无法打开!\n”);
返回-1;
}
捕获>>帧;//获取第一帧的大小
对于(;;)
{
//从相机中获取新帧
捕获>>帧;
//--3.将分类器应用于框架
如果(!frame.empty())
{
检测显示(帧);
}
如果(检测和显示(帧)=真)
{
//录制视频
VideoWriter记录(“MyVideo.avi”,CV_FOURCC('D','I','V','X'),30,frame.size(),true);
如果(!record.isOpened())
{
printf(“VideoWriter无法打开!\n”);
返回-1;
}
//将帧添加到录制的
记录=0)中断;
}
返回0;
}
/**@功能检测和显示*/
/**此函数用于检测人脸绘制一个矩形,检测眼睛和嘴巴并绘制一个圆形*/
bool检测和显示(垫架){
...
...
返回true;
}

尝试另一种编解码器。PIM1可能没有安装在您的机器上。我使用Linux Ubuntu 12.04:/这不是很好的解决方案:VideoWriter record(“MyVideo.avi”、CV_FOURCC('D'、'I'、'V'、'X')、30、frame.size()、true);进入循环。它在循环之外。:)p.s.我解决了它。我拿了记录
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;

}