繁体   English   中英

OpenCV - Videocapture中未处理的异常

[英]OpenCV - unhandled exception in Videocapture

我最近安装了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);
}

它工作,但当我尝试使用这里给出的视频捕捉代码时,它会给出一个错误..

#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;
}

myNewOpenCv1.exe中0x75dc812f处的未处理异常:Microsoft C ++异常:cv ::内存位置0x0019f6d8的异常

我不知道它是否与安装有关。 我对OpenCV很新,并且不太了解,如果任何习惯这种情况的人都可以为我解决这个错误,并且还会给我一个解释,为什么它会发生,并且这方面的指导会很棒。

希望等待你的答案 - 乔纳森 -

尝试更换

cap >> frame;

有:

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

有时opencv相机API会为前几帧提供垃圾,但过了一段时间后一切正常。

您可能希望将该循环限制为固定的迭代次数,以避免无限运行。

以下代码行仅用于边缘检测。

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

所以,如果您只对视频捕获感兴趣,请使用以下代码:

#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;
}

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

暂无
暂无

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

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