开发者

OUTDATED - Error modes for OpenCV

I am using OpenCV 1 to do some image processing, and am confused about the cvSetErrMode function (which is part of CxCore).

OpenCV has three error modes.

  • Leaf: The program is terminated after the error handler is called.
  • Parent: The program is not terminated, but the error handler is called.
  • Silent: Similar to Parent mode, but no error handler is called

At the start of my code, I call cvSetErrMode(CV_ErrModeParent) to switch from the default 'leaf' mode to 'parent' mode so my application is not terminated with an exception/assertion pop up. Unfortunately 'parent' mode doesn't seem to be working. I still get the message dialog pop up, and my application still terminates.

If I call cvSetErrMode(CV_ErrModeSilent) then it actually goes silent, and no longer quits the application or throws up a dialog... but this开发者_如何学C also means that I dont know that an error has occurred. In this case, I think the mode is being set correctly.

Has anyone else seem this behaviour before and might be able to recommend a solution?

References:

  • cvSetErrMode function reference

  • Open CV Error handling mode reference


I am going to answer my own question, because after some fiddling around I have worked out what happens.

When you switch to 'parent' mode instead of leaf mode, there is an error handler that gets called cvGuiBoxReport(). cvGuiBoxReport() is the default error handler. It seems that even in parent mode, cvGuiBoxReport() still terminates your application! Oops.

So, to get around that you can write your own error handler, and redirect the error to be handled and NOT terminate the application.

An example error handler:

int MyErrorHandler(int status, const char* func_name, const char* err_msg, const char* file_name, int line, void*)
{
    std::cerr << "Woohoo, my own custom error handler" << std::endl;
    return 0;
} 

You can set up parent mode and redirect your error with:

cvSetErrMode(CV_ErrModeParent);
cvRedirectError(MyErrorHandler);


In a week of servers crashing from uploading corrupt or empty images to our image processing server, here are some thoughts on how I solved the intricacies of OpenCV's error handling. We are using V2.2 in a C++ server.

The problem arises in cv::imread() and cv::imdecode() when the image to be loaded is corrupt (or empty). Normally OpenCV just exits the process with with some error messages, not a good idea when you're running a server which should work all the time.

Reviewing the source code at https://code.ros.org/trac/opencv/browser/trunk/opencv/modules/core/include/opencv2/core/core.hpp I ignored the hint in the source comments for cv::setBreakOnError() and discovered the that following pattern works:

cv::setBreakOnError(true); // Can be set globally
...
...
cv::Mat srcImage = cv::imread(filename, 1);
if (!srcImage.data) throw std::exception("bad image");

cv::imread() will now not exit the process, but passes control to your own exception handling, so you can do with it what you like.

Finding this has saved a lot of heartbreak.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜