使用OpenCV将选定的开始帧和结束帧存储在视频中-C ++

时间:2018-07-12 13:44:02

标签: c++ opencv

我是OpenCV C ++的新手。我想选择视频中的开始帧和结束帧,并将选定的帧存储到名为“ newVideo”的向量中,然后显示它。但是我在将选定的帧存储到向量中时遇到错误。有人可以帮忙检查问题所在和建议的解决方法吗?

 VideoCapture cap(filename);
 if (!cap.isOpened())
{
    cout << "Error opening video stream or file!" << endl;
    return false;
}

 int total_frame = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_COUNT));
 Mat frame, startframe, endframe;
 int startFrameIndex, endFrameIndex;

 cout << "Please enter a starting frame index(0-"<< total_frame-1 <<"):";
    cin >> startFrameIndex;

 cout << "Please enter a ending frame index (" <<startFrameIndex <<"-"<< total_frame-1 <<"):";
   cin >> endFrameIndex;

 cap.set(CV_CAP_PROP_POS_FRAMES, startFrameIndex);
 cap >> frame;  
 startframe = frame.clone();

 cap.set(CV_CAP_PROP_POS_FRAMES, endFrameIndex);
 cap >> frame;  // or can use cap.read(frame)
 endframe = frame.clone();

 imshow("startFrame", startframe); // to check does it is the start frame  matching with the start frame index that you insert.
imshow("endFrame", endframe);   //// to check does it is the end frame  matching with the end frame index that you insert.
waitKey(0);


cap.set(CV_CAP_PROP_POS_FRAMES, startFrameIndex);

int frameCount = 0;
vector <Mat> newVideo;
for (int frameIndex = startFrameIndex; frameIndex < endFrameIndex; ++frameIndex)
{
    cap >> startframe;

    frameCount ++;
    newVideo.push_back(startframe);


    imshow("New Video ", newVideo);
    waitKey(1);

这是提示错误的错误:

 OpenCV Error: Assertion failed (0 <= i && i < (int)v.size()) in 
 cv::_InputArray::getMat_, file C:\builds\master_PackSlave-win64-vc12- 
 shared\opencv\modules\core\src\matrix.cpp, line 1188

0 个答案:

没有答案