首页 文章

黑屏视频捕获opencv

提问于
浏览
4

我正在尝试测试一个非常简单的程序来使用相机捕获视频,但似乎窗口始终是黑色的 . 相机的指示灯已打开,程序编译得很好 .

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv; 
using namespace std;

int main() {
VideoCapture stream1(0);   //0 is the id of video device.0 if you have only one camera.

if (!stream1.isOpened()) { //check if video device has been initialised
    cout << "cannot open camera";
}

//unconditional loop
while (true) {
    Mat cameraFrame;
    stream1.read(cameraFrame);
    imshow("cam", cameraFrame);
    if (waitKey(30) >= 0)
        break;
}
system("pause");
return 0;
}

2 回答

  • 0

    要缩小问题的根源,请按以下步骤操作:

    • 检查是否正确配置了OpenCV highgui . 使用捕获已保存的视频
    VideoCapture stream1("video.avi");
    stream1.read(cameraFrame);
    

    在cameraFrame上执行imshow .

    • 如果仍然出现黑屏,请将 stream1.read(cameraFrame); 替换为 stream1>>cameraFrame; 如果您现在可以看到您的视频,则表示OpenCV highgui配置正确,并且您正在使用的相机可能存在问题 .

    • 在这种情况下,主摄像头驱动程序通常不会授予对第三方库,OpenCV的访问权限 . 将 VideoCapture stream1(0) 替换为 VideoCapture stream1(1) . 现在,这将指向机器的基本凸轮驱动器,而不是主凸轮驱动器 .

    • 如果黑屏仍然存在,我会建议使用外部网络摄像头进行测试,如果可能的话,问题可能出在相机硬件本身

  • -1

    我有同样的问题,并通过更换解决了它

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

    if( (char)waitKey(10) == 'q' )
         break;
    

相关问题