简体   繁体   English

来自AVI的OpenCV帧捕获

[英]OpenCV frame capture from AVI

I am working on a project with openCV 2.2. 我正在使用openCV 2.2开展一个项目。 I need to do processing on each frame of an AVI file but when I run my code it only grabs the first frame of the file. 我需要对AVI文件的每一帧进行处理,但是当我运行我的代码时,它只抓取文件的第一帧。 The CV_CAP_PROP_POS_FRAMES does not seem to be working. CV_CAP_PROP_POS_FRAMES似乎不起作用。 Any ideas why not? 任何想法为什么不呢?

    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:");
    }

Why don't you try this way using OpenCV 2.3? 为什么不尝试使用OpenCV 2.3? I think it is more direct and efficient, and more clear to your eyes: 我认为它更直接,更有效,更清晰:

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

Something like this should work. 这样的事情应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM