Canny边缘探测器未处理的异常

时间:2015-02-28 19:59:37

标签: c++ opencv canny-operator

我想尝试Canny边缘检测器,但是当我尝试启动时,我收到一个未处理的异常:

canny_project.exe中0x00007FF97F6C8B9C处的未处理异常:Microsoft C ++异常:cv ::内存位置的异常0x0000002485D89860

以下是我在VS2012中实现的代码。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
using namespace cv;

int main(int, char**)
{
    namedWindow("Edges", CV_WINDOW_NORMAL);
    CvCapture* capture = cvCaptureFromCAM(-1);

    cv::Mat frame; cv::Mat out; cv::Mat out2;

    while (1) {
        frame = cvQueryFrame(capture);

        GaussianBlur(frame, out, Size(5, 5), 0, 0);
        cvtColor(out, out2, CV_BGR2GRAY); // produces out2, a one-channel image (CV_8UC1)
        Canny(out2, out2, 100, 200, 3); // the result goes to out2 again,but since it is still one channel it is fine

        if (!frame.data) break;
        imshow("Edges", out2);

        char c = cvWaitKey(33);
        if (c == 'c') break;
    }
    return 0;
}

提前致谢

3 个答案:

答案 0 :(得分:1)

问题可能是您使用cvCaptureFromCAM错误。

cvCaptureFromCAM(0) // not -1

为什么将OpenCV与C-Code一起使用?使用VideoCapture代替CvCapture。

答案 1 :(得分:0)

cvCaptureFromCAM(-1)有错误的参数,如果你只连接了一台摄像机,则使用0。此外,在C API中,当您完成视频处理后,使用CvCapture发布cvReleaseCapture()结构,或使用在析构函数中自动调用Ptr<CvCapture>的{​​{1}}。请尝试this示例,看看您是否正确使用相机。

答案 2 :(得分:0)

请尝试使用此功能并告诉我是否显示图像并尝试使用不同的设备编号:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
using namespace cv;

int main(int, char**)
{
    cv::namedWindow("Capture");
    int deviceNum = 0; // please try different device numbers too like -1, 1, 2, ...
    cv:VideoCapture capture(deviceNum); 
    cv::Mat frame;

    if(!capture.isOpened())
    {
        std::cout << "Could not open device " << deviceNum << std::endl;
        return 0;
    }

    while (true) 
    {
        capture >> frame; // = cvQueryFrame(capture);

        //if (!frame.data) break;
        if(frame.empty())
        {
            std::cout << "could not capture a legal frame" << std::endl;

继续;                 //打破;             }             cv :: imshow(&#34; Capture&#34;,frame);

        char c = cv::waitKey(33);
        if (c == 'c') break;
    }
    std::cout << "press any key to exit" << std::endl;
    cv::waitKey(0); // wait until key pressed
    return 0;
}