简体   繁体   中英

OpenCV - unhandled exception in Videocapture

I recently installed OpenCV 2.4.7 and configured it to my Visual Studio 2010 Ultimate ide... i even tested a code to display an image...

#include "opencv2/highgui/highgui.hpp"
#include "iostream"

using namespace cv;
using namespace std;

int main()
{
    Mat im = imread("d:/lena.jpg");
    if (im.empty()) 
    {
        cout << "Cannot load image!" << endl;
        return -1;
    }
    imshow("Image", im);
    waitKey(0);
}

and it works but when I try to use the videocapture code given here , it gives an error..

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

Unhandled exception at 0x75dc812f in myNewOpenCv1.exe: Microsoft C++ exception: cv::Exception at memory location 0x0019f6d8

i don't know whether its a problem with the installation or not. I'm very new to OpenCV and don't know much so if anyone who is well used to this can fix this error for me and also give me an explanation as to why it happened and a guidance in this would be great.

Hopefully waiting for your answers - Jonathan -

Try to replace

cap >> frame;

with:

while (frame.empty()) {
    cap >> frame;
}

Sometimes opencv camera API gives garbage for the first few frames, but after a while everything works.

You may want to limit that loop to a fixed number of iterations to avoid running infinitely.

The below lines of code is used for edge detection only .

cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);

So,If you are interested in just video capture then use below code :

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        imshow("display", frame);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

To run this code ,you should have set the library path in VS,as well as you should set the dll in linker option in VS.It will work !!!

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