OpenCV在imShow上崩溃仅适用于USB网络摄像头

OpenCV Crashes on imShow only for usb webcam

本文关键字:适用于 USB 网络 摄像头 崩溃 imShow OpenCV      更新时间:2023-10-16

我正在尝试使用Qt进行openCV设置。 我正在使用Qt creator,尽管我目前只进行C++/opencv调用。 此代码适用于我的集成网络摄像头,但是当我切换到USB相机时,我收到一个Windows错误,告诉我opencv.exe已经崩溃,Qt告诉我我的程序意外完成。 有趣的是,如果我将 imshow 更改为 imwrite,我会在文件中从网络摄像头获得输出,因此捕获似乎可以正常工作,我只是无法显示。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
    VideoCapture cap(0);
    // open the default camera, use something different from 0 otherwise;
    // Check VideoCapture documentation.
    if(!cap.isOpened())
        return 1;
    for(;;)
    {
          Mat frame;
          cap.retrieve(frame);
          if( frame.empty() ) break; // end of video stream
          imshow("this is you, smile! :)", frame);
          if( waitKey(1) == 27 ) break; // stop capturing by pressing ESC
    }
    // the camera will be closed automatically upon exit
    // cap.close();
    return 0;
}  

我猜当你使用你的USB摄像头时,你会将参数更改为VideoCapture cap(1)而不是cap(0)。 cap(1) 是您的 USB 相机驱动程序,可能需要单独安装。尝试 cap(2) 或 cap(3),看看您是否通过 USB 摄像头在 imshow 上获得输出。

如果这没有帮助,请更换

cap.retrieve(frame);

cap>>frame;