Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Blue screen when using VideoCapture

I'm accessing the front and back cameras of my surface pro 4. When I run my code the program usually get stuck at the following line and I get a bluescreen after a while.

std::VideoCapture vidCap(1);

Strangely it doesn't always happen. Sometimes (about 1 out of 5 tries) the programm works perfectly fine and I can see my camera stream. Do you guys have ideas what I can do?

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

int main()
{
    const char* windowName = "Webcam Stream";
    cv::namedWindow(windowName, CV_WINDOW_AUTOSIZE);
    cv::Mat frame;
    cv::VideoCapture vidCap(1);

    while (vidCap.isOpened()) {
        vidCap >> frame;

        cv::cvtColor(frame, frame, CV_BGR2GRAY);
        cv::imshow(windowName, frame);

        int k = cv::waitKey(33);
        if (k == 27) {
            break;
        }
    }
    return 0;
}