Black opencv video capture screen

I am trying to test a very simple program for capturing video using a camera, but it looks like the window is always black. The camera is turned on and the program 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;
}
+4
source share
3 answers

To narrow down the source of the problem, follow these steps:

  • Check if OpenGV highgui is configured correctly. Capture a saved video with

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

    execute imshow on cameraFrame.

-If you still get a black screen, replace it stream1.read(cameraFrame);with stream1>>cameraFrame; If you now see your video, it means that OpenGV highgui is configured correctly, and there may be a problem with the camera used.

  • , OpenCV . VideoCapture stream1(0) VideoCapture stream1(1). , .

  • , - , , .

0

( Python). DSHOW. , - :

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)
0

I had the same problem and it was resolved by replacing

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

with

 if( (char)waitKey(10) == 'q' )
     break;
-1
source

All Articles