0

As I am completely new to OpenCV, I tried following a tutorial to capture video from a webcam. I used the following code:

#include "opencv2\opencv.hpp"
#include <stdint.h>

using namespace cv;
using namespace std;

int main(int argv, char** argc) {
    Mat frame;
    VideoCapture vid(0);    //0 if we only have one camera
    if (!vid.isOpened()) {  //check camera has been initialized
        cout << "ERROR: Cannot open the camera";
        return -1;
    }

    while (vid.read(frame)) {
        imshow("webcam", frame);
        if (waitKey(30) >= 0) break;
    }
    return 0;
}

When I run the program, a window that shows the video feed opens, but it then almost immediately closes. I get this snippet from the Debug output:

SETUP: Device is setup and ready to capture.

Event: Code: 0x0d Params: 0, 0
Event: Code: 0x0e Params: 0, 0
Exception thrown at 0x000007FEFCBAA06D in test.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000026EB10.

SETUP: Disconnecting device 0

I don't know what could be wrong with my code or even how to begin debugging this. Any advice? Thank you!

6
  • Are you running in release or debug mode? Commented Jun 27, 2017 at 17:24
  • 1
    And have you stepped through your code with a debugger to see if at a particular line the exception is thrown? Commented Jun 27, 2017 at 17:25
  • This might be useful (and possible duplicate): stackoverflow.com/questions/42221213/… Commented Jun 27, 2017 at 17:35
  • @Javia1492 I was running in debug mode. I used the debugger and found that the exception was being thrown at the imshow line. I switched the while loop to a while(1) and read the image in it, but it didn't work until I also changed the waitKey line to be waitKey(30) == 27. I'm not sure why this worked. Thanks though! Commented Jun 27, 2017 at 19:18
  • Try copying the frame before using it. Commented Jun 27, 2017 at 19:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.