简体   繁体   English

OpenCV - Videocapture中未处理的异常

[英]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... 我最近安装了OpenCV 2.4.7并将其配置为我的Visual Studio 2010 Ultimate ide ...我甚至测试了一个代码来显示图像......

#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 myNewOpenCv1.exe中0x75dc812f处的未处理异常:Microsoft C ++异常:cv ::内存位置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. 我对OpenCV很新,并且不太了解,如果任何习惯这种情况的人都可以为我解决这个错误,并且还会给我一个解释,为什么它会发生,并且这方面的指导会很棒。

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. 有时opencv相机API会为前几帧提供垃圾,但过了一段时间后一切正常。

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 !!! 要运行此代码,您应该在VS中设置库路径,并且您应该在VS中设置链接器选项中的dll。它将起作用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM