简体   繁体   中英

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.

If that is the problem you can go to Settings -> General Settings -> Exclusions -> Scan exclusions and trusted applications and click Settings . Then go to the Trusted Applications tab and click on Add -> Applications . Search for Python and click Ok and check all the boxes. 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. Capture a saved video using

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

    perform imshow on cameraFrame.

-If you still get a black screen, replace stream1.read(cameraFrame); with 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.

  • Often the primary camera driver does not grant access to third party libs, OpenCV in this case. Replace VideoCapture stream1(0) with 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). Saransh Kejriwa's comment worked regarding 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.

I had the same problem and solved it by replacing

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

with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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