c++ - 每 2 秒捕获帧

标签 c++ opencv image-processing frame-rate

我正在做一个关于视频中人脸检测的项目。我从视频中检测到人脸,但它正在捕获每一帧,因此我在一秒钟内获得了如此多的图像(一秒钟内捕获了如此多的帧) 。

问题:我想减少这个问题,每 3 秒捕获帧就足够了。我尝试使用 wait()sleep() 函数。但他们只是停止播放视频一段时间,没有发生其他事情。任何人都可以帮助我克服这个困难吗?

    #include <cv.h>   
    #include <highgui.h>
    #include <time.h>
    #include <stdio.h>
    using namespace std;
    IplImage *frame;
    int frames;
    void facedetect(IplImage* image);
    void saveImage(IplImage *img,char *ex);
    IplImage* resizeImage(const IplImage *origImg, int newWidth,int newHeight, bool      keepAspectRatio);
    const char* cascade_name="haarcascade_frontalface_default.xml";//specify classifier     cascade.
    int k;
    int main(int argc, char** argv)
    {
      // OpenCV Capture object to grab frames
      //CvCapture *capture = cvCaptureFromCAM(0);
      CvCapture *capture=cvCaptureFromFile("video.flv");
      //int frames=cvSetCaptureProperty(capture,CV_CAP_PROP_FPS, 0.5);
      //double res1=cvGetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES);
      //cout<<"res"<<res<<endl;
      // start and end times
       time_t start, end;
       // fps calculated using number of frames / seconds
       double fps;
      // frame counter
      int counter = 0;
      // start the clock
     time(&start);
     //while(cvGrabFrame(capture))
     while(1)
     {
    //if(capture.get(CV_CAP_PROP_POS_FRAMES) % 2 == 0)
    frames=cvSetCaptureProperty(capture,CV_CAP_PROP_FPS, 0.5);
    if(frames%2==0)
    {
    frame = cvQueryFrame(capture);
    cout<<"Frame"<<frame<<endl;
    facedetect(frame);
    }

     }

  cvReleaseCapture(&capture);
  return 0;
}

在捕获每一帧后,我给出了 cvWaitKey(2000)。

最佳答案

这将是我的考验。每 30 帧保存一张图像。当你说一秒钟内有太 multimap 像时,我理解你指的是保存的面孔。

int counter = 0;
// start the clock
time(&start);
//while(cvGrabFrame(capture))
while(1)
{
  frame = cvQueryFrame(capture);
  cout<<"Frame"<<frame<<endl;
  if(count%30==0)
  {
   facedetect(frame);
  }
  count++;
}

如果您确实想跳过帧,请尝试此操作。每秒一帧可能是以下代码的结果。

while(1)
{
 if(count%30==0)
  { 
   frame = cvQueryFrame(capture);
   cout<<"Frame"<<frame<<endl;
   facedetect(frame);
  }
  count++;
}

关于c++ - 每 2 秒捕获帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21951433/

相关文章:

C++11如何打印出高分辨率时钟time_point

c++ - 调用 cin.get() 两次 : unexpected result?

c++ - QThread线程池

python - OpenCV 2.4 VideoCapture 在 Windows 上不起作用

python - 阈值处理后图像变黑

Python:图像处理创建皱纹纸效果

c++ - Operator[] 重载返回错误数据

c++ - Opencv-2.4.5 是否预先启用了 TBB?

python - 如何在 Python 中使用 OpenCV 对图像进行像素化?

image - 对 1000 张图像进行聚类以找到具有更大相似性的图像组