0

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

Nikita Belov
  • 85
  • 1
  • 9
  • Can you please add a if(frame.empty()) after the capture to handle cases where capturing failed? – Micka Feb 28 '15 at 21:22
  • And test whether capture object was opened succesfully! – Micka Feb 28 '15 at 21:23
  • I have added if(frame.empty()) . It's not empty... Any ideas? – Nikita Belov Mar 01 '15 at 16:22
  • if the image isnt empty I dont see any problem (afaik device -1 should work too, but not sure about the C interface)... Please try `cv::VideoCapture capture(-1); if(!capture.isOpened()) return 0; ` instead of `cvCaptureFromCAM` and `if(!capture.retrieve(frame)) return 0;` instead of `cvQueryFrame` lines... – Micka Mar 02 '15 at 10:13
  • do you known the format of your input images produced by the capture? maybe its BGRA instead of BGR? Can you try to remove the cvtColor and the canny operation and instead save the images to disk with `cv::imwrite` or just display the images without any previous operation? – Micka Mar 02 '15 at 10:16
  • Could the fucntion cvQueryFrame be the reason? I have found this: The returned image should not be released or modified by the user. – Nikita Belov Mar 03 '15 at 15:36
  • please try to avoid the cvXXX functions because they use C interface. switch to `cv::` functions and objects! Could you remove the whole cvtColor and canny operations and just `cv::imshow` your image? if that works, the problem is definitely not the capturing. If that doesnt work, the capturing is your problem! – Micka Mar 03 '15 at 15:55
  • I find a dependence. If frame is a cv::Mat, I have a problem with capturing and showing (frame is really empty). So, how to find out the type of capturing video and create a mat frame, which have the same format? – Nikita Belov Mar 06 '15 at 15:49
  • so you capture a frame but it is black? sounds like an Alpha channel... please print mat.channel() and mat.cols() and mat.rows() – Micka Mar 06 '15 at 16:34
  • It's zero. frame = cvQueryFrame(capture); If frame is mat, everything is zero. If it's IplImage* it works correct and function cvShowImage shows video from camera. But in this case cvcvtColot doesn't work. I have found the simple problem: http://stackoverflow.com/questions/4430367/opencv-webcam-capture-problem However, their solution do not help – Nikita Belov Mar 08 '15 at 13:17
  • Please try to use cv::Videocapture object!! – Micka Mar 08 '15 at 13:50
  • I use it. However, frame is still empty – Nikita Belov Mar 08 '15 at 14:42
  • I have tried about 6-8examples and in all cases I have a problem with cvcvtcolor.... I despair – Nikita Belov Mar 08 '15 at 21:33
  • if the frame is empty (cols == 0 or rows == 0) it's obvious that cvtColor crashes. if it is not empty, please tell me what mat.channels is equal to!! – Micka Mar 09 '15 at 07:53
  • mat.channels equals 1. mat.rows=0 and mat.cols=0 – Nikita Belov Mar 09 '15 at 08:39
  • so your capturing just doesnt work. no need at all to look at cvtColor or canny until you are able to capture the images! which device do you try to use for capturing? – Micka Mar 09 '15 at 12:07
  • I have 3 different web cams. The first camera is integrated into the laptop. Two other are Intro digital PC cam. As I said if I use IplImage as the format of images, it's work and I see the result. Is it possible that the case in getting new frame? Now I use this structure: cap >> frame; – Nikita Belov Mar 09 '15 at 17:20

3 Answers3

1

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.

Viatorus
  • 1,804
  • 1
  • 18
  • 41
  • Same error... I have tried canny for usual images and it works properly. I suppose there are some problems with format or size of images... – Nikita Belov Mar 01 '15 at 15:55
0

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.

  • Same error... I have tried canny for usual images and it works properly. I suppose there are some problems with format or size of images... – Nikita Belov Mar 01 '15 at 15:56
  • At what function exactly do you face the exception? Do you access properly your camera at all? – Plamen Tetevensky Mar 01 '15 at 18:24
  • I am quite sure that cvtColor(out, out2, CV_BGR2GRAY); Cause program stops at the next line. And no matter what is it. I have tried another example of Canny edge and the same error after the same string. I have a acceess to camery. I tried another examples with using camera and it's work properly – Nikita Belov Mar 01 '15 at 23:46
  • please print the number of channels and type, directly after capturing. – Micka Mar 02 '15 at 10:20
  • I suggest, also, using C headers, not C++: `#include #include ` – Plamen Tetevensky Mar 02 '15 at 11:39
  • Please, suggest which fuctions will be better to use for capturing number of channels and type? Now, I'm using CvCapture* capture = cvCaptureFromCAM(0); for capture Could I use cvGetCaptureProperty? If yes, I don't know which flag should I use? – Nikita Belov Mar 03 '15 at 15:11
0

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;
}
Micka
  • 19,585
  • 4
  • 56
  • 74
  • Error 1 error C2039: 'isOpen' : is not a member of 'cv::VideoCapture' I have commented if(!capture.isOpen()) condition. CApture window is grey. No errors or exceptins – Nikita Belov Mar 10 '15 at 22:33
  • typo, must be .isOpened () – Micka Mar 11 '15 at 05:42
  • could not capture a legal frame – Nikita Belov Mar 11 '15 at 09:07
  • 1
    I have another laptop with Win 7 x64, same version of VS, opencv e.t.c. And everything works perfect. – Nikita Belov Mar 11 '15 at 17:54
  • I find a regularity. This program works correct only first time after restart PC. At second and further it write "could not capture a legal frame" Could it be the problem with destructor? – Nikita Belov Mar 15 '15 at 17:48
  • can you please remove the break and add a continue there instead? – Micka Mar 15 '15 at 19:07
  • It works!! Magic... I see "could not capture a legal frame" in the console, but I see capture from camera. Thank you!! Could you explain this, please? – Nikita Belov Mar 15 '15 at 22:56
  • maybe your device does not provide legal frames in each iteration but once in a while... but I have no idea why this happens. I never had that problem myself. – Micka Mar 16 '15 at 05:04