base.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Copyright (C) 2014, Itseez Inc., all rights reserved.
  17. // Third party copyrights are property of their respective owners.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification,
  20. // are permitted provided that the following conditions are met:
  21. //
  22. // * Redistribution's of source code must retain the above copyright notice,
  23. // this list of conditions and the following disclaimer.
  24. //
  25. // * Redistribution's in binary form must reproduce the above copyright notice,
  26. // this list of conditions and the following disclaimer in the documentation
  27. // and/or other materials provided with the distribution.
  28. //
  29. // * The name of the copyright holders may not be used to endorse or promote products
  30. // derived from this software without specific prior written permission.
  31. //
  32. // This software is provided by the copyright holders and contributors "as is" and
  33. // any express or implied warranties, including, but not limited to, the implied
  34. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  35. // In no event shall the Intel Corporation or contributors be liable for any direct,
  36. // indirect, incidental, special, exemplary, or consequential damages
  37. // (including, but not limited to, procurement of substitute goods or services;
  38. // loss of use, data, or profits; or business interruption) however caused
  39. // and on any theory of liability, whether in contract, strict liability,
  40. // or tort (including negligence or otherwise) arising in any way out of
  41. // the use of this software, even if advised of the possibility of such damage.
  42. //
  43. //M*/
  44. #ifndef OPENCV_CORE_BASE_HPP
  45. #define OPENCV_CORE_BASE_HPP
  46. #ifndef __cplusplus
  47. # error base.hpp header must be compiled as C++
  48. #endif
  49. #include "opencv2/opencv_modules.hpp"
  50. #include <climits>
  51. #include <algorithm>
  52. #include "opencv2/core/cvdef.h"
  53. #include "opencv2/core/cvstd.hpp"
  54. namespace cv
  55. {
  56. //! @addtogroup core_utils
  57. //! @{
  58. namespace Error {
  59. //! error codes
  60. enum Code {
  61. StsOk= 0, //!< everything is ok
  62. StsBackTrace= -1, //!< pseudo error for back trace
  63. StsError= -2, //!< unknown /unspecified error
  64. StsInternal= -3, //!< internal error (bad state)
  65. StsNoMem= -4, //!< insufficient memory
  66. StsBadArg= -5, //!< function arg/param is bad
  67. StsBadFunc= -6, //!< unsupported function
  68. StsNoConv= -7, //!< iteration didn't converge
  69. StsAutoTrace= -8, //!< tracing
  70. HeaderIsNull= -9, //!< image header is NULL
  71. BadImageSize= -10, //!< image size is invalid
  72. BadOffset= -11, //!< offset is invalid
  73. BadDataPtr= -12, //!<
  74. BadStep= -13, //!< image step is wrong, this may happen for a non-continuous matrix.
  75. BadModelOrChSeq= -14, //!<
  76. BadNumChannels= -15, //!< bad number of channels, for example, some functions accept only single channel matrices.
  77. BadNumChannel1U= -16, //!<
  78. BadDepth= -17, //!< input image depth is not supported by the function
  79. BadAlphaChannel= -18, //!<
  80. BadOrder= -19, //!< number of dimensions is out of range
  81. BadOrigin= -20, //!< incorrect input origin
  82. BadAlign= -21, //!< incorrect input align
  83. BadCallBack= -22, //!<
  84. BadTileSize= -23, //!<
  85. BadCOI= -24, //!< input COI is not supported
  86. BadROISize= -25, //!< incorrect input roi
  87. MaskIsTiled= -26, //!<
  88. StsNullPtr= -27, //!< null pointer
  89. StsVecLengthErr= -28, //!< incorrect vector length
  90. StsFilterStructContentErr= -29, //!< incorrect filter structure content
  91. StsKernelStructContentErr= -30, //!< incorrect transform kernel content
  92. StsFilterOffsetErr= -31, //!< incorrect filter offset value
  93. StsBadSize= -201, //!< the input/output structure size is incorrect
  94. StsDivByZero= -202, //!< division by zero
  95. StsInplaceNotSupported= -203, //!< in-place operation is not supported
  96. StsObjectNotFound= -204, //!< request can't be completed
  97. StsUnmatchedFormats= -205, //!< formats of input/output arrays differ
  98. StsBadFlag= -206, //!< flag is wrong or not supported
  99. StsBadPoint= -207, //!< bad CvPoint
  100. StsBadMask= -208, //!< bad format of mask (neither 8uC1 nor 8sC1)
  101. StsUnmatchedSizes= -209, //!< sizes of input/output structures do not match
  102. StsUnsupportedFormat= -210, //!< the data format/type is not supported by the function
  103. StsOutOfRange= -211, //!< some of parameters are out of range
  104. StsParseError= -212, //!< invalid syntax/structure of the parsed file
  105. StsNotImplemented= -213, //!< the requested function/feature is not implemented
  106. StsBadMemBlock= -214, //!< an allocated block has been corrupted
  107. StsAssert= -215, //!< assertion failed
  108. GpuNotSupported= -216, //!< no CUDA support
  109. GpuApiCallError= -217, //!< GPU API call error
  110. OpenGlNotSupported= -218, //!< no OpenGL support
  111. OpenGlApiCallError= -219, //!< OpenGL API call error
  112. OpenCLApiCallError= -220, //!< OpenCL API call error
  113. OpenCLDoubleNotSupported= -221,
  114. OpenCLInitError= -222, //!< OpenCL initialization error
  115. OpenCLNoAMDBlasFft= -223
  116. };
  117. } //Error
  118. //! @} core_utils
  119. //! @addtogroup core_array
  120. //! @{
  121. //! matrix decomposition types
  122. enum DecompTypes {
  123. /** Gaussian elimination with the optimal pivot element chosen. */
  124. DECOMP_LU = 0,
  125. /** singular value decomposition (SVD) method; the system can be over-defined and/or the matrix
  126. src1 can be singular */
  127. DECOMP_SVD = 1,
  128. /** eigenvalue decomposition; the matrix src1 must be symmetrical */
  129. DECOMP_EIG = 2,
  130. /** Cholesky \f$LL^T\f$ factorization; the matrix src1 must be symmetrical and positively
  131. defined */
  132. DECOMP_CHOLESKY = 3,
  133. /** QR factorization; the system can be over-defined and/or the matrix src1 can be singular */
  134. DECOMP_QR = 4,
  135. /** while all the previous flags are mutually exclusive, this flag can be used together with
  136. any of the previous; it means that the normal equations
  137. \f$\texttt{src1}^T\cdot\texttt{src1}\cdot\texttt{dst}=\texttt{src1}^T\texttt{src2}\f$ are
  138. solved instead of the original system
  139. \f$\texttt{src1}\cdot\texttt{dst}=\texttt{src2}\f$ */
  140. DECOMP_NORMAL = 16
  141. };
  142. /** norm types
  143. src1 and src2 denote input arrays.
  144. */
  145. enum NormTypes {
  146. /**
  147. \f[
  148. norm = \forkthree
  149. {\|\texttt{src1}\|_{L_{\infty}} = \max _I | \texttt{src1} (I)|}{if \(\texttt{normType} = \texttt{NORM_INF}\) }
  150. {\|\texttt{src1}-\texttt{src2}\|_{L_{\infty}} = \max _I | \texttt{src1} (I) - \texttt{src2} (I)|}{if \(\texttt{normType} = \texttt{NORM_INF}\) }
  151. {\frac{\|\texttt{src1}-\texttt{src2}\|_{L_{\infty}} }{\|\texttt{src2}\|_{L_{\infty}} }}{if \(\texttt{normType} = \texttt{NORM_RELATIVE | NORM_INF}\) }
  152. \f]
  153. */
  154. NORM_INF = 1,
  155. /**
  156. \f[
  157. norm = \forkthree
  158. {\| \texttt{src1} \| _{L_1} = \sum _I | \texttt{src1} (I)|}{if \(\texttt{normType} = \texttt{NORM_L1}\)}
  159. { \| \texttt{src1} - \texttt{src2} \| _{L_1} = \sum _I | \texttt{src1} (I) - \texttt{src2} (I)|}{if \(\texttt{normType} = \texttt{NORM_L1}\) }
  160. { \frac{\|\texttt{src1}-\texttt{src2}\|_{L_1} }{\|\texttt{src2}\|_{L_1}} }{if \(\texttt{normType} = \texttt{NORM_RELATIVE | NORM_L1}\) }
  161. \f]*/
  162. NORM_L1 = 2,
  163. /**
  164. \f[
  165. norm = \forkthree
  166. { \| \texttt{src1} \| _{L_2} = \sqrt{\sum_I \texttt{src1}(I)^2} }{if \(\texttt{normType} = \texttt{NORM_L2}\) }
  167. { \| \texttt{src1} - \texttt{src2} \| _{L_2} = \sqrt{\sum_I (\texttt{src1}(I) - \texttt{src2}(I))^2} }{if \(\texttt{normType} = \texttt{NORM_L2}\) }
  168. { \frac{\|\texttt{src1}-\texttt{src2}\|_{L_2} }{\|\texttt{src2}\|_{L_2}} }{if \(\texttt{normType} = \texttt{NORM_RELATIVE | NORM_L2}\) }
  169. \f]
  170. */
  171. NORM_L2 = 4,
  172. /**
  173. \f[
  174. norm = \forkthree
  175. { \| \texttt{src1} \| _{L_2} ^{2} = \sum_I \texttt{src1}(I)^2} {if \(\texttt{normType} = \texttt{NORM_L2SQR}\)}
  176. { \| \texttt{src1} - \texttt{src2} \| _{L_2} ^{2} = \sum_I (\texttt{src1}(I) - \texttt{src2}(I))^2 }{if \(\texttt{normType} = \texttt{NORM_L2SQR}\) }
  177. { \left(\frac{\|\texttt{src1}-\texttt{src2}\|_{L_2} }{\|\texttt{src2}\|_{L_2}}\right)^2 }{if \(\texttt{normType} = \texttt{NORM_RELATIVE | NORM_L2}\) }
  178. \f]
  179. */
  180. NORM_L2SQR = 5,
  181. /**
  182. In the case of one input array, calculates the Hamming distance of the array from zero,
  183. In the case of two input arrays, calculates the Hamming distance between the arrays.
  184. */
  185. NORM_HAMMING = 6,
  186. /**
  187. Similar to NORM_HAMMING, but in the calculation, each two bits of the input sequence will
  188. be added and treated as a single bit to be used in the same calculation as NORM_HAMMING.
  189. */
  190. NORM_HAMMING2 = 7,
  191. NORM_TYPE_MASK = 7, //!< bit-mask which can be used to separate norm type from norm flags
  192. NORM_RELATIVE = 8, //!< flag
  193. NORM_MINMAX = 32 //!< flag
  194. };
  195. //! comparison types
  196. enum CmpTypes { CMP_EQ = 0, //!< src1 is equal to src2.
  197. CMP_GT = 1, //!< src1 is greater than src2.
  198. CMP_GE = 2, //!< src1 is greater than or equal to src2.
  199. CMP_LT = 3, //!< src1 is less than src2.
  200. CMP_LE = 4, //!< src1 is less than or equal to src2.
  201. CMP_NE = 5 //!< src1 is unequal to src2.
  202. };
  203. //! generalized matrix multiplication flags
  204. enum GemmFlags { GEMM_1_T = 1, //!< transposes src1
  205. GEMM_2_T = 2, //!< transposes src2
  206. GEMM_3_T = 4 //!< transposes src3
  207. };
  208. enum DftFlags {
  209. /** performs an inverse 1D or 2D transform instead of the default forward
  210. transform. */
  211. DFT_INVERSE = 1,
  212. /** scales the result: divide it by the number of array elements. Normally, it is
  213. combined with DFT_INVERSE. */
  214. DFT_SCALE = 2,
  215. /** performs a forward or inverse transform of every individual row of the input
  216. matrix; this flag enables you to transform multiple vectors simultaneously and can be used to
  217. decrease the overhead (which is sometimes several times larger than the processing itself) to
  218. perform 3D and higher-dimensional transformations and so forth.*/
  219. DFT_ROWS = 4,
  220. /** performs a forward transformation of 1D or 2D real array; the result,
  221. though being a complex array, has complex-conjugate symmetry (*CCS*, see the function
  222. description below for details), and such an array can be packed into a real array of the same
  223. size as input, which is the fastest option and which is what the function does by default;
  224. however, you may wish to get a full complex array (for simpler spectrum analysis, and so on) -
  225. pass the flag to enable the function to produce a full-size complex output array. */
  226. DFT_COMPLEX_OUTPUT = 16,
  227. /** performs an inverse transformation of a 1D or 2D complex array; the
  228. result is normally a complex array of the same size, however, if the input array has
  229. conjugate-complex symmetry (for example, it is a result of forward transformation with
  230. DFT_COMPLEX_OUTPUT flag), the output is a real array; while the function itself does not
  231. check whether the input is symmetrical or not, you can pass the flag and then the function
  232. will assume the symmetry and produce the real output array (note that when the input is packed
  233. into a real array and inverse transformation is executed, the function treats the input as a
  234. packed complex-conjugate symmetrical array, and the output will also be a real array). */
  235. DFT_REAL_OUTPUT = 32,
  236. /** specifies that input is complex input. If this flag is set, the input must have 2 channels.
  237. On the other hand, for backwards compatibility reason, if input has 2 channels, input is
  238. already considered complex. */
  239. DFT_COMPLEX_INPUT = 64,
  240. /** performs an inverse 1D or 2D transform instead of the default forward transform. */
  241. DCT_INVERSE = DFT_INVERSE,
  242. /** performs a forward or inverse transform of every individual row of the input
  243. matrix. This flag enables you to transform multiple vectors simultaneously and can be used to
  244. decrease the overhead (which is sometimes several times larger than the processing itself) to
  245. perform 3D and higher-dimensional transforms and so forth.*/
  246. DCT_ROWS = DFT_ROWS
  247. };
  248. //! Various border types, image boundaries are denoted with `|`
  249. //! @see borderInterpolate, copyMakeBorder
  250. enum BorderTypes {
  251. BORDER_CONSTANT = 0, //!< `iiiiii|abcdefgh|iiiiiii` with some specified `i`
  252. BORDER_REPLICATE = 1, //!< `aaaaaa|abcdefgh|hhhhhhh`
  253. BORDER_REFLECT = 2, //!< `fedcba|abcdefgh|hgfedcb`
  254. BORDER_WRAP = 3, //!< `cdefgh|abcdefgh|abcdefg`
  255. BORDER_REFLECT_101 = 4, //!< `gfedcb|abcdefgh|gfedcba`
  256. BORDER_TRANSPARENT = 5, //!< `uvwxyz|absdefgh|ijklmno`
  257. BORDER_REFLECT101 = BORDER_REFLECT_101, //!< same as BORDER_REFLECT_101
  258. BORDER_DEFAULT = BORDER_REFLECT_101, //!< same as BORDER_REFLECT_101
  259. BORDER_ISOLATED = 16 //!< do not look outside of ROI
  260. };
  261. //! @} core_array
  262. //! @addtogroup core_utils
  263. //! @{
  264. //! @cond IGNORED
  265. //////////////// static assert /////////////////
  266. #define CVAUX_CONCAT_EXP(a, b) a##b
  267. #define CVAUX_CONCAT(a, b) CVAUX_CONCAT_EXP(a,b)
  268. #if defined(__clang__)
  269. # ifndef __has_extension
  270. # define __has_extension __has_feature /* compatibility, for older versions of clang */
  271. # endif
  272. # if __has_extension(cxx_static_assert)
  273. # define CV_StaticAssert(condition, reason) static_assert((condition), reason " " #condition)
  274. # elif __has_extension(c_static_assert)
  275. # define CV_StaticAssert(condition, reason) _Static_assert((condition), reason " " #condition)
  276. # endif
  277. #elif defined(__GNUC__)
  278. # if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L)
  279. # define CV_StaticAssert(condition, reason) static_assert((condition), reason " " #condition)
  280. # endif
  281. #elif defined(_MSC_VER)
  282. # if _MSC_VER >= 1600 /* MSVC 10 */
  283. # define CV_StaticAssert(condition, reason) static_assert((condition), reason " " #condition)
  284. # endif
  285. #endif
  286. #ifndef CV_StaticAssert
  287. # if !defined(__clang__) && defined(__GNUC__) && (__GNUC__*100 + __GNUC_MINOR__ > 302)
  288. # define CV_StaticAssert(condition, reason) ({ extern int __attribute__((error("CV_StaticAssert: " reason " " #condition))) CV_StaticAssert(); ((condition) ? 0 : CV_StaticAssert()); })
  289. # else
  290. template <bool x> struct CV_StaticAssert_failed;
  291. template <> struct CV_StaticAssert_failed<true> { enum { val = 1 }; };
  292. template<int x> struct CV_StaticAssert_test {};
  293. # define CV_StaticAssert(condition, reason)\
  294. typedef cv::CV_StaticAssert_test< sizeof(cv::CV_StaticAssert_failed< static_cast<bool>(condition) >) > CVAUX_CONCAT(CV_StaticAssert_failed_at_, __LINE__)
  295. # endif
  296. #endif
  297. // Suppress warning "-Wdeprecated-declarations" / C4996
  298. #if defined(_MSC_VER)
  299. #define CV_DO_PRAGMA(x) __pragma(x)
  300. #elif defined(__GNUC__)
  301. #define CV_DO_PRAGMA(x) _Pragma (#x)
  302. #else
  303. #define CV_DO_PRAGMA(x)
  304. #endif
  305. #ifdef _MSC_VER
  306. #define CV_SUPPRESS_DEPRECATED_START \
  307. CV_DO_PRAGMA(warning(push)) \
  308. CV_DO_PRAGMA(warning(disable: 4996))
  309. #define CV_SUPPRESS_DEPRECATED_END CV_DO_PRAGMA(warning(pop))
  310. #elif defined (__clang__) || ((__GNUC__) && (__GNUC__*100 + __GNUC_MINOR__ > 405))
  311. #define CV_SUPPRESS_DEPRECATED_START \
  312. CV_DO_PRAGMA(GCC diagnostic push) \
  313. CV_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")
  314. #define CV_SUPPRESS_DEPRECATED_END CV_DO_PRAGMA(GCC diagnostic pop)
  315. #else
  316. #define CV_SUPPRESS_DEPRECATED_START
  317. #define CV_SUPPRESS_DEPRECATED_END
  318. #endif
  319. #define CV_UNUSED(name) (void)name
  320. //! @endcond
  321. /*! @brief Signals an error and raises the exception.
  322. By default the function prints information about the error to stderr,
  323. then it either stops if setBreakOnError() had been called before or raises the exception.
  324. It is possible to alternate error processing by using redirectError().
  325. @param _code - error code (Error::Code)
  326. @param _err - error description
  327. @param _func - function name. Available only when the compiler supports getting it
  328. @param _file - source file name where the error has occurred
  329. @param _line - line number in the source file where the error has occurred
  330. @see CV_Error, CV_Error_, CV_ErrorNoReturn, CV_ErrorNoReturn_, CV_Assert, CV_DbgAssert
  331. */
  332. CV_EXPORTS void error(int _code, const String& _err, const char* _func, const char* _file, int _line);
  333. #ifdef __GNUC__
  334. # if defined __clang__ || defined __APPLE__
  335. # pragma GCC diagnostic push
  336. # pragma GCC diagnostic ignored "-Winvalid-noreturn"
  337. # endif
  338. #endif
  339. /** same as cv::error, but does not return */
  340. CV_INLINE CV_NORETURN void errorNoReturn(int _code, const String& _err, const char* _func, const char* _file, int _line)
  341. {
  342. error(_code, _err, _func, _file, _line);
  343. #ifdef __GNUC__
  344. # if !defined __clang__ && !defined __APPLE__
  345. // this suppresses this warning: "noreturn" function does return [enabled by default]
  346. __builtin_trap();
  347. // or use infinite loop: for (;;) {}
  348. # endif
  349. #endif
  350. }
  351. #ifdef __GNUC__
  352. # if defined __clang__ || defined __APPLE__
  353. # pragma GCC diagnostic pop
  354. # endif
  355. #endif
  356. #if defined __GNUC__
  357. #define CV_Func __func__
  358. #elif defined _MSC_VER
  359. #define CV_Func __FUNCTION__
  360. #else
  361. #define CV_Func ""
  362. #endif
  363. #ifdef CV_STATIC_ANALYSIS
  364. // In practice, some macro are not processed correctly (noreturn is not detected).
  365. // We need to use simplified definition for them.
  366. #define CV_Error(...) do { abort(); } while (0)
  367. #define CV_Error_(...) do { abort(); } while (0)
  368. #define CV_Assert(cond) do { if (!(cond)) abort(); } while (0)
  369. #define CV_ErrorNoReturn(...) do { abort(); } while (0)
  370. #define CV_ErrorNoReturn_(...) do { abort(); } while (0)
  371. #else // CV_STATIC_ANALYSIS
  372. /** @brief Call the error handler.
  373. Currently, the error handler prints the error code and the error message to the standard
  374. error stream `stderr`. In the Debug configuration, it then provokes memory access violation, so that
  375. the execution stack and all the parameters can be analyzed by the debugger. In the Release
  376. configuration, the exception is thrown.
  377. @param code one of Error::Code
  378. @param msg error message
  379. */
  380. #define CV_Error( code, msg ) cv::error( code, msg, CV_Func, __FILE__, __LINE__ )
  381. /** @brief Call the error handler.
  382. This macro can be used to construct an error message on-fly to include some dynamic information,
  383. for example:
  384. @code
  385. // note the extra parentheses around the formatted text message
  386. CV_Error_( CV_StsOutOfRange,
  387. ("the value at (%d, %d)=%g is out of range", badPt.x, badPt.y, badValue));
  388. @endcode
  389. @param code one of Error::Code
  390. @param args printf-like formatted error message in parentheses
  391. */
  392. #define CV_Error_( code, args ) cv::error( code, cv::format args, CV_Func, __FILE__, __LINE__ )
  393. /** @brief Checks a condition at runtime and throws exception if it fails
  394. The macros CV_Assert (and CV_DbgAssert(expr)) evaluate the specified expression. If it is 0, the macros
  395. raise an error (see cv::error). The macro CV_Assert checks the condition in both Debug and Release
  396. configurations while CV_DbgAssert is only retained in the Debug configuration.
  397. */
  398. #define CV_VA_NUM_ARGS_HELPER(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
  399. #define CV_VA_NUM_ARGS(...) CV_VA_NUM_ARGS_HELPER(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  400. #define CV_Assert_1( expr ) if(!!(expr)) ; else cv::error( cv::Error::StsAssert, #expr, CV_Func, __FILE__, __LINE__ )
  401. #define CV_Assert_2( expr1, expr2 ) CV_Assert_1(expr1); CV_Assert_1(expr2)
  402. #define CV_Assert_3( expr1, expr2, expr3 ) CV_Assert_2(expr1, expr2); CV_Assert_1(expr3)
  403. #define CV_Assert_4( expr1, expr2, expr3, expr4 ) CV_Assert_3(expr1, expr2, expr3); CV_Assert_1(expr4)
  404. #define CV_Assert_5( expr1, expr2, expr3, expr4, expr5 ) CV_Assert_4(expr1, expr2, expr3, expr4); CV_Assert_1(expr5)
  405. #define CV_Assert_6( expr1, expr2, expr3, expr4, expr5, expr6 ) CV_Assert_5(expr1, expr2, expr3, expr4, expr5); CV_Assert_1(expr6)
  406. #define CV_Assert_7( expr1, expr2, expr3, expr4, expr5, expr6, expr7 ) CV_Assert_6(expr1, expr2, expr3, expr4, expr5, expr6 ); CV_Assert_1(expr7)
  407. #define CV_Assert_8( expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8 ) CV_Assert_7(expr1, expr2, expr3, expr4, expr5, expr6, expr7 ); CV_Assert_1(expr8)
  408. #define CV_Assert_9( expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8, expr9 ) CV_Assert_8(expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8 ); CV_Assert_1(expr9)
  409. #define CV_Assert_10( expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8, expr9, expr10 ) CV_Assert_9(expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8, expr9 ); CV_Assert_1(expr10)
  410. #define CV_Assert(...) CVAUX_CONCAT(CV_Assert_, CV_VA_NUM_ARGS(__VA_ARGS__)) (__VA_ARGS__)
  411. /** same as CV_Error(code,msg), but does not return */
  412. #define CV_ErrorNoReturn( code, msg ) cv::errorNoReturn( code, msg, CV_Func, __FILE__, __LINE__ )
  413. /** same as CV_Error_(code,args), but does not return */
  414. #define CV_ErrorNoReturn_( code, args ) cv::errorNoReturn( code, cv::format args, CV_Func, __FILE__, __LINE__ )
  415. #endif // CV_STATIC_ANALYSIS
  416. /** replaced with CV_Assert(expr) in Debug configuration */
  417. #ifdef _DEBUG
  418. # define CV_DbgAssert(expr) CV_Assert(expr)
  419. #else
  420. # define CV_DbgAssert(expr)
  421. #endif
  422. /*
  423. * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
  424. * bit count of A exclusive XOR'ed with B
  425. */
  426. struct CV_EXPORTS Hamming
  427. {
  428. enum { normType = NORM_HAMMING };
  429. typedef unsigned char ValueType;
  430. typedef int ResultType;
  431. /** this will count the bits in a ^ b
  432. */
  433. ResultType operator()( const unsigned char* a, const unsigned char* b, int size ) const;
  434. };
  435. typedef Hamming HammingLUT;
  436. /////////////////////////////////// inline norms ////////////////////////////////////
  437. template<typename _Tp> inline _Tp cv_abs(_Tp x) { return std::abs(x); }
  438. inline int cv_abs(uchar x) { return x; }
  439. inline int cv_abs(schar x) { return std::abs(x); }
  440. inline int cv_abs(ushort x) { return x; }
  441. inline int cv_abs(short x) { return std::abs(x); }
  442. template<typename _Tp, typename _AccTp> static inline
  443. _AccTp normL2Sqr(const _Tp* a, int n)
  444. {
  445. _AccTp s = 0;
  446. int i=0;
  447. #if CV_ENABLE_UNROLLED
  448. for( ; i <= n - 4; i += 4 )
  449. {
  450. _AccTp v0 = a[i], v1 = a[i+1], v2 = a[i+2], v3 = a[i+3];
  451. s += v0*v0 + v1*v1 + v2*v2 + v3*v3;
  452. }
  453. #endif
  454. for( ; i < n; i++ )
  455. {
  456. _AccTp v = a[i];
  457. s += v*v;
  458. }
  459. return s;
  460. }
  461. template<typename _Tp, typename _AccTp> static inline
  462. _AccTp normL1(const _Tp* a, int n)
  463. {
  464. _AccTp s = 0;
  465. int i = 0;
  466. #if CV_ENABLE_UNROLLED
  467. for(; i <= n - 4; i += 4 )
  468. {
  469. s += (_AccTp)cv_abs(a[i]) + (_AccTp)cv_abs(a[i+1]) +
  470. (_AccTp)cv_abs(a[i+2]) + (_AccTp)cv_abs(a[i+3]);
  471. }
  472. #endif
  473. for( ; i < n; i++ )
  474. s += cv_abs(a[i]);
  475. return s;
  476. }
  477. template<typename _Tp, typename _AccTp> static inline
  478. _AccTp normInf(const _Tp* a, int n)
  479. {
  480. _AccTp s = 0;
  481. for( int i = 0; i < n; i++ )
  482. s = std::max(s, (_AccTp)cv_abs(a[i]));
  483. return s;
  484. }
  485. template<typename _Tp, typename _AccTp> static inline
  486. _AccTp normL2Sqr(const _Tp* a, const _Tp* b, int n)
  487. {
  488. _AccTp s = 0;
  489. int i= 0;
  490. #if CV_ENABLE_UNROLLED
  491. for(; i <= n - 4; i += 4 )
  492. {
  493. _AccTp v0 = _AccTp(a[i] - b[i]), v1 = _AccTp(a[i+1] - b[i+1]), v2 = _AccTp(a[i+2] - b[i+2]), v3 = _AccTp(a[i+3] - b[i+3]);
  494. s += v0*v0 + v1*v1 + v2*v2 + v3*v3;
  495. }
  496. #endif
  497. for( ; i < n; i++ )
  498. {
  499. _AccTp v = _AccTp(a[i] - b[i]);
  500. s += v*v;
  501. }
  502. return s;
  503. }
  504. static inline float normL2Sqr(const float* a, const float* b, int n)
  505. {
  506. float s = 0.f;
  507. for( int i = 0; i < n; i++ )
  508. {
  509. float v = a[i] - b[i];
  510. s += v*v;
  511. }
  512. return s;
  513. }
  514. template<typename _Tp, typename _AccTp> static inline
  515. _AccTp normL1(const _Tp* a, const _Tp* b, int n)
  516. {
  517. _AccTp s = 0;
  518. int i= 0;
  519. #if CV_ENABLE_UNROLLED
  520. for(; i <= n - 4; i += 4 )
  521. {
  522. _AccTp v0 = _AccTp(a[i] - b[i]), v1 = _AccTp(a[i+1] - b[i+1]), v2 = _AccTp(a[i+2] - b[i+2]), v3 = _AccTp(a[i+3] - b[i+3]);
  523. s += std::abs(v0) + std::abs(v1) + std::abs(v2) + std::abs(v3);
  524. }
  525. #endif
  526. for( ; i < n; i++ )
  527. {
  528. _AccTp v = _AccTp(a[i] - b[i]);
  529. s += std::abs(v);
  530. }
  531. return s;
  532. }
  533. inline float normL1(const float* a, const float* b, int n)
  534. {
  535. float s = 0.f;
  536. for( int i = 0; i < n; i++ )
  537. {
  538. s += std::abs(a[i] - b[i]);
  539. }
  540. return s;
  541. }
  542. inline int normL1(const uchar* a, const uchar* b, int n)
  543. {
  544. int s = 0;
  545. for( int i = 0; i < n; i++ )
  546. {
  547. s += std::abs(a[i] - b[i]);
  548. }
  549. return s;
  550. }
  551. template<typename _Tp, typename _AccTp> static inline
  552. _AccTp normInf(const _Tp* a, const _Tp* b, int n)
  553. {
  554. _AccTp s = 0;
  555. for( int i = 0; i < n; i++ )
  556. {
  557. _AccTp v0 = a[i] - b[i];
  558. s = std::max(s, std::abs(v0));
  559. }
  560. return s;
  561. }
  562. /** @brief Computes the cube root of an argument.
  563. The function cubeRoot computes \f$\sqrt[3]{\texttt{val}}\f$. Negative arguments are handled correctly.
  564. NaN and Inf are not handled. The accuracy approaches the maximum possible accuracy for
  565. single-precision data.
  566. @param val A function argument.
  567. */
  568. CV_EXPORTS_W float cubeRoot(float val);
  569. /** @brief Calculates the angle of a 2D vector in degrees.
  570. The function fastAtan2 calculates the full-range angle of an input 2D vector. The angle is measured
  571. in degrees and varies from 0 to 360 degrees. The accuracy is about 0.3 degrees.
  572. @param x x-coordinate of the vector.
  573. @param y y-coordinate of the vector.
  574. */
  575. CV_EXPORTS_W float fastAtan2(float y, float x);
  576. /** proxy for hal::LU */
  577. CV_EXPORTS int LU(float* A, size_t astep, int m, float* b, size_t bstep, int n);
  578. /** proxy for hal::LU */
  579. CV_EXPORTS int LU(double* A, size_t astep, int m, double* b, size_t bstep, int n);
  580. /** proxy for hal::Cholesky */
  581. CV_EXPORTS bool Cholesky(float* A, size_t astep, int m, float* b, size_t bstep, int n);
  582. /** proxy for hal::Cholesky */
  583. CV_EXPORTS bool Cholesky(double* A, size_t astep, int m, double* b, size_t bstep, int n);
  584. ////////////////// forward declarations for important OpenCV types //////////////////
  585. //! @cond IGNORED
  586. template<typename _Tp, int cn> class Vec;
  587. template<typename _Tp, int m, int n> class Matx;
  588. template<typename _Tp> class Complex;
  589. template<typename _Tp> class Point_;
  590. template<typename _Tp> class Point3_;
  591. template<typename _Tp> class Size_;
  592. template<typename _Tp> class Rect_;
  593. template<typename _Tp> class Scalar_;
  594. class CV_EXPORTS RotatedRect;
  595. class CV_EXPORTS Range;
  596. class CV_EXPORTS TermCriteria;
  597. class CV_EXPORTS KeyPoint;
  598. class CV_EXPORTS DMatch;
  599. class CV_EXPORTS RNG;
  600. class CV_EXPORTS Mat;
  601. class CV_EXPORTS MatExpr;
  602. class CV_EXPORTS UMat;
  603. class CV_EXPORTS SparseMat;
  604. typedef Mat MatND;
  605. template<typename _Tp> class Mat_;
  606. template<typename _Tp> class SparseMat_;
  607. class CV_EXPORTS MatConstIterator;
  608. class CV_EXPORTS SparseMatIterator;
  609. class CV_EXPORTS SparseMatConstIterator;
  610. template<typename _Tp> class MatIterator_;
  611. template<typename _Tp> class MatConstIterator_;
  612. template<typename _Tp> class SparseMatIterator_;
  613. template<typename _Tp> class SparseMatConstIterator_;
  614. namespace ogl
  615. {
  616. class CV_EXPORTS Buffer;
  617. class CV_EXPORTS Texture2D;
  618. class CV_EXPORTS Arrays;
  619. }
  620. namespace cuda
  621. {
  622. class CV_EXPORTS GpuMat;
  623. class CV_EXPORTS HostMem;
  624. class CV_EXPORTS Stream;
  625. class CV_EXPORTS Event;
  626. }
  627. namespace cudev
  628. {
  629. template <typename _Tp> class GpuMat_;
  630. }
  631. namespace ipp
  632. {
  633. #if OPENCV_ABI_COMPATIBILITY > 300
  634. CV_EXPORTS unsigned long long getIppFeatures();
  635. #else
  636. CV_EXPORTS int getIppFeatures();
  637. #endif
  638. CV_EXPORTS void setIppStatus(int status, const char * const funcname = NULL, const char * const filename = NULL,
  639. int line = 0);
  640. CV_EXPORTS int getIppStatus();
  641. CV_EXPORTS String getIppErrorLocation();
  642. CV_EXPORTS_W bool useIPP();
  643. CV_EXPORTS_W void setUseIPP(bool flag);
  644. CV_EXPORTS_W String getIppVersion();
  645. // IPP Not-Exact mode. This function may force use of IPP then both IPP and OpenCV provide proper results
  646. // but have internal accuracy differences which have to much direct or indirect impact on accuracy tests.
  647. CV_EXPORTS_W bool useIPP_NE();
  648. CV_EXPORTS_W void setUseIPP_NE(bool flag);
  649. } // ipp
  650. //! @endcond
  651. //! @} core_utils
  652. } // cv
  653. #include "opencv2/core/neon_utils.hpp"
  654. #include "opencv2/core/vsx_utils.hpp"
  655. #endif //OPENCV_CORE_BASE_HPP