【问题标题】:OpenCV frame capture from AVI从 AVI 捕获 OpenCV 帧
【发布时间】:2012-05-21 02:55:09
【问题描述】:

我正在使用 openCV 2.2 开发一个项目。我需要对 AVI 文件的每一帧进行处理,但是当我运行我的代码时,它只抓取文件的第一帧。 CV_CAP_PROP_POS_FRAMES 似乎没有工作。有什么想法为什么不呢?

    CvCapture* capture = cvCaptureFromAVI("test1.avi");

    IplImage *img = 0;

    if (!cvGrabFrame(capture)) {
            printf("Error: Couldn't open the image file.\n");
            return 1;
    }

    int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
    int posFrame = 1;
    for(int i =0; i <= numFrames; i++){
        cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, i);
              posFrame = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);

              img = cvGrabFrame(capture);
              cvNamedWindow("Image:", CV_WINDOW_AUTOSIZE);
              cvShowImage("Image:", img);
              printf("%i\n",posFrame);

              cvWaitKey(0);

              cvDestroyWindow("Image:");
    }

【问题讨论】:

  • 你为什么不用 2.3.1 或 2.4?
  • 我用 opencv 2.3.1 测试过,问题仍然存在。

标签: c++ opencv frames avi


【解决方案1】:

您为什么不使用 OpenCV 2.3 尝试这种方式?我认为它更直接,更有效,更清晰:

VideoCapture _videoSource;

if(!_videoSource.open("test1.avi")) 
{
   exit(1);         // Exit if fail
}
_videoSource.set(CV_CAP_PROP_CONVERT_RGB, 1);

Mat frame;
namedWindow("Image");
int posFrame;

while(1) 
{
  _videoSource >> frame;
  posFrame=_videoSource.get(CV_CAP_PROP_POS_FRAMES);
  imshow("output", frame);
  return 0;
}

这样的事情应该可以工作。

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2021-11-26
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 2012-05-13
    • 1970-01-01
    相关资源
    最近更新 更多