Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting opencv error in c++

Tags:

c++

opencv

I'm try to get the error of opencv! say I have this program:

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

int main (){
    cv::Mat frame;
    cv::VideoCapture cap(1); // I don't have a second videoinput device! 
    int key = 0; 

    while(key !=27){
        cap >> frame;
        cv::imshow("frame",frame);
        key = cv::waitKey(10);
    }

    cap.release();
    return 0;
}

when I run this program I get in the console this message :

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown functi
on, file ..\..\..\opencv\modules\highgui\src\window.cpp, line 261

My question is how can I get this message and save it in a string for every error that I get! and if it'S possible escaping the program crash!

thanks in advance!

like image 412
Engine Avatar asked Oct 21 '13 07:10

Engine


1 Answers

It uses C++ exceptions. See here in the doc for more.

try
{
    ... // call OpenCV
}
catch( cv::Exception& e )
{
    const char* err_msg = e.what();
    std::cout << "exception caught: " << err_msg << std::endl;
}

A CV_Assert in the OpenCV code is a macro which calls the OpenCV function error. That function can be seen here. It will always print the error text on stderr unless you don't have the customErrorCallback set. You do that via cvRedirectError, see here.

like image 137
Albert Avatar answered Oct 14 '22 08:10

Albert