来自 AVI 的 OpenCV 帧捕获

OpenCV frame capture from AVI

本文关键字:OpenCV AVI 来自      更新时间:2023-10-16

我正在用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("%in",posFrame);
              cvWaitKey(0);
              cvDestroyWindow("Image:");
    }

为什么不使用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;
}

这样的事情应该有效。