OpenCV frame capture from AVI

偶尔善良 提交于 2019-12-30 06:51:06

问题


I am working on a project with 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. The CV_CAP_PROP_POS_FRAMES does not seem to be working. 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:");
    }

回答1:


Why don't you try this way using 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.



来源:https://stackoverflow.com/questions/10569510/opencv-frame-capture-from-avi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!