How to suppress OpenCV error message

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

I have a code like this:

try { H = findHomography( obj, scene, CV_RANSAC ); } catch (Exception &e) { if (showOutput) cout<< "Error throwed when finding homography"<<endl; errorCount++; if (errorCount >=10) { errorCount = 0; selected_temp = -99; foundBB = false; bb_x1 = 0; bb_x2 = 0; bb_y1 = 0; bb_y2 = 0; } return -1; } 

An error will be raised if findHomography cannot find things. The error message includes:

 OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in findHomography, file /Users/dji-mini/Downloads/opencv- 2.4.6/modules/calib3d/src/fundam.cpp, line 1074 OpenCV Error: Assertion failed (count >= 4) in cvFindHomography, file /Users/dji-mini/Downloads/opencv-2.4.6/modules/calib3d/src/fundam.cpp, line 235 

Since I know under what conditions a message will appear, I want to suppress these error messages. But I do not know how to do this.

The old version of OpenCV seems to have a "cvSetErrMode" which, according to other articles, is amortized in OpenCV 2.X. So, what function can I use to suppress OpenCV error messages?

+7
c ++ opencv g ++
source share
1 answer

cv::error() is called each time an assertion error occurs. The default behavior is to print the statement statement before std::cerr .

You can use the undocumented function cv::redirectError() to set the error handling callback. This will override the default behavior of cv::error() . First you 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 that returns:

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

If at any time you want to restore the default behavior, you can do this:

 cv::redirectError(nullptr); //Restore default behavior; pass NULL if no C++11 
+13
source share

All Articles