utility.hpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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) 2015, 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_UTILITY_H
  45. #define OPENCV_CORE_UTILITY_H
  46. #ifndef __cplusplus
  47. # error utility.hpp header must be compiled as C++
  48. #endif
  49. #if defined(check)
  50. # warning Detected Apple 'check' macro definition, it can cause build conflicts. Please, include this header before any Apple headers.
  51. #endif
  52. #include "opencv2/core.hpp"
  53. #include <ostream>
  54. #ifdef CV_CXX11
  55. #include <functional>
  56. #endif
  57. namespace cv
  58. {
  59. #ifdef CV_COLLECT_IMPL_DATA
  60. CV_EXPORTS void setImpl(int flags); // set implementation flags and reset storage arrays
  61. CV_EXPORTS void addImpl(int flag, const char* func = 0); // add implementation and function name to storage arrays
  62. // Get stored implementation flags and functions names arrays
  63. // Each implementation entry correspond to function name entry, so you can find which implementation was executed in which function
  64. CV_EXPORTS int getImpl(std::vector<int> &impl, std::vector<String> &funName);
  65. CV_EXPORTS bool useCollection(); // return implementation collection state
  66. CV_EXPORTS void setUseCollection(bool flag); // set implementation collection state
  67. #define CV_IMPL_PLAIN 0x01 // native CPU OpenCV implementation
  68. #define CV_IMPL_OCL 0x02 // OpenCL implementation
  69. #define CV_IMPL_IPP 0x04 // IPP implementation
  70. #define CV_IMPL_MT 0x10 // multithreaded implementation
  71. #define CV_IMPL_ADD(impl) \
  72. if(cv::useCollection()) \
  73. { \
  74. cv::addImpl(impl, CV_Func); \
  75. }
  76. #else
  77. #define CV_IMPL_ADD(impl)
  78. #endif
  79. //! @addtogroup core_utils
  80. //! @{
  81. /** @brief Automatically Allocated Buffer Class
  82. The class is used for temporary buffers in functions and methods.
  83. If a temporary buffer is usually small (a few K's of memory),
  84. but its size depends on the parameters, it makes sense to create a small
  85. fixed-size array on stack and use it if it's large enough. If the required buffer size
  86. is larger than the fixed size, another buffer of sufficient size is allocated dynamically
  87. and released after the processing. Therefore, in typical cases, when the buffer size is small,
  88. there is no overhead associated with malloc()/free().
  89. At the same time, there is no limit on the size of processed data.
  90. This is what AutoBuffer does. The template takes 2 parameters - type of the buffer elements and
  91. the number of stack-allocated elements. Here is how the class is used:
  92. \code
  93. void my_func(const cv::Mat& m)
  94. {
  95. cv::AutoBuffer<float> buf(1000); // create automatic buffer containing 1000 floats
  96. buf.allocate(m.rows); // if m.rows <= 1000, the pre-allocated buffer is used,
  97. // otherwise the buffer of "m.rows" floats will be allocated
  98. // dynamically and deallocated in cv::AutoBuffer destructor
  99. ...
  100. }
  101. \endcode
  102. */
  103. #ifdef OPENCV_ENABLE_MEMORY_SANITIZER
  104. template<typename _Tp, size_t fixed_size = 0> class AutoBuffer
  105. #else
  106. template<typename _Tp, size_t fixed_size = 1024/sizeof(_Tp)+8> class AutoBuffer
  107. #endif
  108. {
  109. public:
  110. typedef _Tp value_type;
  111. //! the default constructor
  112. AutoBuffer();
  113. //! constructor taking the real buffer size
  114. explicit AutoBuffer(size_t _size);
  115. //! the copy constructor
  116. AutoBuffer(const AutoBuffer<_Tp, fixed_size>& buf);
  117. //! the assignment operator
  118. AutoBuffer<_Tp, fixed_size>& operator = (const AutoBuffer<_Tp, fixed_size>& buf);
  119. //! destructor. calls deallocate()
  120. ~AutoBuffer();
  121. //! allocates the new buffer of size _size. if the _size is small enough, stack-allocated buffer is used
  122. void allocate(size_t _size);
  123. //! deallocates the buffer if it was dynamically allocated
  124. void deallocate();
  125. //! resizes the buffer and preserves the content
  126. void resize(size_t _size);
  127. //! returns the current buffer size
  128. size_t size() const;
  129. //! returns pointer to the real buffer, stack-allocated or heap-allocated
  130. inline _Tp* data() { return ptr; }
  131. //! returns read-only pointer to the real buffer, stack-allocated or heap-allocated
  132. inline const _Tp* data() const { return ptr; }
  133. #if !defined(OPENCV_DISABLE_DEPRECATED_COMPATIBILITY) // use to .data() calls instead
  134. //! returns pointer to the real buffer, stack-allocated or heap-allocated
  135. operator _Tp* () { return ptr; }
  136. //! returns read-only pointer to the real buffer, stack-allocated or heap-allocated
  137. operator const _Tp* () const { return ptr; }
  138. #else
  139. //! returns a reference to the element at specified location. No bounds checking is performed in Release builds.
  140. inline _Tp& operator[] (size_t i) { CV_DbgCheckLT(i, sz, "out of range"); return ptr[i]; }
  141. //! returns a reference to the element at specified location. No bounds checking is performed in Release builds.
  142. inline const _Tp& operator[] (size_t i) const { CV_DbgCheckLT(i, sz, "out of range"); return ptr[i]; }
  143. #endif
  144. protected:
  145. //! pointer to the real buffer, can point to buf if the buffer is small enough
  146. _Tp* ptr;
  147. //! size of the real buffer
  148. size_t sz;
  149. //! pre-allocated buffer. At least 1 element to confirm C++ standard requirements
  150. _Tp buf[(fixed_size > 0) ? fixed_size : 1];
  151. };
  152. /** @brief Sets/resets the break-on-error mode.
  153. When the break-on-error mode is set, the default error handler issues a hardware exception, which
  154. can make debugging more convenient.
  155. \return the previous state
  156. */
  157. CV_EXPORTS bool setBreakOnError(bool flag);
  158. extern "C" typedef int (*ErrorCallback)( int status, const char* func_name,
  159. const char* err_msg, const char* file_name,
  160. int line, void* userdata );
  161. /** @brief Sets the new error handler and the optional user data.
  162. The function sets the new error handler, called from cv::error().
  163. \param errCallback the new error handler. If NULL, the default error handler is used.
  164. \param userdata the optional user data pointer, passed to the callback.
  165. \param prevUserdata the optional output parameter where the previous user data pointer is stored
  166. \return the previous error handler
  167. */
  168. CV_EXPORTS ErrorCallback redirectError( ErrorCallback errCallback, void* userdata=0, void** prevUserdata=0);
  169. CV_EXPORTS String tempfile( const char* suffix = 0);
  170. CV_EXPORTS void glob(String pattern, std::vector<String>& result, bool recursive = false);
  171. /** @brief OpenCV will try to set the number of threads for the next parallel region.
  172. If threads == 0, OpenCV will disable threading optimizations and run all it's functions
  173. sequentially. Passing threads \< 0 will reset threads number to system default. This function must
  174. be called outside of parallel region.
  175. OpenCV will try to run its functions with specified threads number, but some behaviour differs from
  176. framework:
  177. - `TBB` - User-defined parallel constructions will run with the same threads number, if
  178. another is not specified. If later on user creates his own scheduler, OpenCV will use it.
  179. - `OpenMP` - No special defined behaviour.
  180. - `Concurrency` - If threads == 1, OpenCV will disable threading optimizations and run its
  181. functions sequentially.
  182. - `GCD` - Supports only values \<= 0.
  183. - `C=` - No special defined behaviour.
  184. @param nthreads Number of threads used by OpenCV.
  185. @sa getNumThreads, getThreadNum
  186. */
  187. CV_EXPORTS_W void setNumThreads(int nthreads);
  188. /** @brief Returns the number of threads used by OpenCV for parallel regions.
  189. Always returns 1 if OpenCV is built without threading support.
  190. The exact meaning of return value depends on the threading framework used by OpenCV library:
  191. - `TBB` - The number of threads, that OpenCV will try to use for parallel regions. If there is
  192. any tbb::thread_scheduler_init in user code conflicting with OpenCV, then function returns
  193. default number of threads used by TBB library.
  194. - `OpenMP` - An upper bound on the number of threads that could be used to form a new team.
  195. - `Concurrency` - The number of threads, that OpenCV will try to use for parallel regions.
  196. - `GCD` - Unsupported; returns the GCD thread pool limit (512) for compatibility.
  197. - `C=` - The number of threads, that OpenCV will try to use for parallel regions, if before
  198. called setNumThreads with threads \> 0, otherwise returns the number of logical CPUs,
  199. available for the process.
  200. @sa setNumThreads, getThreadNum
  201. */
  202. CV_EXPORTS_W int getNumThreads();
  203. /** @brief Returns the index of the currently executed thread within the current parallel region. Always
  204. returns 0 if called outside of parallel region.
  205. @deprecated Current implementation doesn't corresponding to this documentation.
  206. The exact meaning of the return value depends on the threading framework used by OpenCV library:
  207. - `TBB` - Unsupported with current 4.1 TBB release. Maybe will be supported in future.
  208. - `OpenMP` - The thread number, within the current team, of the calling thread.
  209. - `Concurrency` - An ID for the virtual processor that the current context is executing on (0
  210. for master thread and unique number for others, but not necessary 1,2,3,...).
  211. - `GCD` - System calling thread's ID. Never returns 0 inside parallel region.
  212. - `C=` - The index of the current parallel task.
  213. @sa setNumThreads, getNumThreads
  214. */
  215. CV_EXPORTS_W int getThreadNum();
  216. /** @brief Returns full configuration time cmake output.
  217. Returned value is raw cmake output including version control system revision, compiler version,
  218. compiler flags, enabled modules and third party libraries, etc. Output format depends on target
  219. architecture.
  220. */
  221. CV_EXPORTS_W const String& getBuildInformation();
  222. /** @brief Returns library version string
  223. For example "3.4.1-dev".
  224. @sa getMajorVersion, getMinorVersion, getRevisionVersion
  225. */
  226. CV_EXPORTS_W String getVersionString();
  227. /** @brief Returns major library version */
  228. CV_EXPORTS_W int getVersionMajor();
  229. /** @brief Returns minor library version */
  230. CV_EXPORTS_W int getVersionMinor();
  231. /** @brief Returns revision field of the library version */
  232. CV_EXPORTS_W int getVersionRevision();
  233. /** @brief Returns the number of ticks.
  234. The function returns the number of ticks after the certain event (for example, when the machine was
  235. turned on). It can be used to initialize RNG or to measure a function execution time by reading the
  236. tick count before and after the function call.
  237. @sa getTickFrequency, TickMeter
  238. */
  239. CV_EXPORTS_W int64 getTickCount();
  240. /** @brief Returns the number of ticks per second.
  241. The function returns the number of ticks per second. That is, the following code computes the
  242. execution time in seconds:
  243. @code
  244. double t = (double)getTickCount();
  245. // do something ...
  246. t = ((double)getTickCount() - t)/getTickFrequency();
  247. @endcode
  248. @sa getTickCount, TickMeter
  249. */
  250. CV_EXPORTS_W double getTickFrequency();
  251. /** @brief a Class to measure passing time.
  252. The class computes passing time by counting the number of ticks per second. That is, the following code computes the
  253. execution time in seconds:
  254. @code
  255. TickMeter tm;
  256. tm.start();
  257. // do something ...
  258. tm.stop();
  259. std::cout << tm.getTimeSec();
  260. @endcode
  261. It is also possible to compute the average time over multiple runs:
  262. @code
  263. TickMeter tm;
  264. for (int i = 0; i < 100; i++)
  265. {
  266. tm.start();
  267. // do something ...
  268. tm.stop();
  269. }
  270. double average_time = tm.getTimeSec() / tm.getCounter();
  271. std::cout << "Average time in second per iteration is: " << average_time << std::endl;
  272. @endcode
  273. @sa getTickCount, getTickFrequency
  274. */
  275. class CV_EXPORTS_W TickMeter
  276. {
  277. public:
  278. //! the default constructor
  279. CV_WRAP TickMeter()
  280. {
  281. reset();
  282. }
  283. /**
  284. starts counting ticks.
  285. */
  286. CV_WRAP void start()
  287. {
  288. startTime = cv::getTickCount();
  289. }
  290. /**
  291. stops counting ticks.
  292. */
  293. CV_WRAP void stop()
  294. {
  295. int64 time = cv::getTickCount();
  296. if (startTime == 0)
  297. return;
  298. ++counter;
  299. sumTime += (time - startTime);
  300. startTime = 0;
  301. }
  302. /**
  303. returns counted ticks.
  304. */
  305. CV_WRAP int64 getTimeTicks() const
  306. {
  307. return sumTime;
  308. }
  309. /**
  310. returns passed time in microseconds.
  311. */
  312. CV_WRAP double getTimeMicro() const
  313. {
  314. return getTimeMilli()*1e3;
  315. }
  316. /**
  317. returns passed time in milliseconds.
  318. */
  319. CV_WRAP double getTimeMilli() const
  320. {
  321. return getTimeSec()*1e3;
  322. }
  323. /**
  324. returns passed time in seconds.
  325. */
  326. CV_WRAP double getTimeSec() const
  327. {
  328. return (double)getTimeTicks() / getTickFrequency();
  329. }
  330. /**
  331. returns internal counter value.
  332. */
  333. CV_WRAP int64 getCounter() const
  334. {
  335. return counter;
  336. }
  337. /**
  338. resets internal values.
  339. */
  340. CV_WRAP void reset()
  341. {
  342. startTime = 0;
  343. sumTime = 0;
  344. counter = 0;
  345. }
  346. private:
  347. int64 counter;
  348. int64 sumTime;
  349. int64 startTime;
  350. };
  351. /** @brief output operator
  352. @code
  353. TickMeter tm;
  354. tm.start();
  355. // do something ...
  356. tm.stop();
  357. std::cout << tm;
  358. @endcode
  359. */
  360. static inline
  361. std::ostream& operator << (std::ostream& out, const TickMeter& tm)
  362. {
  363. return out << tm.getTimeSec() << "sec";
  364. }
  365. /** @brief Returns the number of CPU ticks.
  366. The function returns the current number of CPU ticks on some architectures (such as x86, x64,
  367. PowerPC). On other platforms the function is equivalent to getTickCount. It can also be used for
  368. very accurate time measurements, as well as for RNG initialization. Note that in case of multi-CPU
  369. systems a thread, from which getCPUTickCount is called, can be suspended and resumed at another CPU
  370. with its own counter. So, theoretically (and practically) the subsequent calls to the function do
  371. not necessary return the monotonously increasing values. Also, since a modern CPU varies the CPU
  372. frequency depending on the load, the number of CPU clocks spent in some code cannot be directly
  373. converted to time units. Therefore, getTickCount is generally a preferable solution for measuring
  374. execution time.
  375. */
  376. CV_EXPORTS_W int64 getCPUTickCount();
  377. /** @brief Returns true if the specified feature is supported by the host hardware.
  378. The function returns true if the host hardware supports the specified feature. When user calls
  379. setUseOptimized(false), the subsequent calls to checkHardwareSupport() will return false until
  380. setUseOptimized(true) is called. This way user can dynamically switch on and off the optimized code
  381. in OpenCV.
  382. @param feature The feature of interest, one of cv::CpuFeatures
  383. */
  384. CV_EXPORTS_W bool checkHardwareSupport(int feature);
  385. /** @brief Returns feature name by ID
  386. Returns empty string if feature is not defined
  387. */
  388. CV_EXPORTS_W String getHardwareFeatureName(int feature);
  389. /** @brief Returns list of CPU features enabled during compilation.
  390. Returned value is a string containing space separated list of CPU features with following markers:
  391. - no markers - baseline features
  392. - prefix `*` - features enabled in dispatcher
  393. - suffix `?` - features enabled but not available in HW
  394. Example: `SSE SSE2 SSE3 *SSE4.1 *SSE4.2 *FP16 *AVX *AVX2 *AVX512-SKX?`
  395. */
  396. CV_EXPORTS std::string getCPUFeaturesLine();
  397. /** @brief Returns the number of logical CPUs available for the process.
  398. */
  399. CV_EXPORTS_W int getNumberOfCPUs();
  400. /** @brief Aligns a pointer to the specified number of bytes.
  401. The function returns the aligned pointer of the same type as the input pointer:
  402. \f[\texttt{(_Tp*)(((size_t)ptr + n-1) & -n)}\f]
  403. @param ptr Aligned pointer.
  404. @param n Alignment size that must be a power of two.
  405. */
  406. template<typename _Tp> static inline _Tp* alignPtr(_Tp* ptr, int n=(int)sizeof(_Tp))
  407. {
  408. CV_DbgAssert((n & (n - 1)) == 0); // n is a power of 2
  409. return (_Tp*)(((size_t)ptr + n-1) & -n);
  410. }
  411. /** @brief Aligns a buffer size to the specified number of bytes.
  412. The function returns the minimum number that is greater than or equal to sz and is divisible by n :
  413. \f[\texttt{(sz + n-1) & -n}\f]
  414. @param sz Buffer size to align.
  415. @param n Alignment size that must be a power of two.
  416. */
  417. static inline size_t alignSize(size_t sz, int n)
  418. {
  419. CV_DbgAssert((n & (n - 1)) == 0); // n is a power of 2
  420. return (sz + n-1) & -n;
  421. }
  422. /** @brief Integer division with result round up.
  423. Use this function instead of `ceil((float)a / b)` expressions.
  424. @sa alignSize
  425. */
  426. static inline int divUp(int a, unsigned int b)
  427. {
  428. CV_DbgAssert(a >= 0);
  429. return (a + b - 1) / b;
  430. }
  431. /** @overload */
  432. static inline size_t divUp(size_t a, unsigned int b)
  433. {
  434. return (a + b - 1) / b;
  435. }
  436. /** @brief Round first value up to the nearest multiple of second value.
  437. Use this function instead of `ceil((float)a / b) * b` expressions.
  438. @sa divUp
  439. */
  440. static inline int roundUp(int a, unsigned int b)
  441. {
  442. CV_DbgAssert(a >= 0);
  443. return a + b - 1 - (a + b -1) % b;
  444. }
  445. /** @overload */
  446. static inline size_t roundUp(size_t a, unsigned int b)
  447. {
  448. return a + b - 1 - (a + b - 1) % b;
  449. }
  450. /** @brief Enables or disables the optimized code.
  451. The function can be used to dynamically turn on and off optimized dispatched code (code that uses SSE4.2, AVX/AVX2,
  452. and other instructions on the platforms that support it). It sets a global flag that is further
  453. checked by OpenCV functions. Since the flag is not checked in the inner OpenCV loops, it is only
  454. safe to call the function on the very top level in your application where you can be sure that no
  455. other OpenCV function is currently executed.
  456. By default, the optimized code is enabled unless you disable it in CMake. The current status can be
  457. retrieved using useOptimized.
  458. @param onoff The boolean flag specifying whether the optimized code should be used (onoff=true)
  459. or not (onoff=false).
  460. */
  461. CV_EXPORTS_W void setUseOptimized(bool onoff);
  462. /** @brief Returns the status of optimized code usage.
  463. The function returns true if the optimized code is enabled. Otherwise, it returns false.
  464. */
  465. CV_EXPORTS_W bool useOptimized();
  466. static inline size_t getElemSize(int type) { return (size_t)CV_ELEM_SIZE(type); }
  467. /////////////////////////////// Parallel Primitives //////////////////////////////////
  468. /** @brief Base class for parallel data processors
  469. */
  470. class CV_EXPORTS ParallelLoopBody
  471. {
  472. public:
  473. virtual ~ParallelLoopBody();
  474. virtual void operator() (const Range& range) const = 0;
  475. };
  476. /** @brief Parallel data processor
  477. */
  478. CV_EXPORTS void parallel_for_(const Range& range, const ParallelLoopBody& body, double nstripes=-1.);
  479. #ifdef CV_CXX11
  480. class ParallelLoopBodyLambdaWrapper : public ParallelLoopBody
  481. {
  482. private:
  483. std::function<void(const Range&)> m_functor;
  484. public:
  485. ParallelLoopBodyLambdaWrapper(std::function<void(const Range&)> functor) :
  486. m_functor(functor)
  487. { }
  488. virtual void operator() (const cv::Range& range) const CV_OVERRIDE
  489. {
  490. m_functor(range);
  491. }
  492. };
  493. inline void parallel_for_(const Range& range, std::function<void(const Range&)> functor, double nstripes=-1.)
  494. {
  495. parallel_for_(range, ParallelLoopBodyLambdaWrapper(functor), nstripes);
  496. }
  497. #endif
  498. /////////////////////////////// forEach method of cv::Mat ////////////////////////////
  499. template<typename _Tp, typename Functor> inline
  500. void Mat::forEach_impl(const Functor& operation) {
  501. if (false) {
  502. operation(*reinterpret_cast<_Tp*>(0), reinterpret_cast<int*>(0));
  503. // If your compiler fails in this line.
  504. // Please check that your functor signature is
  505. // (_Tp&, const int*) <- multi-dimensional
  506. // or (_Tp&, void*) <- in case you don't need current idx.
  507. }
  508. CV_Assert(this->total() / this->size[this->dims - 1] <= INT_MAX);
  509. const int LINES = static_cast<int>(this->total() / this->size[this->dims - 1]);
  510. class PixelOperationWrapper :public ParallelLoopBody
  511. {
  512. public:
  513. PixelOperationWrapper(Mat_<_Tp>* const frame, const Functor& _operation)
  514. : mat(frame), op(_operation) {}
  515. virtual ~PixelOperationWrapper(){}
  516. // ! Overloaded virtual operator
  517. // convert range call to row call.
  518. virtual void operator()(const Range &range) const CV_OVERRIDE
  519. {
  520. const int DIMS = mat->dims;
  521. const int COLS = mat->size[DIMS - 1];
  522. if (DIMS <= 2) {
  523. for (int row = range.start; row < range.end; ++row) {
  524. this->rowCall2(row, COLS);
  525. }
  526. } else {
  527. std::vector<int> idx(DIMS); /// idx is modified in this->rowCall
  528. idx[DIMS - 2] = range.start - 1;
  529. for (int line_num = range.start; line_num < range.end; ++line_num) {
  530. idx[DIMS - 2]++;
  531. for (int i = DIMS - 2; i >= 0; --i) {
  532. if (idx[i] >= mat->size[i]) {
  533. idx[i - 1] += idx[i] / mat->size[i];
  534. idx[i] %= mat->size[i];
  535. continue; // carry-over;
  536. }
  537. else {
  538. break;
  539. }
  540. }
  541. this->rowCall(&idx[0], COLS, DIMS);
  542. }
  543. }
  544. }
  545. private:
  546. Mat_<_Tp>* const mat;
  547. const Functor op;
  548. // ! Call operator for each elements in this row.
  549. inline void rowCall(int* const idx, const int COLS, const int DIMS) const {
  550. int &col = idx[DIMS - 1];
  551. col = 0;
  552. _Tp* pixel = &(mat->template at<_Tp>(idx));
  553. while (col < COLS) {
  554. op(*pixel, const_cast<const int*>(idx));
  555. pixel++; col++;
  556. }
  557. col = 0;
  558. }
  559. // ! Call operator for each elements in this row. 2d mat special version.
  560. inline void rowCall2(const int row, const int COLS) const {
  561. union Index{
  562. int body[2];
  563. operator const int*() const {
  564. return reinterpret_cast<const int*>(this);
  565. }
  566. int& operator[](const int i) {
  567. return body[i];
  568. }
  569. } idx = {{row, 0}};
  570. // Special union is needed to avoid
  571. // "error: array subscript is above array bounds [-Werror=array-bounds]"
  572. // when call the functor `op` such that access idx[3].
  573. _Tp* pixel = &(mat->template at<_Tp>(idx));
  574. const _Tp* const pixel_end = pixel + COLS;
  575. while(pixel < pixel_end) {
  576. op(*pixel++, static_cast<const int*>(idx));
  577. idx[1]++;
  578. }
  579. }
  580. PixelOperationWrapper& operator=(const PixelOperationWrapper &) {
  581. CV_Assert(false);
  582. // We can not remove this implementation because Visual Studio warning C4822.
  583. return *this;
  584. }
  585. };
  586. parallel_for_(cv::Range(0, LINES), PixelOperationWrapper(reinterpret_cast<Mat_<_Tp>*>(this), operation));
  587. }
  588. /////////////////////////// Synchronization Primitives ///////////////////////////////
  589. class CV_EXPORTS Mutex
  590. {
  591. public:
  592. Mutex();
  593. ~Mutex();
  594. Mutex(const Mutex& m);
  595. Mutex& operator = (const Mutex& m);
  596. void lock();
  597. bool trylock();
  598. void unlock();
  599. struct Impl;
  600. protected:
  601. Impl* impl;
  602. };
  603. class CV_EXPORTS AutoLock
  604. {
  605. public:
  606. AutoLock(Mutex& m) : mutex(&m) { mutex->lock(); }
  607. ~AutoLock() { mutex->unlock(); }
  608. protected:
  609. Mutex* mutex;
  610. private:
  611. AutoLock(const AutoLock&);
  612. AutoLock& operator = (const AutoLock&);
  613. };
  614. // TLS interface
  615. class CV_EXPORTS TLSDataContainer
  616. {
  617. protected:
  618. TLSDataContainer();
  619. virtual ~TLSDataContainer();
  620. void gatherData(std::vector<void*> &data) const;
  621. #if OPENCV_ABI_COMPATIBILITY > 300
  622. void* getData() const;
  623. void release();
  624. private:
  625. #else
  626. void release();
  627. public:
  628. void* getData() const;
  629. #endif
  630. virtual void* createDataInstance() const = 0;
  631. virtual void deleteDataInstance(void* pData) const = 0;
  632. int key_;
  633. public:
  634. void cleanup(); //! Release created TLS data container objects. It is similar to release() call, but it keeps TLS container valid.
  635. };
  636. // Main TLS data class
  637. template <typename T>
  638. class TLSData : protected TLSDataContainer
  639. {
  640. public:
  641. inline TLSData() {}
  642. inline ~TLSData() { release(); } // Release key and delete associated data
  643. inline T* get() const { return (T*)getData(); } // Get data associated with key
  644. inline T& getRef() const { T* ptr = (T*)getData(); CV_Assert(ptr); return *ptr; } // Get data associated with key
  645. // Get data from all threads
  646. inline void gather(std::vector<T*> &data) const
  647. {
  648. std::vector<void*> &dataVoid = reinterpret_cast<std::vector<void*>&>(data);
  649. gatherData(dataVoid);
  650. }
  651. inline void cleanup() { TLSDataContainer::cleanup(); }
  652. private:
  653. virtual void* createDataInstance() const CV_OVERRIDE {return new T;} // Wrapper to allocate data by template
  654. virtual void deleteDataInstance(void* pData) const CV_OVERRIDE {delete (T*)pData;} // Wrapper to release data by template
  655. // Disable TLS copy operations
  656. TLSData(TLSData &) {}
  657. TLSData& operator =(const TLSData &) {return *this;}
  658. };
  659. /** @brief Designed for command line parsing
  660. The sample below demonstrates how to use CommandLineParser:
  661. @code
  662. CommandLineParser parser(argc, argv, keys);
  663. parser.about("Application name v1.0.0");
  664. if (parser.has("help"))
  665. {
  666. parser.printMessage();
  667. return 0;
  668. }
  669. int N = parser.get<int>("N");
  670. double fps = parser.get<double>("fps");
  671. String path = parser.get<String>("path");
  672. use_time_stamp = parser.has("timestamp");
  673. String img1 = parser.get<String>(0);
  674. String img2 = parser.get<String>(1);
  675. int repeat = parser.get<int>(2);
  676. if (!parser.check())
  677. {
  678. parser.printErrors();
  679. return 0;
  680. }
  681. @endcode
  682. ### Keys syntax
  683. The keys parameter is a string containing several blocks, each one is enclosed in curly braces and
  684. describes one argument. Each argument contains three parts separated by the `|` symbol:
  685. -# argument names is a space-separated list of option synonyms (to mark argument as positional, prefix it with the `@` symbol)
  686. -# default value will be used if the argument was not provided (can be empty)
  687. -# help message (can be empty)
  688. For example:
  689. @code{.cpp}
  690. const String keys =
  691. "{help h usage ? | | print this message }"
  692. "{@image1 | | image1 for compare }"
  693. "{@image2 |<none>| image2 for compare }"
  694. "{@repeat |1 | number }"
  695. "{path |. | path to file }"
  696. "{fps | -1.0 | fps for output video }"
  697. "{N count |100 | count of objects }"
  698. "{ts timestamp | | use time stamp }"
  699. ;
  700. }
  701. @endcode
  702. Note that there are no default values for `help` and `timestamp` so we can check their presence using the `has()` method.
  703. Arguments with default values are considered to be always present. Use the `get()` method in these cases to check their
  704. actual value instead.
  705. String keys like `get<String>("@image1")` return the empty string `""` by default - even with an empty default value.
  706. Use the special `<none>` default value to enforce that the returned string must not be empty. (like in `get<String>("@image2")`)
  707. ### Usage
  708. For the described keys:
  709. @code{.sh}
  710. # Good call (3 positional parameters: image1, image2 and repeat; N is 200, ts is true)
  711. $ ./app -N=200 1.png 2.jpg 19 -ts
  712. # Bad call
  713. $ ./app -fps=aaa
  714. ERRORS:
  715. Parameter 'fps': can not convert: [aaa] to [double]
  716. @endcode
  717. */
  718. class CV_EXPORTS CommandLineParser
  719. {
  720. public:
  721. /** @brief Constructor
  722. Initializes command line parser object
  723. @param argc number of command line arguments (from main())
  724. @param argv array of command line arguments (from main())
  725. @param keys string describing acceptable command line parameters (see class description for syntax)
  726. */
  727. CommandLineParser(int argc, const char* const argv[], const String& keys);
  728. /** @brief Copy constructor */
  729. CommandLineParser(const CommandLineParser& parser);
  730. /** @brief Assignment operator */
  731. CommandLineParser& operator = (const CommandLineParser& parser);
  732. /** @brief Destructor */
  733. ~CommandLineParser();
  734. /** @brief Returns application path
  735. This method returns the path to the executable from the command line (`argv[0]`).
  736. For example, if the application has been started with such a command:
  737. @code{.sh}
  738. $ ./bin/my-executable
  739. @endcode
  740. this method will return `./bin`.
  741. */
  742. String getPathToApplication() const;
  743. /** @brief Access arguments by name
  744. Returns argument converted to selected type. If the argument is not known or can not be
  745. converted to selected type, the error flag is set (can be checked with @ref check).
  746. For example, define:
  747. @code{.cpp}
  748. String keys = "{N count||}";
  749. @endcode
  750. Call:
  751. @code{.sh}
  752. $ ./my-app -N=20
  753. # or
  754. $ ./my-app --count=20
  755. @endcode
  756. Access:
  757. @code{.cpp}
  758. int N = parser.get<int>("N");
  759. @endcode
  760. @param name name of the argument
  761. @param space_delete remove spaces from the left and right of the string
  762. @tparam T the argument will be converted to this type if possible
  763. @note You can access positional arguments by their `@`-prefixed name:
  764. @code{.cpp}
  765. parser.get<String>("@image");
  766. @endcode
  767. */
  768. template <typename T>
  769. T get(const String& name, bool space_delete = true) const
  770. {
  771. T val = T();
  772. getByName(name, space_delete, ParamType<T>::type, (void*)&val);
  773. return val;
  774. }
  775. /** @brief Access positional arguments by index
  776. Returns argument converted to selected type. Indexes are counted from zero.
  777. For example, define:
  778. @code{.cpp}
  779. String keys = "{@arg1||}{@arg2||}"
  780. @endcode
  781. Call:
  782. @code{.sh}
  783. ./my-app abc qwe
  784. @endcode
  785. Access arguments:
  786. @code{.cpp}
  787. String val_1 = parser.get<String>(0); // returns "abc", arg1
  788. String val_2 = parser.get<String>(1); // returns "qwe", arg2
  789. @endcode
  790. @param index index of the argument
  791. @param space_delete remove spaces from the left and right of the string
  792. @tparam T the argument will be converted to this type if possible
  793. */
  794. template <typename T>
  795. T get(int index, bool space_delete = true) const
  796. {
  797. T val = T();
  798. getByIndex(index, space_delete, ParamType<T>::type, (void*)&val);
  799. return val;
  800. }
  801. /** @brief Check if field was provided in the command line
  802. @param name argument name to check
  803. */
  804. bool has(const String& name) const;
  805. /** @brief Check for parsing errors
  806. Returns false if error occurred while accessing the parameters (bad conversion, missing arguments,
  807. etc.). Call @ref printErrors to print error messages list.
  808. */
  809. bool check() const;
  810. /** @brief Set the about message
  811. The about message will be shown when @ref printMessage is called, right before arguments table.
  812. */
  813. void about(const String& message);
  814. /** @brief Print help message
  815. This method will print standard help message containing the about message and arguments description.
  816. @sa about
  817. */
  818. void printMessage() const;
  819. /** @brief Print list of errors occurred
  820. @sa check
  821. */
  822. void printErrors() const;
  823. protected:
  824. void getByName(const String& name, bool space_delete, int type, void* dst) const;
  825. void getByIndex(int index, bool space_delete, int type, void* dst) const;
  826. struct Impl;
  827. Impl* impl;
  828. };
  829. //! @} core_utils
  830. //! @cond IGNORED
  831. /////////////////////////////// AutoBuffer implementation ////////////////////////////////////////
  832. template<typename _Tp, size_t fixed_size> inline
  833. AutoBuffer<_Tp, fixed_size>::AutoBuffer()
  834. {
  835. ptr = buf;
  836. sz = fixed_size;
  837. }
  838. template<typename _Tp, size_t fixed_size> inline
  839. AutoBuffer<_Tp, fixed_size>::AutoBuffer(size_t _size)
  840. {
  841. ptr = buf;
  842. sz = fixed_size;
  843. allocate(_size);
  844. }
  845. template<typename _Tp, size_t fixed_size> inline
  846. AutoBuffer<_Tp, fixed_size>::AutoBuffer(const AutoBuffer<_Tp, fixed_size>& abuf )
  847. {
  848. ptr = buf;
  849. sz = fixed_size;
  850. allocate(abuf.size());
  851. for( size_t i = 0; i < sz; i++ )
  852. ptr[i] = abuf.ptr[i];
  853. }
  854. template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>&
  855. AutoBuffer<_Tp, fixed_size>::operator = (const AutoBuffer<_Tp, fixed_size>& abuf)
  856. {
  857. if( this != &abuf )
  858. {
  859. deallocate();
  860. allocate(abuf.size());
  861. for( size_t i = 0; i < sz; i++ )
  862. ptr[i] = abuf.ptr[i];
  863. }
  864. return *this;
  865. }
  866. template<typename _Tp, size_t fixed_size> inline
  867. AutoBuffer<_Tp, fixed_size>::~AutoBuffer()
  868. { deallocate(); }
  869. template<typename _Tp, size_t fixed_size> inline void
  870. AutoBuffer<_Tp, fixed_size>::allocate(size_t _size)
  871. {
  872. if(_size <= sz)
  873. {
  874. sz = _size;
  875. return;
  876. }
  877. deallocate();
  878. sz = _size;
  879. if(_size > fixed_size)
  880. {
  881. ptr = new _Tp[_size];
  882. }
  883. }
  884. template<typename _Tp, size_t fixed_size> inline void
  885. AutoBuffer<_Tp, fixed_size>::deallocate()
  886. {
  887. if( ptr != buf )
  888. {
  889. delete[] ptr;
  890. ptr = buf;
  891. sz = fixed_size;
  892. }
  893. }
  894. template<typename _Tp, size_t fixed_size> inline void
  895. AutoBuffer<_Tp, fixed_size>::resize(size_t _size)
  896. {
  897. if(_size <= sz)
  898. {
  899. sz = _size;
  900. return;
  901. }
  902. size_t i, prevsize = sz, minsize = MIN(prevsize, _size);
  903. _Tp* prevptr = ptr;
  904. ptr = _size > fixed_size ? new _Tp[_size] : buf;
  905. sz = _size;
  906. if( ptr != prevptr )
  907. for( i = 0; i < minsize; i++ )
  908. ptr[i] = prevptr[i];
  909. for( i = prevsize; i < _size; i++ )
  910. ptr[i] = _Tp();
  911. if( prevptr != buf )
  912. delete[] prevptr;
  913. }
  914. template<typename _Tp, size_t fixed_size> inline size_t
  915. AutoBuffer<_Tp, fixed_size>::size() const
  916. { return sz; }
  917. template<> inline std::string CommandLineParser::get<std::string>(int index, bool space_delete) const
  918. {
  919. return get<String>(index, space_delete);
  920. }
  921. template<> inline std::string CommandLineParser::get<std::string>(const String& name, bool space_delete) const
  922. {
  923. return get<String>(name, space_delete);
  924. }
  925. //! @endcond
  926. // Basic Node class for tree building
  927. template<class OBJECT>
  928. class CV_EXPORTS Node
  929. {
  930. public:
  931. Node()
  932. {
  933. m_pParent = 0;
  934. }
  935. Node(OBJECT& payload) : m_payload(payload)
  936. {
  937. m_pParent = 0;
  938. }
  939. ~Node()
  940. {
  941. removeChilds();
  942. if (m_pParent)
  943. {
  944. int idx = m_pParent->findChild(this);
  945. if (idx >= 0)
  946. m_pParent->m_childs.erase(m_pParent->m_childs.begin() + idx);
  947. }
  948. }
  949. Node<OBJECT>* findChild(OBJECT& payload) const
  950. {
  951. for(size_t i = 0; i < this->m_childs.size(); i++)
  952. {
  953. if(this->m_childs[i]->m_payload == payload)
  954. return this->m_childs[i];
  955. }
  956. return NULL;
  957. }
  958. int findChild(Node<OBJECT> *pNode) const
  959. {
  960. for (size_t i = 0; i < this->m_childs.size(); i++)
  961. {
  962. if(this->m_childs[i] == pNode)
  963. return (int)i;
  964. }
  965. return -1;
  966. }
  967. void addChild(Node<OBJECT> *pNode)
  968. {
  969. if(!pNode)
  970. return;
  971. CV_Assert(pNode->m_pParent == 0);
  972. pNode->m_pParent = this;
  973. this->m_childs.push_back(pNode);
  974. }
  975. void removeChilds()
  976. {
  977. for(size_t i = 0; i < m_childs.size(); i++)
  978. {
  979. m_childs[i]->m_pParent = 0; // avoid excessive parent vector trimming
  980. delete m_childs[i];
  981. }
  982. m_childs.clear();
  983. }
  984. int getDepth()
  985. {
  986. int count = 0;
  987. Node *pParent = m_pParent;
  988. while(pParent) count++, pParent = pParent->m_pParent;
  989. return count;
  990. }
  991. public:
  992. OBJECT m_payload;
  993. Node<OBJECT>* m_pParent;
  994. std::vector<Node<OBJECT>*> m_childs;
  995. };
  996. // Instrumentation external interface
  997. namespace instr
  998. {
  999. #if !defined OPENCV_ABI_CHECK
  1000. enum TYPE
  1001. {
  1002. TYPE_GENERAL = 0, // OpenCV API function, e.g. exported function
  1003. TYPE_MARKER, // Information marker
  1004. TYPE_WRAPPER, // Wrapper function for implementation
  1005. TYPE_FUN, // Simple function call
  1006. };
  1007. enum IMPL
  1008. {
  1009. IMPL_PLAIN = 0,
  1010. IMPL_IPP,
  1011. IMPL_OPENCL,
  1012. };
  1013. struct NodeDataTls
  1014. {
  1015. NodeDataTls()
  1016. {
  1017. m_ticksTotal = 0;
  1018. }
  1019. uint64 m_ticksTotal;
  1020. };
  1021. class CV_EXPORTS NodeData
  1022. {
  1023. public:
  1024. NodeData(const char* funName = 0, const char* fileName = NULL, int lineNum = 0, void* retAddress = NULL, bool alwaysExpand = false, cv::instr::TYPE instrType = TYPE_GENERAL, cv::instr::IMPL implType = IMPL_PLAIN);
  1025. NodeData(NodeData &ref);
  1026. ~NodeData();
  1027. NodeData& operator=(const NodeData&);
  1028. cv::String m_funName;
  1029. cv::instr::TYPE m_instrType;
  1030. cv::instr::IMPL m_implType;
  1031. const char* m_fileName;
  1032. int m_lineNum;
  1033. void* m_retAddress;
  1034. bool m_alwaysExpand;
  1035. bool m_funError;
  1036. volatile int m_counter;
  1037. volatile uint64 m_ticksTotal;
  1038. TLSData<NodeDataTls> m_tls;
  1039. int m_threads;
  1040. // No synchronization
  1041. double getTotalMs() const { return ((double)m_ticksTotal / cv::getTickFrequency()) * 1000; }
  1042. double getMeanMs() const { return (((double)m_ticksTotal/m_counter) / cv::getTickFrequency()) * 1000; }
  1043. };
  1044. bool operator==(const NodeData& lhs, const NodeData& rhs);
  1045. typedef Node<NodeData> InstrNode;
  1046. CV_EXPORTS InstrNode* getTrace();
  1047. #endif // !defined OPENCV_ABI_CHECK
  1048. CV_EXPORTS bool useInstrumentation();
  1049. CV_EXPORTS void setUseInstrumentation(bool flag);
  1050. CV_EXPORTS void resetTrace();
  1051. enum FLAGS
  1052. {
  1053. FLAGS_NONE = 0,
  1054. FLAGS_MAPPING = 0x01,
  1055. FLAGS_EXPAND_SAME_NAMES = 0x02,
  1056. };
  1057. CV_EXPORTS void setFlags(FLAGS modeFlags);
  1058. static inline void setFlags(int modeFlags) { setFlags((FLAGS)modeFlags); }
  1059. CV_EXPORTS FLAGS getFlags();
  1060. } // namespace instr
  1061. namespace samples {
  1062. //! @addtogroup core_utils_samples
  1063. // This section describes utility functions for OpenCV samples.
  1064. //
  1065. // @note Implementation of these utilities is not thread-safe.
  1066. //
  1067. //! @{
  1068. /** @brief Try to find requested data file
  1069. Search directories:
  1070. 1. Directories passed via `addSamplesDataSearchPath()`
  1071. 2. OPENCV_SAMPLES_DATA_PATH_HINT environment variable
  1072. 3. OPENCV_SAMPLES_DATA_PATH environment variable
  1073. If parameter value is not empty and nothing is found then stop searching.
  1074. 4. Detects build/install path based on:
  1075. a. current working directory (CWD)
  1076. b. and/or binary module location (opencv_core/opencv_world, doesn't work with static linkage)
  1077. 5. Scan `<source>/{,data,samples/data}` directories if build directory is detected or the current directory is in source tree.
  1078. 6. Scan `<install>/share/OpenCV` directory if install directory is detected.
  1079. @see cv::utils::findDataFile
  1080. @param relative_path Relative path to data file
  1081. @param required Specify "file not found" handling.
  1082. If true, function prints information message and raises cv::Exception.
  1083. If false, function returns empty result
  1084. @param silentMode Disables messages
  1085. @return Returns path (absolute or relative to the current directory) or empty string if file is not found
  1086. */
  1087. CV_EXPORTS_W cv::String findFile(const cv::String& relative_path, bool required = true, bool silentMode = false);
  1088. CV_EXPORTS_W cv::String findFileOrKeep(const cv::String& relative_path, bool silentMode = false);
  1089. inline cv::String findFileOrKeep(const cv::String& relative_path, bool silentMode)
  1090. {
  1091. cv::String res = findFile(relative_path, false, silentMode);
  1092. if (res.empty())
  1093. return relative_path;
  1094. return res;
  1095. }
  1096. /** @brief Override search data path by adding new search location
  1097. Use this only to override default behavior
  1098. Passed paths are used in LIFO order.
  1099. @param path Path to used samples data
  1100. */
  1101. CV_EXPORTS_W void addSamplesDataSearchPath(const cv::String& path);
  1102. /** @brief Append samples search data sub directory
  1103. General usage is to add OpenCV modules name (`<opencv_contrib>/modules/<name>/samples/data` -> `<name>/samples/data` + `modules/<name>/samples/data`).
  1104. Passed subdirectories are used in LIFO order.
  1105. @param subdir samples data sub directory
  1106. */
  1107. CV_EXPORTS_W void addSamplesDataSearchSubDirectory(const cv::String& subdir);
  1108. //! @}
  1109. } // namespace samples
  1110. namespace utils {
  1111. CV_EXPORTS int getThreadID();
  1112. } // namespace
  1113. } //namespace cv
  1114. #ifndef DISABLE_OPENCV_24_COMPATIBILITY
  1115. #include "opencv2/core/core_c.h"
  1116. #endif
  1117. #endif //OPENCV_CORE_UTILITY_H