45#ifndef OPENCV_CORE_UTILITY_H
46#define OPENCV_CORE_UTILITY_H
49# error utility.hpp header must be compiled as C++
53# warning Detected Apple 'check' macro definition, it can cause build conflicts. Please, include this header before any Apple headers.
56#include "opencv2/core.hpp"
97#ifdef OPENCV_ENABLE_MEMORY_SANITIZER
98template<
typename _Tp,
size_t fixed_size = 0>
class AutoBuffer
100template<typename _Tp, size_t fixed_size = 1024/
sizeof(_Tp)+8>
class AutoBuffer
104 typedef _Tp value_type;
128 inline _Tp*
data() {
return ptr; }
130 inline const _Tp*
data()
const {
return ptr; }
132#if !defined(OPENCV_DISABLE_DEPRECATED_COMPATIBILITY)
134 operator _Tp* () { return ptr; }
136 operator const _Tp* ()
const {
return ptr; }
139 inline _Tp& operator[] (
size_t i) { CV_DbgCheckLT(i, sz,
"out of range");
return ptr[i]; }
141 inline const _Tp& operator[] (
size_t i)
const { CV_DbgCheckLT(i, sz,
"out of range");
return ptr[i]; }
150 _Tp buf[(fixed_size > 0) ? fixed_size : 1];
162extern "C" typedef int (*ErrorCallback)(
int status,
const char* func_name,
163 const char* err_msg,
const char* file_name,
164 int line,
void* userdata );
177CV_EXPORTS ErrorCallback
redirectError( ErrorCallback errCallback,
void* userdata=0,
void** prevUserdata=0);
179CV_EXPORTS String tempfile(
const char* suffix = 0);
180CV_EXPORTS
void glob(String pattern, std::vector<String>& result,
bool recursive =
false);
316 sumTime += (time - startTime);
321 CV_WRAP int64 getTimeTicks()
const
327 CV_WRAP
double getTimeMicro()
const
329 return getTimeMilli()*1e3;
333 CV_WRAP
double getTimeMilli()
const
335 return getTimeSec()*1e3;
339 CV_WRAP
double getTimeSec()
const
345 CV_WRAP int64 getCounter()
const
351 CV_WRAP
double getFPS()
const
353 const double sec = getTimeSec();
354 if (sec < DBL_EPSILON)
356 return counter / sec;
360 CV_WRAP
double getAvgTimeSec()
const
364 return getTimeSec() / counter;
368 CV_WRAP
double getAvgTimeMilli()
const
370 return getAvgTimeSec() * 1e3;
398std::ostream& operator << (std::ostream& out,
const TickMeter& tm)
457template<
typename _Tp>
static inline _Tp*
alignPtr(_Tp* ptr,
int n=(
int)
sizeof(_Tp))
460 return (_Tp*)(((size_t)ptr + n-1) & -n);
473 return (sz + n-1) & -n;
482static inline int divUp(
int a,
unsigned int b)
485 return (a + b - 1) / b;
488static inline size_t divUp(
size_t a,
unsigned int b)
490 return (a + b - 1) / b;
499static inline int roundUp(
int a,
unsigned int b)
502 return a + b - 1 - (a + b -1) % b;
505static inline size_t roundUp(
size_t a,
unsigned int b)
507 return a + b - 1 - (a + b - 1) % b;
516template<
int N,
typename T>
static inline
519 CV_StaticAssert((N & (N - 1)) == 0,
"");
520 return (((
size_t)data) & (N - 1)) == 0;
523template<
int N>
static inline
526 return isAligned<N>((
size_t)p1);
529template<
int N>
static inline
532 return isAligned<N>(((
size_t)p1)|((
size_t)p2));
535template<
int N>
static inline
536bool isAligned(
const void* p1,
const void* p2,
const void* p3)
538 return isAligned<N>(((
size_t)p1)|((
size_t)p2)|((
size_t)p3));
541template<
int N>
static inline
542bool isAligned(
const void* p1,
const void* p2,
const void* p3,
const void* p4)
544 return isAligned<N>(((
size_t)p1)|((
size_t)p2)|((
size_t)p3)|((
size_t)p4));
568static inline size_t getElemSize(
int type) {
return (
size_t)CV_ELEM_SIZE(type); }
580 virtual void operator() (
const Range& range)
const = 0;
593 std::function<void(
const Range&)> m_functor;
602 virtual void operator() (
const cv::Range& range)
const CV_OVERRIDE
617template<
typename _Tp,
typename Functor>
inline
620 operation(*
reinterpret_cast<_Tp*
>(0),
reinterpret_cast<int*
>(0));
629 const int LINES =
static_cast<int>(this->
total() / this->size[this->
dims - 1]);
634 PixelOperationWrapper(
Mat_<_Tp>*
const frame,
const Functor& _operation)
635 : mat(frame), op(_operation) {}
636 virtual ~PixelOperationWrapper(){}
641 const int DIMS = mat->dims;
642 const int COLS = mat->size[DIMS - 1];
644 for (
int row = range.start;
row < range.end; ++
row) {
645 this->rowCall2(
row, COLS);
648 std::vector<int> idx(DIMS);
649 idx[DIMS - 2] = range.start - 1;
651 for (
int line_num = range.start; line_num < range.end; ++line_num) {
653 for (
int i = DIMS - 2; i >= 0; --i) {
654 if (idx[i] >= mat->size[i]) {
655 idx[i - 1] += idx[i] / mat->size[i];
656 idx[i] %= mat->size[i];
663 this->rowCall(&idx[0], COLS, DIMS);
671 inline void rowCall(
int*
const idx,
const int COLS,
const int DIMS)
const {
672 int &
col = idx[DIMS - 1];
674 _Tp* pixel = &(mat->template at<_Tp>(idx));
677 op(*pixel,
const_cast<const int*
>(idx));
683 inline void rowCall2(
const int row,
const int COLS)
const {
686 operator const int*()
const {
687 return reinterpret_cast<const int*
>(
this);
689 int& operator[](
const int i) {
697 _Tp* pixel = &(mat->template at<_Tp>(idx));
698 const _Tp*
const pixel_end = pixel + COLS;
699 while(pixel < pixel_end) {
700 op(*pixel++,
static_cast<const int*
>(idx));
704 PixelOperationWrapper&
operator=(
const PixelOperationWrapper &) {
717typedef std::recursive_mutex Mutex;
718typedef std::lock_guard<cv::Mutex> AutoLock;
865 template <
typename T>
866 T
get(
const String& name,
bool space_delete =
true)
const
897 template <
typename T>
898 T
get(
int index,
bool space_delete =
true)
const
909 bool has(
const String& name)
const;
939 void getByName(
const String& name,
bool space_delete, Param type,
void* dst)
const;
940 void getByIndex(
int index,
bool space_delete, Param type,
void* dst)
const;
952template<
typename _Tp,
size_t fixed_size>
inline
959template<
typename _Tp,
size_t fixed_size>
inline
967template<
typename _Tp,
size_t fixed_size>
inline
972 allocate(abuf.size());
973 for(
size_t i = 0; i < sz; i++ )
974 ptr[i] = abuf.ptr[i];
977template<
typename _Tp,
size_t fixed_size>
inline AutoBuffer<_Tp, fixed_size>&
983 allocate(abuf.size());
984 for(
size_t i = 0; i < sz; i++ )
985 ptr[i] = abuf.ptr[i];
990template<
typename _Tp,
size_t fixed_size>
inline
994template<
typename _Tp,
size_t fixed_size>
inline void
1004 if(_size > fixed_size)
1006 ptr =
new _Tp[_size];
1010template<
typename _Tp,
size_t fixed_size>
inline void
1021template<
typename _Tp,
size_t fixed_size>
inline void
1029 size_t i, prevsize = sz, minsize = MIN(prevsize, _size);
1032 ptr = _size > fixed_size ?
new _Tp[_size] : buf;
1035 if( ptr != prevptr )
1036 for( i = 0; i < minsize; i++ )
1037 ptr[i] = prevptr[i];
1038 for( i = prevsize; i < _size; i++ )
1041 if( prevptr != buf )
1045template<
typename _Tp,
size_t fixed_size>
inline size_t
1053template<
class OBJECT>
1061 Node(OBJECT& payload) : m_payload(payload)
1070 int idx = m_pParent->findChild(
this);
1072 m_pParent->m_childs.erase(m_pParent->m_childs.begin() + idx);
1078 for(
size_t i = 0; i < this->m_childs.size(); i++)
1080 if(this->m_childs[i]->m_payload == payload)
1081 return this->m_childs[i];
1088 for (
size_t i = 0; i < this->m_childs.size(); i++)
1090 if(this->m_childs[i] == pNode)
1102 pNode->m_pParent =
this;
1103 this->m_childs.push_back(pNode);
1108 for(
size_t i = 0; i < m_childs.size(); i++)
1110 m_childs[i]->m_pParent = 0;
1119 Node *pParent = m_pParent;
1120 while(pParent) count++, pParent = pParent->m_pParent;
1127 std::vector<Node<OBJECT>*> m_childs;
1163CV_EXPORTS_W cv::String
findFile(
const cv::String& relative_path,
bool required =
true,
bool silentMode =
false);
1165CV_EXPORTS_W cv::String findFileOrKeep(
const cv::String& relative_path,
bool silentMode =
false);
1167inline cv::String findFileOrKeep(
const cv::String& relative_path,
bool silentMode)
1169 cv::String res =
findFile(relative_path,
false, silentMode);
1171 return relative_path;
1198CV_EXPORTS
int getThreadID();
1204#ifdef CV_COLLECT_IMPL_DATA
1205#include "opencv2/core/utils/instrumentation.hpp"
1208#define CV_IMPL_ADD(impl)
Automatically Allocated Buffer Class
Definition: utility.hpp:102
_Tp * ptr
pointer to the real buffer, can point to buf if the buffer is small enough
Definition: utility.hpp:146
void allocate(size_t _size)
allocates the new buffer of size _size. if the _size is small enough, stack-allocated buffer is used
~AutoBuffer()
destructor. calls deallocate()
AutoBuffer(const AutoBuffer< _Tp, fixed_size > &buf)
the copy constructor
_Tp * data()
returns pointer to the real buffer, stack-allocated or heap-allocated
Definition: utility.hpp:128
size_t size() const
returns the current buffer size
AutoBuffer< _Tp, fixed_size > & operator=(const AutoBuffer< _Tp, fixed_size > &buf)
the assignment operator
size_t sz
size of the real buffer
Definition: utility.hpp:148
void resize(size_t _size)
resizes the buffer and preserves the content
AutoBuffer()
the default constructor
void deallocate()
deallocates the buffer if it was dynamically allocated
AutoBuffer(size_t _size)
constructor taking the real buffer size
const _Tp * data() const
returns read-only pointer to the real buffer, stack-allocated or heap-allocated
Definition: utility.hpp:130
Designed for command line parsing
Definition: utility.hpp:800
T get(const String &name, bool space_delete=true) const
Access arguments by name
Definition: utility.hpp:866
String getPathToApplication() const
Returns application path
void about(const String &message)
Set the about message
T get(int index, bool space_delete=true) const
Access positional arguments by index
Definition: utility.hpp:898
CommandLineParser(const CommandLineParser &parser)
Copy constructor
void printErrors() const
Print list of errors occurred
void printMessage() const
Print help message
bool has(const String &name) const
Check if field was provided in the command line
~CommandLineParser()
Destructor
CommandLineParser(int argc, const char *const argv[], const String &keys)
Constructor
bool check() const
Check for parsing errors
Template matrix class derived from Mat
Definition: mat.hpp:2199
Mat col(int x) const
Creates a matrix header for the specified matrix column.
int dims
the matrix dimensionality, >= 2
Definition: mat.hpp:2105
Mat row(int y) const
Creates a matrix header for the specified matrix row.
Mat & operator=(const Mat &m)
assignment operators
size_t total() const
Returns the total number of array elements.
bool empty() const
Returns true if the array has no elements.
Mat operator()(Range rowRange, Range colRange) const
Extracts a rectangular submatrix.
Definition: utility.hpp:1055
Base class for parallel data processors
Definition: utility.hpp:577
Definition: utility.hpp:591
Template class specifying a continuous subsequence (slice) of a sequence.
Definition: core/types.hpp:590
a Class to measure passing time.
Definition: utility.hpp:295
CV_WRAP double getTimeSec() const
returns passed time in seconds.
Definition: utility.hpp:339
CV_EXPORTS void parallel_for_(const Range &range, const ParallelLoopBody &body, double nstripes=-1.)
Parallel data processor
CV_EXPORTS_W void addSamplesDataSearchPath(const cv::String &path)
Override search data path by adding new search location
CV_EXPORTS_W void addSamplesDataSearchSubDirectory(const cv::String &subdir)
Append samples search data sub directory
CV_EXPORTS_W cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file
CV_EXPORTS_W String getHardwareFeatureName(int feature)
Returns feature name by ID
static int roundUp(int a, unsigned int b)
Round first value up to the nearest multiple of second value.
Definition: utility.hpp:499
CV_EXPORTS_W void setNumThreads(int nthreads)
OpenCV will try to set the number of threads for the next parallel region.
CV_EXPORTS_W std::string getCPUFeaturesLine()
Returns list of CPU features enabled during compilation.
static int divUp(int a, unsigned int b)
Integer division with result round up.
Definition: utility.hpp:482
CV_EXPORTS ErrorCallback redirectError(ErrorCallback errCallback, void *userdata=0, void **prevUserdata=0)
Sets the new error handler and the optional user data.
CV_EXPORTS_W int getVersionMinor()
Returns minor library version
CV_EXPORTS_W bool checkHardwareSupport(int feature)
Returns true if the specified feature is supported by the host hardware.
CV_EXPORTS_W double getTickFrequency()
Returns the number of ticks per second.
CV_EXPORTS_W int getThreadNum()
Returns the index of the currently executed thread within the current parallel region....
CV_EXPORTS_W const String & getBuildInformation()
Returns full configuration time cmake output.
static _Tp * alignPtr(_Tp *ptr, int n=(int) sizeof(_Tp))
Aligns a pointer to the specified number of bytes.
Definition: utility.hpp:457
void forEach_impl(const Functor &operation)
Definition: utility.hpp:618
CV_EXPORTS_W void setUseOptimized(bool onoff)
Enables or disables the optimized code.
CV_EXPORTS_W int getVersionRevision()
Returns revision field of the library version
CV_EXPORTS bool setBreakOnError(bool flag)
Sets/resets the break-on-error mode.
CV_EXPORTS_W String getVersionString()
Returns library version string
CV_EXPORTS_W int64 getTickCount()
Returns the number of ticks.
CV_EXPORTS_W bool useOptimized()
Returns the status of optimized code usage.
CV_EXPORTS_W int getNumThreads()
Returns the number of threads used by OpenCV for parallel regions.
CV_EXPORTS_W int getVersionMajor()
Returns major library version
CV_EXPORTS_W int getNumberOfCPUs()
Returns the number of logical CPUs available for the process.
static bool isAligned(const T &data)
Alignment check of passed values
Definition: utility.hpp:517
CV_EXPORTS_W int64 getCPUTickCount()
Returns the number of CPU ticks.
static size_t alignSize(size_t sz, int n)
Aligns a buffer size to the specified number of bytes.
Definition: utility.hpp:470
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails
Definition: base.hpp:342
#define CV_DbgAssert(expr)
Definition: base.hpp:375
CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75
Definition: core.hpp:3076