简体   繁体   中英

Status of VideoCapture object in OpenCV

I have encapsulated the OpenCV VideoCapture class into a class of my own and am trying to initialize it from a video file on disk. The .isOpened() function returns true , but the frames I read later on are empty.

  • If the VideoCapture can't read frames, why is it reporting being open ?
  • How can I further inspect the status of the VideoCapture object to find out why it's not reading frames?

Here is a tiny excerpt. Init() later throws an error because the frames read by camera_.read() are empty. Better scroll through to the code at the bottom, it exemplifies the problem better.

Camera::Camera(int target_width, int target_height, std::string filename)
    : target_width_(target_width),
      target_height_(target_height),
      camera_(filename)
{
    if (!camera_.isOpened()){
        cerr << "Failed to open video file" << endl;
        exit(EXIT_FAILURE);
    }
    camera_.read(frame_full_);
    if (frame_full_.empty()){
       cerr << "Failed to read from video file" << endl;
       exit(EXIT_FAILURE);
    }
    Init();
}

EDIT:

So, reading from an actual webcam works fine, it is the code above (reading from a file by filename) that returns empty frames.

EDIT2:

So—as suggested by Mhd.Tahawi—my whole problem can be summarized by the code below:

cv::VideoCapture cap("/Users/Me/Projects/whatev/some_video__640x360.avi");
cv::Mat test;

if (cap.isOpened()){
    cap.read(test);
    if (test.empty()) {
        std::cout << "BRO DO YOU EVEN CAPTURE?" << std::endl;
    }
}

I suspect codec problems - your code looks fine.

On a Mac, use brew install opencv --with-ffmpeg .

(But double check if you really have a the correct filepath, even though it shouldn't say that it is open). If you are on windows you need "\\" in your path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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