【问题标题】:Status of VideoCapture object in OpenCVOpenCV 中 VideoCapture 对象的状态
【发布时间】:2014-03-07 00:35:00
【问题描述】:

我已将 OpenCV VideoCapture 类封装到我自己的类中,并尝试从磁盘上的视频文件初始化它。 .isOpened() 函数返回 true,但我后来读到的帧是空的。

  • 如果VideoCapture 无法读取帧,为什么它报告打开
  • 如何进一步检查 VideoCapture 对象的状态以找出它不读取帧的原因?

这里有一小段摘录。 Init() 稍后会抛出错误,因为 camera_.read() 读取的帧是空的。 最好滚动到底部的代码,它可以更好地说明问题。

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();
}

编辑:

因此,从实际的网络摄像头读取数据可以正常工作,是上面的代码(按文件名从文件读取)返回空帧。

EDIT2:

所以——正如 Mhd.Tahawi 所建议的——我的整个问题可以用下面的代码来总结:

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;
    }
}

【问题讨论】:

  • 你用的是什么相机?
  • 我正在尝试从 文件 中读取以进行调试,Camera 的名称可能有点令人困惑。 Camera实际上有2个构造函数,对应VideoCapture的2个构造函数(使用相机设备和文件名字符串)。
  • 您是否尝试使用VideoCapture 对象而不使用您的包装类?是否给出相同的结果?
  • 从文件读取是否也返回空帧?我的意思是你的相机设备型号是什么?有时可能是因为它与 opencv 不兼容。
  • 考虑将我的评论移至答案以提高我的 SO-Fame ^^

标签: c++ opencv


【解决方案1】:

我怀疑编解码器有问题 - 您的代码看起来不错。

在 Mac 上,使用 brew install opencv --with-ffmpeg

(但请仔细检查您是否真的有正确的文件路径,即使它不应该说它是打开的)。如果您在 Windows 上,则路径中需要“\”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 2013-06-22
    • 2017-08-29
    • 2011-10-03
    • 2018-06-21
    • 2012-08-30
    相关资源
    最近更新 更多