1
votes

I'm new to opencv and it's developing. I opened camera feed and i want to take the last frame after the 30s. and save the frame in Mat type object. to ensure that i used imshow to display the used image. but i got errors please find below the code i used.

int main(int argc, char** argv){

    VideoCapture cap(0);

    vector<Mat> frame;
    namedWindow("feed",1);
    for(;;)
    {
        frame.push_back(Mat()); // push back images of Mat type
        cap >> frame.back(); // take the last frame
        imshow("feed", frame);
        if (waitKey(30) >=0) { // wait 30s to take the last
            break;
        }
    }
return(0);

Error

OpenCV Error: Assertion failed (0 <= i && i < (int)v.size()) in getMat, file /Users/waruni/Documents/opencv/modules/core/src/matrix.cpp, line 992
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/waruni/Documents/opencv/modules/core/src/matrix.cpp:992: error: (-215) 0 <= i && i < (int)v.size() in function getMat
1
btw, waitKey() measures milliseconds, not secondsberak
i guess, you wanted: imshow("feed", frame.back()); (you can#t use imshow() with a whole vectorberak

1 Answers

1
votes

As berak mentioned you cannot display a vector of matrices. So, frame.back() is probably what you want. I adapted your programme slightly.

#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>

int main(int argc, char *argv[])
{
  cv::VideoCapture cap(0);
  if(!cap.isOpened())  // check if we succeeded
      return -1;
  std::vector<cv::Mat> frame;
  cv::namedWindow("feed", cv::WINDOW_AUTOSIZE);
  for(;;)
    {
      cv::Mat temp;
      cap >> temp;
      frame.push_back(temp);
      cv::imshow("feed", frame.back());
      if (cv::waitKey(30*1000) >=0) { // wait 30s to take the last
          break;
        }
    }
  return(0);
}