14

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!

2 Answers 2

28

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.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your answer! it works , is there anyway to disable the output of the error message bellow ?
Just remove the std::cout? You can put it inside a file instead.
no it doesn't work ! the output happens in cv::imshow() because the frame is pointing to NULL value
@Engine: You can do that via cvRedirectError. I extended my answer.
@Albert Hey, my cvWriteFrame routine returns a 0 (meaning it failed, I have observed it returns 1 when I actually was able to write a video). But, I need to extract verbose error messages. It raises no exception and tried redirecting the stderr to stdout from the command line itself to no success. Can you help me with using cvRedirectError in PyOpenCV?Documentation has no help!
|
-1

You have to check whether OpenCV function calls in your code is successfully executed or not. Then you can understand the exact problem. Here is the modified code.

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

    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the video cam" << endl;
         return -1;
    }

    int key = 0; 

    while(key !=27){
        bool bSuccess = cap.read(frame); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read the frame from video cam" << endl;
            break;
        }
        cv::imshow("frame",frame);
        key = cv::waitKey(10);
    }

    cap.release();
    return 0;
}

1 Comment

the problem isn't getting the program running, but having a way to save opencv error messages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.