2

I am trying to code an openCV program that opens a new widow that displays video from the webcam (laptop), and recieve the error Unhandled exception at 0x7530C41F in cv.exe: Microsoft C++ exception: cv::Exception at memory location 0x001FF5D0.

i have double checked all of the included dll's as well as the system path without any positive results.

here is my code:

#include <opencv\cv.h>
#include <opencv\highgui.h>
using namespace cv;
int main()
{

    Mat image;

    VideoCapture cap;
    cap.open(0);

    namedWindow("window", CV_WINDOW_AUTOSIZE);

    while (1)
    {
        cap >> image;
        imshow("window", image);
        waitKey(33);
    }
    return 0;
}
3
  • Did your camera open when running this code? Commented Apr 13, 2014 at 4:17
  • most likely the cap.open fails, and then cap >> image crashes. you can set up an exception handler and inspect the exception message. but easiest here is probably just to single-step in the debugger. Commented Apr 13, 2014 at 9:14
  • @herohuyongtao , no the window did fully open, it got stuck in a load. also the webcam light did not turn on.., but my laptops default webcam viewer opened dell webcam central and it showed a single frame from the webcam, never more than one, Commented Apr 13, 2014 at 20:28

2 Answers 2

2

I discovered the problem with my code, i had to put a hold on the execution of the program using waitKey(1000) or else the program will cause a memory leak. This must have been done because the connection to the webcam on my laptop was not fully established before executing the code. Putting a wait on the code execution prevents the crash and everything is now up and running.

  #include <opencv\cv.h>
    #include <opencv\highgui.h>
    using namespace cv;
    int main()
    {

        Mat image;

        VideoCapture cap;
        cap.open(0);

        namedWindow("window", CV_WINDOW_AUTOSIZE);
            waitKey(1000);

        while (1)
        {
            cap >> image;
            imshow("window", image);
            waitKey(33);
        }
        return 0;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps using VideoCapture::isOpened() would tell you if it's safe to use your VideoCapture instance?

Comments

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.