base.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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_L2SQR}\) }
  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|abcdefgh|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. /*! @brief Signals an error and raises the exception.
  265. By default the function prints information about the error to stderr,
  266. then it either stops if setBreakOnError() had been called before or raises the exception.
  267. It is possible to alternate error processing by using redirectError().
  268. @param _code - error code (Error::Code)
  269. @param _err - error description
  270. @param _func - function name. Available only when the compiler supports getting it
  271. @param _file - source file name where the error has occurred
  272. @param _line - line number in the source file where the error has occurred
  273. @see CV_Error, CV_Error_, CV_Assert, CV_DbgAssert
  274. */
  275. CV_EXPORTS CV_NORETURN void error(int _code, const String& _err, const char* _func, const char* _file, int _line);
  276. #ifdef CV_STATIC_ANALYSIS
  277. // In practice, some macro are not processed correctly (noreturn is not detected).
  278. // We need to use simplified definition for them.
  279. #define CV_Error(code, msg) do { (void)(code); (void)(msg); abort(); } while (0)
  280. #define CV_Error_(code, args) do { (void)(code); (void)(cv::format args); abort(); } while (0)
  281. #define CV_Assert( expr ) do { if (!(expr)) abort(); } while (0)
  282. #else // CV_STATIC_ANALYSIS
  283. /** @brief Call the error handler.
  284. Currently, the error handler prints the error code and the error message to the standard
  285. error stream `stderr`. In the Debug configuration, it then provokes memory access violation, so that
  286. the execution stack and all the parameters can be analyzed by the debugger. In the Release
  287. configuration, the exception is thrown.
  288. @param code one of Error::Code
  289. @param msg error message
  290. */
  291. #define CV_Error( code, msg ) cv::error( code, msg, CV_Func, __FILE__, __LINE__ )
  292. /** @brief Call the error handler.
  293. This macro can be used to construct an error message on-fly to include some dynamic information,
  294. for example:
  295. @code
  296. // note the extra parentheses around the formatted text message
  297. CV_Error_(Error::StsOutOfRange,
  298. ("the value at (%d, %d)=%g is out of range", badPt.x, badPt.y, badValue));
  299. @endcode
  300. @param code one of Error::Code
  301. @param args printf-like formatted error message in parentheses
  302. */
  303. #define CV_Error_( code, args ) cv::error( code, cv::format args, CV_Func, __FILE__, __LINE__ )
  304. /** @brief Checks a condition at runtime and throws exception if it fails
  305. The macros CV_Assert (and CV_DbgAssert(expr)) evaluate the specified expression. If it is 0, the macros
  306. raise an error (see cv::error). The macro CV_Assert checks the condition in both Debug and Release
  307. configurations while CV_DbgAssert is only retained in the Debug configuration.
  308. */
  309. #define CV_Assert( expr ) do { if(!!(expr)) ; else cv::error( cv::Error::StsAssert, #expr, CV_Func, __FILE__, __LINE__ ); } while(0)
  310. #endif // CV_STATIC_ANALYSIS
  311. //! @cond IGNORED
  312. #if !defined(__OPENCV_BUILD) // TODO: backward compatibility only
  313. #ifndef CV_ErrorNoReturn
  314. #define CV_ErrorNoReturn CV_Error
  315. #endif
  316. #ifndef CV_ErrorNoReturn_
  317. #define CV_ErrorNoReturn_ CV_Error_
  318. #endif
  319. #endif
  320. #define CV_Assert_1 CV_Assert
  321. #define CV_Assert_2( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_1( __VA_ARGS__ ))
  322. #define CV_Assert_3( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_2( __VA_ARGS__ ))
  323. #define CV_Assert_4( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_3( __VA_ARGS__ ))
  324. #define CV_Assert_5( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_4( __VA_ARGS__ ))
  325. #define CV_Assert_6( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_5( __VA_ARGS__ ))
  326. #define CV_Assert_7( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_6( __VA_ARGS__ ))
  327. #define CV_Assert_8( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_7( __VA_ARGS__ ))
  328. #define CV_Assert_9( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_8( __VA_ARGS__ ))
  329. #define CV_Assert_10( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_9( __VA_ARGS__ ))
  330. #define CV_Assert_N(...) do { __CV_EXPAND(__CV_CAT(CV_Assert_, __CV_VA_NUM_ARGS(__VA_ARGS__)) (__VA_ARGS__)); } while(0)
  331. //! @endcond
  332. #if defined _DEBUG || defined CV_STATIC_ANALYSIS
  333. # define CV_DbgAssert(expr) CV_Assert(expr)
  334. #else
  335. /** replaced with CV_Assert(expr) in Debug configuration */
  336. # define CV_DbgAssert(expr)
  337. #endif
  338. /*
  339. * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
  340. * bit count of A exclusive XOR'ed with B
  341. */
  342. struct CV_EXPORTS Hamming
  343. {
  344. static const NormTypes normType = NORM_HAMMING;
  345. typedef unsigned char ValueType;
  346. typedef int ResultType;
  347. /** this will count the bits in a ^ b
  348. */
  349. ResultType operator()( const unsigned char* a, const unsigned char* b, int size ) const;
  350. };
  351. typedef Hamming HammingLUT;
  352. /////////////////////////////////// inline norms ////////////////////////////////////
  353. template<typename _Tp> inline _Tp cv_abs(_Tp x) { return std::abs(x); }
  354. inline int cv_abs(uchar x) { return x; }
  355. inline int cv_abs(schar x) { return std::abs(x); }
  356. inline int cv_abs(ushort x) { return x; }
  357. inline int cv_abs(short x) { return std::abs(x); }
  358. template<typename _Tp, typename _AccTp> static inline
  359. _AccTp normL2Sqr(const _Tp* a, int n)
  360. {
  361. _AccTp s = 0;
  362. int i=0;
  363. #if CV_ENABLE_UNROLLED
  364. for( ; i <= n - 4; i += 4 )
  365. {
  366. _AccTp v0 = a[i], v1 = a[i+1], v2 = a[i+2], v3 = a[i+3];
  367. s += v0*v0 + v1*v1 + v2*v2 + v3*v3;
  368. }
  369. #endif
  370. for( ; i < n; i++ )
  371. {
  372. _AccTp v = a[i];
  373. s += v*v;
  374. }
  375. return s;
  376. }
  377. template<typename _Tp, typename _AccTp> static inline
  378. _AccTp normL1(const _Tp* a, int n)
  379. {
  380. _AccTp s = 0;
  381. int i = 0;
  382. #if CV_ENABLE_UNROLLED
  383. for(; i <= n - 4; i += 4 )
  384. {
  385. s += (_AccTp)cv_abs(a[i]) + (_AccTp)cv_abs(a[i+1]) +
  386. (_AccTp)cv_abs(a[i+2]) + (_AccTp)cv_abs(a[i+3]);
  387. }
  388. #endif
  389. for( ; i < n; i++ )
  390. s += cv_abs(a[i]);
  391. return s;
  392. }
  393. template<typename _Tp, typename _AccTp> static inline
  394. _AccTp normInf(const _Tp* a, int n)
  395. {
  396. _AccTp s = 0;
  397. for( int i = 0; i < n; i++ )
  398. s = std::max(s, (_AccTp)cv_abs(a[i]));
  399. return s;
  400. }
  401. template<typename _Tp, typename _AccTp> static inline
  402. _AccTp normL2Sqr(const _Tp* a, const _Tp* b, int n)
  403. {
  404. _AccTp s = 0;
  405. int i= 0;
  406. #if CV_ENABLE_UNROLLED
  407. for(; i <= n - 4; i += 4 )
  408. {
  409. _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]);
  410. s += v0*v0 + v1*v1 + v2*v2 + v3*v3;
  411. }
  412. #endif
  413. for( ; i < n; i++ )
  414. {
  415. _AccTp v = _AccTp(a[i] - b[i]);
  416. s += v*v;
  417. }
  418. return s;
  419. }
  420. static inline float normL2Sqr(const float* a, const float* b, int n)
  421. {
  422. float s = 0.f;
  423. for( int i = 0; i < n; i++ )
  424. {
  425. float v = a[i] - b[i];
  426. s += v*v;
  427. }
  428. return s;
  429. }
  430. template<typename _Tp, typename _AccTp> static inline
  431. _AccTp normL1(const _Tp* a, const _Tp* b, int n)
  432. {
  433. _AccTp s = 0;
  434. int i= 0;
  435. #if CV_ENABLE_UNROLLED
  436. for(; i <= n - 4; i += 4 )
  437. {
  438. _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]);
  439. s += std::abs(v0) + std::abs(v1) + std::abs(v2) + std::abs(v3);
  440. }
  441. #endif
  442. for( ; i < n; i++ )
  443. {
  444. _AccTp v = _AccTp(a[i] - b[i]);
  445. s += std::abs(v);
  446. }
  447. return s;
  448. }
  449. inline float normL1(const float* a, const float* b, int n)
  450. {
  451. float s = 0.f;
  452. for( int i = 0; i < n; i++ )
  453. {
  454. s += std::abs(a[i] - b[i]);
  455. }
  456. return s;
  457. }
  458. inline int normL1(const uchar* a, const uchar* b, int n)
  459. {
  460. int s = 0;
  461. for( int i = 0; i < n; i++ )
  462. {
  463. s += std::abs(a[i] - b[i]);
  464. }
  465. return s;
  466. }
  467. template<typename _Tp, typename _AccTp> static inline
  468. _AccTp normInf(const _Tp* a, const _Tp* b, int n)
  469. {
  470. _AccTp s = 0;
  471. for( int i = 0; i < n; i++ )
  472. {
  473. _AccTp v0 = a[i] - b[i];
  474. s = std::max(s, std::abs(v0));
  475. }
  476. return s;
  477. }
  478. /** @brief Computes the cube root of an argument.
  479. The function cubeRoot computes \f$\sqrt[3]{\texttt{val}}\f$. Negative arguments are handled correctly.
  480. NaN and Inf are not handled. The accuracy approaches the maximum possible accuracy for
  481. single-precision data.
  482. @param val A function argument.
  483. */
  484. CV_EXPORTS_W float cubeRoot(float val);
  485. /** @brief Calculates the angle of a 2D vector in degrees.
  486. The function fastAtan2 calculates the full-range angle of an input 2D vector. The angle is measured
  487. in degrees and varies from 0 to 360 degrees. The accuracy is about 0.3 degrees.
  488. @param x x-coordinate of the vector.
  489. @param y y-coordinate of the vector.
  490. */
  491. CV_EXPORTS_W float fastAtan2(float y, float x);
  492. /** proxy for hal::LU */
  493. CV_EXPORTS int LU(float* A, size_t astep, int m, float* b, size_t bstep, int n);
  494. /** proxy for hal::LU */
  495. CV_EXPORTS int LU(double* A, size_t astep, int m, double* b, size_t bstep, int n);
  496. /** proxy for hal::Cholesky */
  497. CV_EXPORTS bool Cholesky(float* A, size_t astep, int m, float* b, size_t bstep, int n);
  498. /** proxy for hal::Cholesky */
  499. CV_EXPORTS bool Cholesky(double* A, size_t astep, int m, double* b, size_t bstep, int n);
  500. ////////////////// forward declarations for important OpenCV types //////////////////
  501. //! @cond IGNORED
  502. template<typename _Tp, int cn> class Vec;
  503. template<typename _Tp, int m, int n> class Matx;
  504. template<typename _Tp> class Complex;
  505. template<typename _Tp> class Point_;
  506. template<typename _Tp> class Point3_;
  507. template<typename _Tp> class Size_;
  508. template<typename _Tp> class Rect_;
  509. template<typename _Tp> class Scalar_;
  510. class CV_EXPORTS RotatedRect;
  511. class CV_EXPORTS Range;
  512. class CV_EXPORTS TermCriteria;
  513. class CV_EXPORTS KeyPoint;
  514. class CV_EXPORTS DMatch;
  515. class CV_EXPORTS RNG;
  516. class CV_EXPORTS Mat;
  517. class CV_EXPORTS MatExpr;
  518. class CV_EXPORTS UMat;
  519. class CV_EXPORTS SparseMat;
  520. typedef Mat MatND;
  521. template<typename _Tp> class Mat_;
  522. template<typename _Tp> class SparseMat_;
  523. class CV_EXPORTS MatConstIterator;
  524. class CV_EXPORTS SparseMatIterator;
  525. class CV_EXPORTS SparseMatConstIterator;
  526. template<typename _Tp> class MatIterator_;
  527. template<typename _Tp> class MatConstIterator_;
  528. template<typename _Tp> class SparseMatIterator_;
  529. template<typename _Tp> class SparseMatConstIterator_;
  530. namespace ogl
  531. {
  532. class CV_EXPORTS Buffer;
  533. class CV_EXPORTS Texture2D;
  534. class CV_EXPORTS Arrays;
  535. }
  536. namespace cuda
  537. {
  538. class CV_EXPORTS GpuMat;
  539. class CV_EXPORTS HostMem;
  540. class CV_EXPORTS Stream;
  541. class CV_EXPORTS Event;
  542. }
  543. namespace cudev
  544. {
  545. template <typename _Tp> class GpuMat_;
  546. }
  547. namespace ipp
  548. {
  549. CV_EXPORTS unsigned long long getIppFeatures();
  550. CV_EXPORTS void setIppStatus(int status, const char * const funcname = NULL, const char * const filename = NULL,
  551. int line = 0);
  552. CV_EXPORTS int getIppStatus();
  553. CV_EXPORTS String getIppErrorLocation();
  554. CV_EXPORTS_W bool useIPP();
  555. CV_EXPORTS_W void setUseIPP(bool flag);
  556. CV_EXPORTS_W String getIppVersion();
  557. // IPP Not-Exact mode. This function may force use of IPP then both IPP and OpenCV provide proper results
  558. // but have internal accuracy differences which have too much direct or indirect impact on accuracy tests.
  559. CV_EXPORTS_W bool useIPP_NotExact();
  560. CV_EXPORTS_W void setUseIPP_NotExact(bool flag);
  561. #ifndef DISABLE_OPENCV_3_COMPATIBILITY
  562. static inline bool useIPP_NE() { return useIPP_NotExact(); }
  563. static inline void setUseIPP_NE(bool flag) { setUseIPP_NotExact(flag); }
  564. #endif
  565. } // ipp
  566. //! @endcond
  567. //! @} core_utils
  568. } // cv
  569. #include "opencv2/core/neon_utils.hpp"
  570. #include "opencv2/core/vsx_utils.hpp"
  571. #include "opencv2/core/check.hpp"
  572. #endif //OPENCV_CORE_BASE_HPP