Utility and system functions and macros
Overview
// namespaces namespace cv::Error; // typedefs typedef int (*cv::ErrorCallback)( int status, const char *func_name, const char *err_msg, const char *file_name, int line, void *userdata ); typedef Hamming cv::HammingLUT; // enums enum CpuFeatures; enum cv::SortFlags; // structs struct cv::Hamming; // unions union Cv16suf; union Cv32suf; union Cv64suf; // classes template <typename _Tp> class cv::Allocator; template < typename _Tp, size_t fixed_size = 1024/sizeof(_Tp)+8 > class cv::AutoBuffer; class cv::AutoLock; class cv::CommandLineParser; class cv::Exception; class cv::Mutex; class cv::ParallelLoopBody; template <typename T> class cv::TLSData; class cv::TLSDataContainer; class cv::TickMeter; // global functions template <typename _Tp> static _Tp* cv::alignPtr( _Tp* ptr, int n = (int) sizeof(_Tp) ); static size_t cv::alignSize( size_t sz, int n ); bool cv::checkHardwareSupport(int feature); bool cv::Cholesky( float* A, size_t astep, int m, float* b, size_t bstep, int n ); bool cv::Cholesky( double* A, size_t astep, int m, double* b, size_t bstep, int n ); float cv::cubeRoot(float val); template <typename _Tp> _Tp cv::cv_abs(_Tp x); int cv::cv_abs(uchar x); int cv::cv_abs(schar x); int cv::cv_abs(ushort x); int cv::cv_abs(short x); void cv::error(const Exception& exc); void cv::error( int _code, const String& _err, const char* _func, const char* _file, int _line ); void cv::errorNoReturn( int _code, const String& _err, const char* _func, const char* _file, int _line ); float cv::fastAtan2( float y, float x ); void cv::fastFree(void* ptr); void* cv::fastMalloc(size_t bufSize); String cv::format( const char* fmt, ... ); const String& cv::getBuildInformation(); int64 cv::getCPUTickCount(); static size_t cv::getElemSize(int type); int cv::getNumberOfCPUs(); int cv::getNumThreads(); int cv::getThreadNum(); int64 cv::getTickCount(); double cv::getTickFrequency(); void cv::glob( String pattern, std::vector<String>& result, bool recursive = false ); int cv::LU( float* A, size_t astep, int m, float* b, size_t bstep, int n ); int cv::LU( double* A, size_t astep, int m, double* b, size_t bstep, int n ); template < typename _Tp, typename _AccTp > static _AccTp cv::normInf( const _Tp* a, int n ); template < typename _Tp, typename _AccTp > static _AccTp cv::normInf( const _Tp* a, const _Tp* b, int n ); template < typename _Tp, typename _AccTp > static _AccTp cv::normL1( const _Tp* a, int n ); template < typename _Tp, typename _AccTp > static _AccTp cv::normL1( const _Tp* a, const _Tp* b, int n ); float cv::normL1( const float* a, const float* b, int n ); int cv::normL1( const uchar* a, const uchar* b, int n ); template < typename _Tp, typename _AccTp > static _AccTp cv::normL2Sqr( const _Tp* a, int n ); template < typename _Tp, typename _AccTp > static _AccTp cv::normL2Sqr( const _Tp* a, const _Tp* b, int n ); static float cv::normL2Sqr( const float* a, const float* b, int n ); static std::ostream& cv::operator<<( std::ostream& out, const TickMeter& tm ); void cv::parallel_for_( const Range& range, const ParallelLoopBody& body, double nstripes = -1. ); ErrorCallback cv::redirectError( ErrorCallback errCallback, void* userdata = 0, void** prevUserdata = 0 ); template <typename _Tp> static _Tp cv::saturate_cast(uchar v); template <typename _Tp> static _Tp cv::saturate_cast(schar v); template <typename _Tp> static _Tp cv::saturate_cast(ushort v); template <typename _Tp> static _Tp cv::saturate_cast(short v); template <typename _Tp> static _Tp cv::saturate_cast(unsigned v); template <typename _Tp> static _Tp cv::saturate_cast(int v); template <typename _Tp> static _Tp cv::saturate_cast(float v); template <typename _Tp> static _Tp cv::saturate_cast(double v); template <typename _Tp> static _Tp cv::saturate_cast(int64 v); template <typename _Tp> static _Tp cv::saturate_cast(uint64 v); schar cv::saturate_cast< schar >(uchar v); schar cv::saturate_cast< schar >(ushort v); schar cv::saturate_cast< schar >(int v); schar cv::saturate_cast< schar >(short v); schar cv::saturate_cast< schar >(unsigned v); schar cv::saturate_cast< schar >(float v); schar cv::saturate_cast< schar >(double v); schar cv::saturate_cast< schar >(int64 v); schar cv::saturate_cast< schar >(uint64 v); short cv::saturate_cast< short >(ushort v); short cv::saturate_cast< short >(int v); short cv::saturate_cast< short >(unsigned v); short cv::saturate_cast< short >(float v); short cv::saturate_cast< short >(double v); short cv::saturate_cast< short >(int64 v); short cv::saturate_cast< short >(uint64 v); uchar cv::saturate_cast< uchar >(schar v); uchar cv::saturate_cast< uchar >(ushort v); uchar cv::saturate_cast< uchar >(int v); uchar cv::saturate_cast< uchar >(short v); uchar cv::saturate_cast< uchar >(unsigned v); uchar cv::saturate_cast< uchar >(float v); uchar cv::saturate_cast< uchar >(double v); uchar cv::saturate_cast< uchar >(int64 v); uchar cv::saturate_cast< uchar >(uint64 v); unsigned cv::saturate_cast< unsigned >(float v); unsigned cv::saturate_cast< unsigned >(double v); ushort cv::saturate_cast< ushort >(schar v); ushort cv::saturate_cast< ushort >(short v); ushort cv::saturate_cast< ushort >(int v); ushort cv::saturate_cast< ushort >(unsigned v); ushort cv::saturate_cast< ushort >(float v); ushort cv::saturate_cast< ushort >(double v); ushort cv::saturate_cast< ushort >(int64 v); ushort cv::saturate_cast< ushort >(uint64 v); bool cv::setBreakOnError(bool flag); void cv::setNumThreads(int nthreads); void cv::setUseOptimized(bool onoff); String cv::tempfile(const char* suffix = 0); bool cv::useOptimized(); CV_XADD( int* addr, int delta ); int cvCeil(double value); int cvCeil(float value); int cvCeil(int value); int cvFloor(double value); int cvFloor(float value); int cvFloor(int value); int cvIsInf(double value); int cvIsInf(float value); int cvIsNaN(double value); int cvIsNaN(float value); int cvRound(double value); int cvRound(float value); int cvRound(int value); // macros #define CV_2PI #define CV_Assert(expr) #define CV_CPU_AVX #define CV_CPU_AVX2 #define CV_CPU_AVX_512BW #define CV_CPU_AVX_512CD #define CV_CPU_AVX_512DQ #define CV_CPU_AVX_512ER #define CV_CPU_AVX_512F #define CV_CPU_AVX_512IFMA512 #define CV_CPU_AVX_512PF #define CV_CPU_AVX_512VBMI #define CV_CPU_AVX_512VL #define CV_CPU_FMA3 #define CV_CPU_FP16 #define CV_CPU_MMX #define CV_CPU_NEON #define CV_CPU_NONE #define CV_CPU_POPCNT #define CV_CPU_SSE #define CV_CPU_SSE2 #define CV_CPU_SSE3 #define CV_CPU_SSE4_1 #define CV_CPU_SSE4_2 #define CV_CPU_SSSE3 #define CV_DECL_ALIGNED(x) #define CV_DbgAssert(expr) #define CV_ELEM_SIZE(type) #define CV_ELEM_SIZE1(type) #define CV_ENABLE_UNROLLED #define CV_EXPORTS #define CV_EXPORTS_AS(synonym) #define CV_EXPORTS_W #define CV_EXPORTS_W_MAP #define CV_EXPORTS_W_SIMPLE #define CV_EXTERN_C #define CV_Error( \ code, \ msg \ ) #define CV_ErrorNoReturn( \ code, \ msg \ ) #define CV_ErrorNoReturn_( \ code, \ args \ ) #define CV_Error_( \ code, \ args \ ) #define CV_FP16_TYPE #define CV_Func #define CV_HARDWARE_MAX_FEATURE #define CV_IN_OUT #define CV_IS_CONT_MAT #define CV_IS_MAT_CONT(flags) #define CV_IS_SUBMAT(flags) #define CV_LOG2 #define CV_MAT_CN(flags) #define CV_MAT_CN_MASK #define CV_MAT_CONT_FLAG #define CV_MAT_CONT_FLAG_SHIFT #define CV_MAT_TYPE(flags) #define CV_MAT_TYPE_MASK #define CV_OUT #define CV_PI #define CV_PROP #define CV_PROP_RW #define CV_SUBMAT_FLAG #define CV_SUBMAT_FLAG_SHIFT #define CV_WRAP #define CV_WRAP_AS(synonym) #define MAX( \ a, \ b \ ) #define MIN( \ a, \ b \ ) #define OPENCV_ABI_COMPATIBILITY #define __CV_CAT( \ x, \ y \ ) #define __CV_CAT_( \ x, \ y \ ) #define __CV_CAT__( \ x, \ y \ ) #define __CV_EXPAND(x)
Detailed Documentation
Global Functions
template <typename _Tp> static _Tp* cv::alignPtr( _Tp* ptr, int n = (int) sizeof(_Tp) )
Aligns a pointer to the specified number of bytes.
The function returns the aligned pointer of the same type as the input pointer:
Parameters:
ptr | Aligned pointer. |
n | Alignment size that must be a power of two. |
static size_t cv::alignSize( size_t sz, int n )
Aligns a buffer size to the specified number of bytes.
The function returns the minimum number that is greater or equal to sz and is divisible by n :
Parameters:
sz | Buffer size to align. |
n | Alignment size that must be a power of two. |
bool cv::checkHardwareSupport(int feature)
Returns true if the specified feature is supported by the host hardware.
The function returns true if the host hardware supports the specified feature. When user calls setUseOptimized(false), the subsequent calls to checkHardwareSupport() will return false until setUseOptimized(true) is called. This way user can dynamically switch on and off the optimized code in OpenCV.
Parameters:
feature | The feature of interest, one of cv::CpuFeatures |
bool cv::Cholesky( float* A, size_t astep, int m, float* b, size_t bstep, int n )
proxy for hal::Cholesky
bool cv::Cholesky( double* A, size_t astep, int m, double* b, size_t bstep, int n )
proxy for hal::Cholesky
float cv::cubeRoot(float val)
Computes the cube root of an argument.
The function cubeRoot computes \(\sqrt[3]{\texttt{val}}\). Negative arguments are handled correctly. NaN and Inf are not handled. The accuracy approaches the maximum possible accuracy for single-precision data.
Parameters:
val | A function argument. |
void cv::error(const Exception& exc)
Signals an error and raises the exception.
By default the function prints information about the error to stderr, then it either stops if cv::setBreakOnError() had been called before or raises the exception. It is possible to alternate error processing by using cv::redirectError(). Deprecated drop this version
Parameters:
exc | the exception raisen. |
void cv::error( int _code, const String& _err, const char* _func, const char* _file, int _line )
Signals an error and raises the exception.
By default the function prints information about the error to stderr, then it either stops if setBreakOnError() had been called before or raises the exception. It is possible to alternate error processing by using redirectError().
Parameters:
_code |
|
_err |
|
_func |
|
_file |
|
_line |
|
See also:
CV_Error, CV_Error_, CV_ErrorNoReturn, CV_ErrorNoReturn_, CV_Assert, CV_DbgAssert
void cv::errorNoReturn( int _code, const String& _err, const char* _func, const char* _file, int _line )
same as cv::error, but does not return
float cv::fastAtan2( float y, float x )
Calculates the angle of a 2D vector in degrees.
The function fastAtan2 calculates the full-range angle of an input 2D vector. The angle is measured in degrees and varies from 0 to 360 degrees. The accuracy is about 0.3 degrees.
Parameters:
x | x-coordinate of the vector. |
y | y-coordinate of the vector. |
void cv::fastFree(void* ptr)
Deallocates a memory buffer.
The function deallocates the buffer allocated with fastMalloc . If NULL pointer is passed, the function does nothing. C version of the function clears the pointer pptr to avoid problems with double memory deallocation.
Parameters:
ptr | Pointer to the allocated buffer. |
void* cv::fastMalloc(size_t bufSize)
Allocates an aligned memory buffer.
The function allocates the buffer of the specified size and returns it. When the buffer size is 16 bytes or more, the returned buffer is aligned to 16 bytes.
Parameters:
bufSize | Allocated buffer size. |
String cv::format( const char* fmt, ... )
Returns a text string formatted using the printf-like expression.
The function acts like sprintf but forms and returns an STL string. It can be used to form an error message in the Exception constructor.
Parameters:
fmt | printf-compatible formatting specifiers. |
const String& cv::getBuildInformation()
Returns full configuration time cmake output.
Returned value is raw cmake output including version control system revision, compiler version, compiler flags, enabled modules and third party libraries, etc. Output format depends on target architecture.
int64 cv::getCPUTickCount()
Returns the number of CPU ticks.
The function returns the current number of CPU ticks on some architectures (such as x86, x64, PowerPC). On other platforms the function is equivalent to getTickCount. It can also be used for very accurate time measurements, as well as for RNG initialization. Note that in case of multi-CPU systems a thread, from which getCPUTickCount is called, can be suspended and resumed at another CPU with its own counter. So, theoretically (and practically) the subsequent calls to the function do not necessary return the monotonously increasing values. Also, since a modern CPU varies the CPU frequency depending on the load, the number of CPU clocks spent in some code cannot be directly converted to time units. Therefore, getTickCount is generally a preferable solution for measuring execution time.
int cv::getNumberOfCPUs()
Returns the number of logical CPUs available for the process.
int cv::getNumThreads()
Returns the number of threads used by OpenCV for parallel regions.
Always returns 1 if OpenCV is built without threading support.
The exact meaning of return value depends on the threading framework used by OpenCV library:
TBB
– The number of threads, that OpenCV will try to use for parallel regions. If there is any tbb::thread_scheduler_init in user code conflicting with OpenCV, then function returns default number of threads used by TBB library.OpenMP
– An upper bound on the number of threads that could be used to form a new team.Concurrency
– The number of threads, that OpenCV will try to use for parallel regions.GCD
– Unsupported; returns the GCD thread pool limit (512) for compatibility.C=
– The number of threads, that OpenCV will try to use for parallel regions, if before called setNumThreads with threads > 0, otherwise returns the number of logical CPUs, available for the process.
See also:
int cv::getThreadNum()
Returns the index of the currently executed thread within the current parallel region. Always returns 0 if called outside of parallel region.
The exact meaning of return value depends on the threading framework used by OpenCV library:
TBB
– Unsupported with current 4.1 TBB release. Maybe will be supported in future.OpenMP
– The thread number, within the current team, of the calling thread.Concurrency
– An ID for the virtual processor that the current context is executing on (0 for master thread and unique number for others, but not necessary 1,2,3,…).GCD
– System calling thread’s ID. Never returns 0 inside parallel region.C=
– The index of the current parallel task.
See also:
int64 cv::getTickCount()
Returns the number of ticks.
The function returns the number of ticks after the certain event (for example, when the machine was turned on). It can be used to initialize RNG or to measure a function execution time by reading the tick count before and after the function call.
See also:
double cv::getTickFrequency()
Returns the number of ticks per second.
The function returns the number of ticks per second. That is, the following code computes the execution time in seconds:
double t = (double)getTickCount(); // do something ... t = ((double)getTickCount() - t)/getTickFrequency();
See also:
int cv::LU( float* A, size_t astep, int m, float* b, size_t bstep, int n )
proxy for hal::LU
int cv::LU( double* A, size_t astep, int m, double* b, size_t bstep, int n )
proxy for hal::LU
static std::ostream& cv::operator<<( std::ostream& out, const TickMeter& tm )
output operator
TickMeter tm; tm.start(); // do something ... tm.stop(); std::cout << tm;
void cv::parallel_for_( const Range& range, const ParallelLoopBody& body, double nstripes = -1. )
Parallel data processor.
ErrorCallback cv::redirectError( ErrorCallback errCallback, void* userdata = 0, void** prevUserdata = 0 )
Sets the new error handler and the optional user data.
The function sets the new error handler, called from cv::error().
Parameters:
errCallback | the new error handler. If NULL, the default error handler is used. |
userdata | the optional user data pointer, passed to the callback. |
prevUserdata | the optional output parameter where the previous user data pointer is stored |
Returns:
the previous error handler
template <typename _Tp> static _Tp cv::saturate_cast(uchar v)
Template function for accurate conversion from one primitive type to another.
The functions saturate_cast resemble the standard C++ cast operations, such as static_cast<T>() and others. They perform an efficient and accurate conversion from one primitive type to another (see the introduction chapter). saturate in the name means that when the input value v is out of the range of the target type, the result is not formed just by taking low bits of the input, but instead the value is clipped. For example:
uchar a = saturate_cast<uchar>(-100); // a = 0 (UCHAR_MIN) short b = saturate_cast<short>(33333.33333); // b = 32767 (SHRT_MAX)
Such clipping is done when the target type is unsigned char , signed char , unsigned short or signed short . For 32-bit integers, no clipping is done.
When the parameter is a floating-point value and the target type is an integer (8-, 16- or 32-bit), the floating-point value is first rounded to the nearest integer and then clipped if needed (when the target type is 8- or 16-bit).
This operation is used in the simplest or most complex image processing functions in OpenCV.
Parameters:
v | Function parameter. |
See also:
add, subtract, multiply, divide, Mat::convertTo
template <typename _Tp> static _Tp cv::saturate_cast(schar v)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template <typename _Tp> static _Tp cv::saturate_cast(ushort v)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template <typename _Tp> static _Tp cv::saturate_cast(short v)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template <typename _Tp> static _Tp cv::saturate_cast(unsigned v)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template <typename _Tp> static _Tp cv::saturate_cast(int v)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template <typename _Tp> static _Tp cv::saturate_cast(float v)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template <typename _Tp> static _Tp cv::saturate_cast(double v)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template <typename _Tp> static _Tp cv::saturate_cast(int64 v)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template <typename _Tp> static _Tp cv::saturate_cast(uint64 v)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
bool cv::setBreakOnError(bool flag)
Sets/resets the break-on-error mode.
When the break-on-error mode is set, the default error handler issues a hardware exception, which can make debugging more convenient.
Returns:
the previous state
void cv::setNumThreads(int nthreads)
OpenCV will try to set the number of threads for the next parallel region.
If threads == 0, OpenCV will disable threading optimizations and run all it’s functions sequentially. Passing threads < 0 will reset threads number to system default. This function must be called outside of parallel region.
OpenCV will try to run it’s functions with specified threads number, but some behaviour differs from framework:
TBB
– User-defined parallel constructions will run with the same threads number, if another does not specified. If later on user creates own scheduler, OpenCV will use it.OpenMP
– No special defined behaviour.Concurrency
– If threads == 1, OpenCV will disable threading optimizations and run it’s functions sequentially.GCD
– Supports only values <= 0.C=
– No special defined behaviour.
Parameters:
nthreads | Number of threads used by OpenCV. |
See also:
void cv::setUseOptimized(bool onoff)
Enables or disables the optimized code.
The function can be used to dynamically turn on and off optimized code (code that uses SSE2, AVX, and other instructions on the platforms that support it). It sets a global flag that is further checked by OpenCV functions. Since the flag is not checked in the inner OpenCV loops, it is only safe to call the function on the very top level in your application where you can be sure that no other OpenCV function is currently executed.
By default, the optimized code is enabled unless you disable it in CMake. The current status can be retrieved using useOptimized.
Parameters:
onoff | The boolean flag specifying whether the optimized code should be used (onoff=true) or not (onoff=false). |
bool cv::useOptimized()
Returns the status of optimized code usage.
The function returns true if the optimized code is enabled. Otherwise, it returns false.
int cvCeil(double value)
Rounds floating-point number to the nearest integer not smaller than the original.
The function computes an integer i such that:
Parameters:
value | floating-point number. If the value is outside of INT_MIN … INT_MAX range, the result is not defined. |
int cvCeil(float value)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
int cvCeil(int value)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
int cvFloor(double value)
Rounds floating-point number to the nearest integer not larger than the original.
The function computes an integer i such that:
Parameters:
value | floating-point number. If the value is outside of INT_MIN … INT_MAX range, the result is not defined. |
int cvFloor(float value)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
int cvFloor(int value)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
int cvIsInf(double value)
Determines if the argument is Infinity.
The function returns 1 if the argument is a plus or minus infinity (as defined by IEEE754 standard) and 0 otherwise.
Parameters:
value | The input floating-point value |
int cvIsInf(float value)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
int cvIsNaN(double value)
Determines if the argument is Not A Number.
The function returns 1 if the argument is Not A Number (as defined by IEEE754 standard), 0 otherwise.
Parameters:
value | The input floating-point value |
int cvIsNaN(float value)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
int cvRound(double value)
Rounds floating-point number to the nearest integer.
Parameters:
value | floating-point number. If the value is outside of INT_MIN … INT_MAX range, the result is not defined. |
int cvRound(float value)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
int cvRound(int value)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Macros
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
The macros CV_Assert (and CV_DbgAssert(expr)) evaluate the specified expression. If it is 0, the macros raise an error (see cv::error). The macro CV_Assert checks the condition in both Debug and Release configurations while CV_DbgAssert is only retained in the Debug configuration.
#define CV_DbgAssert(expr)
replaced with CV_Assert(expr) in Debug configuration
#define CV_ELEM_SIZE(type)
0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem))
#define CV_ELEM_SIZE1(type)
Size of each channel item, 0x8442211 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem)
#define CV_Error( \ code, \ msg \ )
Call the error handler.
Currently, the error handler prints the error code and the error message to the standard error stream stderr
. In the Debug configuration, it then provokes memory access violation, so that the execution stack and all the parameters can be analyzed by the debugger. In the Release configuration, the exception is thrown.
Parameters:
code | one of Error::Code |
msg | error message |
#define CV_ErrorNoReturn( \ code, \ msg \ )
same as CV_Error(code,msg), but does not return
#define CV_ErrorNoReturn_( \ code, \ args \ )
same as CV_Error_(code,args), but does not return
#define CV_Error_( \ code, \ args \ )
Call the error handler.
This macro can be used to construct an error message on-fly to include some dynamic information, for example:
// note the extra parentheses around the formatted text message CV_Error_( CV_StsOutOfRange, ("the value at (%d, %d)=%g is out of range", badPt.x, badPt.y, badValue));
Parameters:
code | one of Error::Code |
args | printf-like formatted error message in parentheses |