跳帧问题(OpenCv)

时间:2016-05-13 04:27:04

标签: opencv video-capture

我正在尝试在opencv上播放视频。但是,该程序只显示视频的最后一帧。谁能告诉我为什么会这样呢?以下是我的代码。

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

#include<iostream>
#include<conio.h>           // it may be necessary to change or remove this line if not using Windows

使用命名空间cv; using namespace std; ////////////////////////////////////////////////// ///////////////////////////////////////////////// int main(void){

cv::VideoCapture capVideo; /*add a video capture object*/

cv::Mat imgFrame; /*add  a picture object to capture each frame*/

capVideo.open("768x576.avi"); /*open the video*/

if (!capVideo.isOpened()) {                                                 // if unable to open video file
    std::cout << "\nerror reading video file" << std::endl << std::endl;      // show error message
    _getch();                    // it may be necessary to change or remove this line if not using Windows
    return(0);                                                              // and exit program
}

/*this shows the number of frame will be given*/
if (capVideo.get(CV_CAP_PROP_FRAME_COUNT) < 1) {
    std::cout << "\nerror: video file must have at least one frame";
    _getch();
    return(0);
}

/*printing out the number of frame*/
cout<<"the number of frame:" << capVideo.get(CV_CAP_PROP_FRAME_COUNT) << endl;


char* videoTitle = "this is displaying the video";
cv::namedWindow(videoTitle, WINDOW_AUTOSIZE);
capVideo.read(imgFrame); // it will return a boolean value
int i = 0;
while (1)
{
    if (capVideo.read(imgFrame) == false)
    {
        break;
    }
    imshow(videoTitle, imgFrame);
    //it read every frame of the video

    i++;
    cout << "Frame Number: " << i << endl;  
}
cout << "End of the video" << endl;

waitKey(0);
return(0);

}

1 个答案:

答案 0 :(得分:0)

我将修改while循环,如下所示。

while (capVideo.isOpened() && chCheckForEscKey != 27) {

        imshow("imgFrame", imgFrame);// now we prepare for the next iteration

        if ((capVideo.get(CAP_PROP_POS_FRAMES) + 1) < capVideo.get(CAP_PROP_FRAME_COUNT)) {       // if there is at least one more frame
            capVideo.read(imgFrame);                            // read it
        }
        else {                                                  // else
            cout << "end of video\n";                      // show end of video message
            break;                                              // and jump out of while loop
        }

        chCheckForEscKey = waitKey(1);      // get key press in case user pressed esc

    }

    if (chCheckForEscKey != 27) {               // if the user did not press esc (i.e. we reached the end of the video)
        waitKey(0);                         // hold the windows open to allow the "end of video" message to show
    }
    // note that if the user did press esc, we don't need to hold the windows open, we can simply let the program end which will close the windows

    return(0);
}