OpenCV - 禁用打印异常

OpenCV - disable print Exceptions

我是 OpenCV 的新手,但现在我需要 catch cv:Exception。我制作了 try and catch 块:

try{
    //do some opencv things
}
catch (cv::Exception& e){
    cout << "OpenCV error: " << endl << e.what();
    cin.get();
}

效果很好,我可以捕获异常。但是异常消息被写入了两次。一个来自 opencv 库,第二个来自我。

有什么方法可以禁止从 opencv 向 stderr 发送消息吗?我制作了发布版本 - 将 VS2013 更改为 Release 并从所有库的名称中删除 "d" 。但是还是写了两遍

您可以使用 cvRedirectError 覆盖 OpenCV 的默认错误处理。

你可以使用它like this:

int myErrorHandler(int status, const char* func_name, const char* err_msg, 
                   const char* file_name, int line, void*)
{
    // Do whatever you want here
    return 0;
}

cvRedirectError(myErrorHandler); ///< Call this once somewhere

这就是恢复默认行为的方式:

cv::redirectError(cv::ErrorCallback())