简体   繁体   English

黑屏视频捕获opencv

[英]black screen video capture opencv

I am trying to test a very simple program to capture a video using camera, but it seems like the window is always black.我正在尝试测试一个非常简单的程序来使用相机捕获视频,但似乎窗口总是黑色的。 The camera's led is turned on, and the program is compiled just fine.摄像头的灯亮了,程序编译就好了。

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

I had the same problem and I found out that Kaspersky was blocking access to my camera.我遇到了同样的问题,我发现卡巴斯基阻止了对我的相机的访问。 If you open Kapresky and go to Reports and then to the Host Intrusion Prevention column under the Advanced Thread Protection tab you can find if it blocks your camera.如果您打开 Kapresky 并转到“报告”,然后转到“高级线程保护”选项卡下的“主机入侵防护”列,您可以查看它是否阻止了您的摄像头。

If that is the problem you can go to Settings -> General Settings -> Exclusions -> Scan exclusions and trusted applications and click Settings .如果这是问题所在,您可以转到Settings -> General Settings -> Exclusions -> Scan exclusions and trusted applications ,然后单击Settings Then go to the Trusted Applications tab and click on Add -> Applications .然后转到Trusted Applications tab并单击Add -> Applications Search for Python and click Ok and check all the boxes.搜索Python并单击确定并选中所有框。 Click Ok and then Save and it should work.单击确定,然后保存,它应该可以工作。

To narrow down the source of the problem, here's how you can proceed:要缩小问题的根源,您可以按照以下步骤进行操作:

  • Check if OpenCV highgui is configured correctly.检查 OpenCV highgui 是否配置正确。 Capture a saved video using使用捕获保存的视频

    VideoCapture stream1("video.avi"); stream1.read(cameraFrame);

    perform imshow on cameraFrame.在 cameraFrame 上执行 imshow。

-If you still get a black screen, replace stream1.read(cameraFrame); -如果还是黑屏,更换stream1.read(cameraFrame); with stream1>>cameraFrame;stream1>>cameraFrame; If you can now see your video, that means that OpenCV highgui is configured correctly, and there may be an issue with the camera you're using.如果您现在可以看到您的视频,则意味着 OpenCV highgui 配置正确,并且您使用的相机可能存在问题。

  • Often the primary camera driver does not grant access to third party libs, OpenCV in this case.通常,主相机驱动程序不会授予对第三方库(在本例中为 OpenCV)的访问权限。 Replace VideoCapture stream1(0) with VideoCapture stream1(1) .VideoCapture stream1(0)替换为VideoCapture stream1(1) This will now point to the basic cam driver of your machine, instead of the primary cam driver.现在这将指向您机器的基本凸轮驱动程序,而不是主凸轮驱动程序。

  • If the black screen persists, I would suggest using an external webcam for testing, if possible, and the issue might be in the camera hardware itself如果黑屏仍然存在,如果可能的话,我建议使用外部网络摄像头进行测试,问题可能出在相机硬件本身

Encountered similar problem (but using Python).遇到类似的问题(但使用Python)。 Saransh Kejriwa's comment worked regarding DSHOW. Saransh Kejriwa 的评论对 DSHOW 有效。 In case anyone stumbles upon this:万一有人偶然发现:

fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
cap = cv2.VideoCapture()
cap.open(1 + cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FOURCC, fourcc)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
cap.set(cv2.CAP_PROP_FPS, 60)

I was having same issue, after trying all solutions I found that my webcam resolution was higher.我遇到了同样的问题,在尝试了所有解决方案后,我发现我的网络摄像头分辨率更高。

Fixed it by changing 1280 * 720 to 640 * 480.通过将 1280 * 720 更改为 640 * 480 来修复它。

I had the same problem and solved it by replacing 我有同样的问题,并通过替换解决了

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM