openCV读取.avi文件的视频捕获获取空框架

OpenCV read VideoCapture of .avi file get empty frame

本文关键字:获取 框架 的视频 读取 avi 文件 openCV      更新时间:2023-10-16

我是OpenCV的新手,并尝试运行一个简单的程序,该程序读取.avi文件并逐帧在窗口中显示IT。我的系统是OSX El Capitan。这是我的代码:

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main( int argc, const char** argv )
{
    Mat frame;
    VideoCapture cap("crs01.avi");
    if(!cap.isOpened()){  // check if we succeeded
        cout << "Video not loaded!!" << endl;
        return -1;
    } else {
        cout << "Loaded!!" << endl;
    }
    int count = cap.get(CV_CAP_PROP_FRAME_COUNT);
    for(int i = 0; i < count; i++){
//    while(true) {
        cap >> frame;
        if(!frame.empty()){
            imshow("edges", frame);
        } else {
            cout << "EMPTY" << endl;
        }
        if(waitKey(30) >= 0)
            break;
    }
    cap.release();
    return 0;
}

但是,该程序总是会返回我空的框架。它可以打印"已加载",但之后它不断打印"空"。我已经在内置摄像头上尝试了此程序,即Videocapture Cap(0);以及读取其他.Avi文件,并且在两种情况下都可以正常工作。我还试图运行一个调试器,事实证明,"盖帽>>帧"行之后的框架确实是空的。

有人可以帮助我吗?这个问题与MAC系统有关系吗?预先感谢!

这是另一种做到的方法。

cv::VideoCapture mycap;
std::string fileName="PATH_TO_FILE//FILENAME";
mycap.open(fileName);       
int totalFrameNumber = (int)mycap.get(CV_CAP_PROP_FRAME_COUNT);
cv::Mat newImg;
for (int counter = 0; counter<totalFrameNumber; counter++){
    bool frameIsCorrect = mycap.read(newImg);
    if (new_Img.cols>0)
    {
        cv::imshow("frame", newImg);
        cv::waitKey(1); //or waitKey(0) to pause on every new frame for a key press
    }
}