Nikita Belov
Nikita Belov

Reputation: 85

Unhandled exception at Canny edge detector

I want to try Canny edge detector, but when I try to start I receive an Unhandled exception:

Unhandled exception at 0x00007FF97F6C8B9C in canny_project.exe: Microsoft C++ exception: cv::Exception at memory location 0x0000002485D89860

Below is the code that I implemented In 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;
}

Thanks in advance

Upvotes: 0

Views: 411

Answers (3)

Micka
Micka

Reputation: 20160

Please try this instead and tell me whether images are shown or not and try different device numbers too:

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

continue; //break; } cv::imshow("Capture", 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;
}

Upvotes: 0

Plamen Tetevensky
Plamen Tetevensky

Reputation: 702

cvCaptureFromCAM(-1) has wrong argument, use 0, if you have just one camera connected. In addition, in C API, when you finished working with video, release CvCapture structure with cvReleaseCapture(), or use Ptr<CvCapture> that calls cvReleaseCapture() automatically in the destructor. Have a try, please, this example, to see if you access your camera properly.

Upvotes: 0

Viatorus
Viatorus

Reputation: 1903

The problem is probably you are using cvCaptureFromCAM wrong.

cvCaptureFromCAM(0) // not -1

Why do you use OpenCV with C-Code? Use VideoCapture instead CvCapture.

Upvotes: 1

Related Questions