Opencv Error Handling Mechanism

Source: Internet
Author: User

The error handling in opencv1.0 is similar to the error handling in the C language standard function library. It is determined by checking the global error identification.ProgramWhether an error occurs and report the error.

In opencv1.0, if an error occurs when a function is called, no error is directly returned.CodeInstead, use the cv_error macro to call the cverror function and set the error status. Then, call the standard or custom error processor.

Each program thread has a global variable that contains an error (an integer). This state can be queried by the cvgeterrstatus function.

 //  Save the file and row where the error is located Typedef Struct  {  Const   Char * File;  Int  Line;} cvstackrecord;  // Save the context information of the current thread Typedef Struct  Cvcontext {  Int Err_code; //  Error Code      Int Err_mode; //  Error Handling Mode Cverrorcallback error_callback; //  Error handling function pointer entry      Void * Userdata;  Char Err_msg [ 4096  ]; Cvstackrecord err_ctx;} cvcontext; cv_impl  Void Cverror ( Int Code, Const   Char * Func_name, Const   Char * Err_msg, Const   Char * File_name, Int  Line ){  If (Code =Cv_stsok) cvseterrstatus (CODE );  Else  {Cvcontext * Context = icvgetcontext (); //  Get current context information          If (Code! = Cv_stsbacktrace & code! = Cv_stsautotrace ){  //  Save the error message to the current context.              Char * Message = context-> Err_msg; Context -> Err_code =Code; strcpy (message, err_msg); Context -> Err_ctx.file = File_name; Context -> Err_ctx.line = Line ;}  If (Context-> err_mode! = Cv_errmodesilent ){  //  Call error processor for error handling              Int Terminate = context-> error_callback (Code, func_name, err_msg, file_name, line, context-> Userdata );  If (Terminate) {cv_dbg_break ();  //  Exit (-ABS (terminate ));  }}}} 

The error handling mechanism in opencv2.4.1 is different from the above. Although the external interface has not changed, the internal processing method of opencv2.4.1 has changed. The main parameter is the exception handling mechanism of C ++.

If an error occurs when a function is called and the error code is not directly returned, use the cv_error macro to call the cverror function and call the error function in cverror to report the error message.

Before entering the error function, create an error exception class to save the current error information, and use this exception class as the interface of the error function.

In the error function, first check whether a custom error processor exists. If yes, go to the custom error handler to handle the error. Otherwise, report the error message and throw an error exception.

If an exception is caught in the error handling area, you can capture the exception and handle it accordingly. The corresponding code is as follows:

 1   # Define Cv_error (Code, MSG )\ 2  {\  3   Cverror (CODE), cvfuncname, MSG, _ file __, _ line __);\  4   _ Cv_exit __;\  5   }  6 Cv_impl Void Cverror ( Int Code, Const   Char * Func_name, Const  Char * Err_msg, Const   Char * File_name, Int  Line)  7   {  8   CV: Error (CV: exception (Code, err_msg, func_name, file_name, line ));  9   }  10   Void Error ( Const Exception & Exc) 11   {  12       If (Customerrorcallback! = 0 ) //  Check whether a custom error processor exists  13   Customerrorcallback (EXC. Code, exc. func. c_str (), exc. Err. c_str (),  14   Exc. file. c_str (), exc. Line, customerrorcallbackdata );  15       Else  16  {  17           //  Report error information  18           Const   Char * Errorstr = Cverrorstr (EXC. Code );  19           Char Buf [ 1 < 16  ];  20 Sprintf (BUF, " Opencv error: % s (% s) in % s, file % s, line % d  "  ,  21 Errorstr, exc. Err. c_str (), exc. func. Size ()> 0 ? 22 Exc. func. c_str (): "  Unknown Function  "  , Exc. file. c_str (), exc. line );  23 Fprintf (stderr, "  % S \ n "  , Buf );  24   Fflush (stderr );  25   }  26   27       If  (Breakonerror)  28   {  29           Static   Volatile   Int * P = 0 ;  30 * P = 0  ;  31   }  32       Throw Exc; //  Throw an exception  33 }

In general, the error handling mechanism in opencv is changed from the C language to the C ++ processing method.

 

For more information, seeSource code.

References:

Http://www.opencv.org.cn/index.php/Cxcore%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86%E5%92%8C%E7%B3%BB%E7%BB%9F%E5%87%BD%E6%95%B0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.