How to suppress OpenCV error message

后端 未结 1 1265
北荒
北荒 2020-12-11 03:01

I am writing an OpenCV project using g++ and opencv 2.4.6

I have some code like this:

try 
{
    H = findHomography( obj, scene, CV_RANSAC );
}
catch         


        
相关标签:
1条回答
  • 2020-12-11 03:40

    cv::error() is called on every occurrence of an assertion failure. The default behavior is to print the assertion statement to std::cerr.

    You can use the undocumented cv::redirectError() function to set a custom error-handling callback. This will override the default behavior of cv::error(). You first need to define a custom error-handling function:

    int handleError( int status, const char* func_name,
                const char* err_msg, const char* file_name,
                int line, void* userdata )
    {
        //Do nothing -- will suppress console output
        return 0;   //Return value is not used
    }
    

    And then set the callback before the code which throw:

        cv::redirectError(handleError);
    
    try {
        // Etc...
    

    If at any point you wish to restore the default behavior, you can do so:

    cv::redirectError(nullptr);    //Restore default behavior; pass NULL if no C++11
    
    0 讨论(0)
提交回复
热议问题