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