【问题标题】:OpenCV C++ doesn't return false when capture is not working捕获不工作时,OpenCV C++ 不返回 false
【发布时间】:2016-12-01 15:08:36
【问题描述】:

我正在使用 IP 摄像机,需要知道捕获失败的时间。根据 OpenCV 文档,VideoCapture::read(OutputArray image) 应在未抓取任何帧时返回 false(相机已断开连接,或没有更多帧)在视频文件中),但这没有发生。当我的相机断开连接时,我的代码卡在了读取功能中。我需要知道捕获失败的时间。如何才能做到这一点?我的代码如下:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    String ptzCam = "rtsp://user:password@ip:port/videoMain";

    VideoCapture cap(ptzCam);  // open the default camera
    if (!cap.isOpened()) {
        return -1;
    }

    namedWindow("capture", 1);

    for (;;) {
        Mat frame;

        if (cap.read(frame)) {
            cout << "Capturing... " << endl;
            imshow("capture", frame);           
        }
        else {
            cout << "Capture is not opened." << endl;           
        }   

        if (waitKey(30) >= 0)
            break;
    }

    cap.release();
    destroyAllWindows();
}

【问题讨论】:

  • 是冻结(可能是驱动程序问题)还是没有返回图像?你可以测试 if(frame.empty()) doSomething;在后一种情况下。
  • 它冻结并且代码卡在 cap.read(frame) 中。将尝试检查框架是否为空并尝试更新驱动程序。感谢您的提示。
  • 如果冻结,您无法检查结果是否为空(因为结果永远不会发生)。我想这是您相机的驱动程序问题,而不是严格意义上的 openCV 问题。可能你什么都做不了……你用的是哪个操作系统?
  • 我使用的是 Windows 7。
  • 您可以尝试使用InputLibrary的测试应用程序打开相机,并检查如果在那里拔下相机是否会出现相同的行为。或者 DirectShow、VLC 或其他可以打开相机的应用程序也是如此。

标签: c++ image opencv image-processing


【解决方案1】:

你应该试试下面的代码:

cv::VideoCapture cap;
cv::Mat frm;

cap.open(0);
if (!cap.isOpened())
    // error handling
while (true) {
    if (!cap.grab() || !cap.retrieve(frm) || frm.empty()) {
        // error handling
    }
    // cv::imshow and cv::waitKey or any other frm manipulations
}

您可以在这里了解 cv::VideoCapture 成员函数:cv::VideoCapture::grab(), cv::VideoCapture::retrieve()

【讨论】:

  • 能否解释一下 cap.grab() 和 cap.retrieve 的作用?
【解决方案2】:

我自己目前正在玩 OpenCV,我试图弄清楚如何处理断开连接然后重新连接 USB 摄像头。当我在拔下 USB 后使用cap.isOpened() 函数时,我注意到没有任何改变返回值。拔下相机后,我还注意到尝试读取帧会导致崩溃。这使得我无法重新插入相机并再次播放。但是下面的代码使我可以断开/重新连接相机而不会导致程序崩溃。

while (true) { //loop for showing camera footage
    if (cap.read(frame)) { //check if there is an frame to read
        imshow("something", frame); //show the frame
        if (waitKey(1000 / 60) >= 0) 
            break;
    }else{ //if you can't read the frame (because camera is disconnected) enter an loop
        while (true) {
            VideoCapture newCapture(0); //create an new capture
            if (!newCapture.isOpened()) //check if the new capture is open or not
                cout << "camera is unavailable" << endl;
            else {
                cout << "camera seems available" << endl;
                cap = newCapture; //if the new capture is open set it as the origional capture
                break; //break from the loop
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2011-04-25
    • 2011-08-27
    相关资源
    最近更新 更多