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
98 template<
typename _Tp,
size_t fixed_size = 0>
class AutoBuffer
100 template<typename _Tp, size_t fixed_size = 1024/
sizeof(_Tp)+8>
class AutoBuffer
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];
316 sumTime += (time - startTime);
329 return getTimeMilli()*1e3;
335 return getTimeSec()*1e3;
353 const double sec = getTimeSec();
354 if (sec < DBL_EPSILON)
356 return counter / sec;
364 return getTimeSec() / counter;
370 return getAvgTimeSec() * 1e3;
457 template<
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;
482 static inline int divUp(
int a,
unsigned int b)
485 return (a + b - 1) / b;
488 static inline size_t divUp(
size_t a,
unsigned int b)
490 return (a + b - 1) / b;
499 static inline int roundUp(
int a,
unsigned int b)
502 return a + b - 1 - (a + b -1) % b;
505 static inline size_t roundUp(
size_t a,
unsigned int b)
507 return a + b - 1 - (a + b - 1) % b;
516 template<
int N,
typename T>
static inline
519 CV_StaticAssert((N & (N - 1)) == 0,
"");
520 return (((
size_t)
data) & (N - 1)) == 0;
523 template<
int N>
static inline
526 return isAligned<N>((
size_t)p1);
529 template<
int N>
static inline
532 return isAligned<N>(((
size_t)p1)|((
size_t)p2));
535 template<
int N>
static inline
536 bool isAligned(
const void* p1,
const void* p2,
const void* p3)
538 return isAligned<N>(((
size_t)p1)|((
size_t)p2)|((
size_t)p3));
541 template<
int N>
static inline
542 bool 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));
617 template<
typename _Tp,
typename Functor>
inline
620 operation(*
reinterpret_cast<_Tp*
>(0),
reinterpret_cast<int*
>(0));
634 PixelOperationWrapper(
Mat_<_Tp>*
const frame,
const Functor& _operation)
636 virtual ~PixelOperationWrapper(){}
641 const int DIMS =
mat->dims;
642 const int COLS =
mat->size[DIMS - 1];
645 this->rowCall2(
row, COLS);
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]) {
663 this->rowCall(&
idx[0], COLS, DIMS);
671 inline void rowCall(
int*
const idx,
const int COLS,
const int DIMS)
const {
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 &) {
717 #ifndef OPENCV_DISABLE_THREAD_SUPPORT
725 "cv::Mutex is disabled by OPENCV_DISABLE_THREAD_SUPPORT=ON");
729 "cv::Mutex is disabled by OPENCV_DISABLE_THREAD_SUPPORT=ON");
885 template <
typename T>
917 template <
typename T>
972 template<
typename _Tp,
size_t fixed_size>
inline
979 template<
typename _Tp,
size_t fixed_size>
inline
987 template<
typename _Tp,
size_t fixed_size>
inline
992 allocate(abuf.size());
993 for(
size_t i = 0; i < sz; i++ )
994 ptr[i] = abuf.ptr[i];
997 template<
typename _Tp,
size_t fixed_size>
inline AutoBuffer<_Tp, fixed_size>&
1003 allocate(abuf.size());
1004 for(
size_t i = 0; i < sz; i++ )
1005 ptr[i] = abuf.ptr[i];
1010 template<
typename _Tp,
size_t fixed_size>
inline
1014 template<
typename _Tp,
size_t fixed_size>
inline void
1024 if(_size > fixed_size)
1026 ptr =
new _Tp[_size];
1030 template<
typename _Tp,
size_t fixed_size>
inline void
1041 template<
typename _Tp,
size_t fixed_size>
inline void
1049 size_t i, prevsize = sz, minsize =
MIN(prevsize, _size);
1052 ptr = _size > fixed_size ?
new _Tp[_size] : buf;
1055 if( ptr != prevptr )
1056 for( i = 0; i < minsize; i++ )
1057 ptr[i] = prevptr[i];
1058 for( i = prevsize; i < _size; i++ )
1061 if( prevptr != buf )
1065 template<
typename _Tp,
size_t fixed_size>
inline size_t
1073 template<
class OBJECT>
1081 Node(OBJECT& payload) : m_payload(payload)
1090 int idx = m_pParent->findChild(
this);
1092 m_pParent->m_childs.erase(m_pParent->m_childs.begin() +
idx);
1098 for(
size_t i = 0; i < this->m_childs.size(); i++)
1100 if(this->m_childs[i]->m_payload == payload)
1101 return this->m_childs[i];
1108 for (
size_t i = 0; i < this->m_childs.size(); i++)
1110 if(this->m_childs[i] == pNode)
1123 this->m_childs.push_back(pNode);
1128 for(
size_t i = 0; i < m_childs.size(); i++)
1130 m_childs[i]->m_pParent = 0;
1139 Node *pParent = m_pParent;
1191 return relative_path;
1224 #ifdef CV_COLLECT_IMPL_DATA
1225 #include "opencv2/core/utils/instrumentation.hpp"
1228 #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
const _Tp * data() const
returns read-only pointer to the real buffer, stack-allocated or heap-allocated
Definition: utility.hpp:130
~AutoBuffer()
destructor. calls deallocate()
AutoBuffer(const AutoBuffer< _Tp, fixed_size > &buf)
the copy constructor
size_t size() const
returns the current buffer size
_Tp * data()
returns pointer to the real buffer, stack-allocated or heap-allocated
Definition: utility.hpp:128
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
AutoBuffer< _Tp, fixed_size > & operator=(const AutoBuffer< _Tp, fixed_size > &buf)
the assignment operator
_Tp value_type
Definition: utility.hpp:104
Designed for command line parsing.
Definition: utility.hpp:820
T get(const String &name, bool space_delete=true) const
Access arguments by name.
Definition: utility.hpp:886
String getPathToApplication() const
Returns application path.
void about(const String &message)
Set the about message.
Impl * impl
Definition: utility.hpp:962
void getByIndex(int index, bool space_delete, Param type, void *dst) const
T get(int index, bool space_delete=true) const
Access positional arguments by index.
Definition: utility.hpp:918
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.
void getByName(const String &name, bool space_delete, Param type, void *dst) const
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:2230
MatSize size
Definition: mat.hpp:2160
Mat col(int x) const
Creates a matrix header for the specified matrix column.
int dims
the matrix dimensionality, >= 2
Definition: mat.hpp:2136
Mat row(int y) const
Creates a matrix header for the specified matrix row.
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.
Mat & operator=(const Mat &m)
assignment operators
Definition: utility.hpp:1075
Node< OBJECT > * findChild(OBJECT &payload) const
Definition: utility.hpp:1096
Node(OBJECT &payload)
Definition: utility.hpp:1081
int findChild(Node< OBJECT > *pNode) const
Definition: utility.hpp:1106
~Node()
Definition: utility.hpp:1085
int getDepth()
Definition: utility.hpp:1136
std::vector< Node< OBJECT > * > m_childs
Definition: utility.hpp:1147
Node< OBJECT > * m_pParent
Definition: utility.hpp:1146
Node()
Definition: utility.hpp:1077
void addChild(Node< OBJECT > *pNode)
Definition: utility.hpp:1116
OBJECT m_payload
Definition: utility.hpp:1145
void removeChilds()
Definition: utility.hpp:1126
Definition: utility.hpp:591
virtual void operator()(const cv::Range &range) const CV_OVERRIDE
Definition: utility.hpp:602
ParallelLoopBodyLambdaWrapper(std::function< void(const Range &)> functor)
Definition: utility.hpp:596
Base class for parallel data processors.
Definition: utility.hpp:577
virtual ~ParallelLoopBody()
Template class specifying a continuous subsequence (slice) of a sequence.
Definition: types.hpp:623
a Class to measure passing time.
Definition: utility.hpp:295
CV_WRAP void stop()
stops counting ticks.
Definition: utility.hpp:310
CV_WRAP void reset()
resets internal values.
Definition: utility.hpp:374
CV_WRAP double getFPS() const
returns average FPS (frames per second) value.
Definition: utility.hpp:351
CV_WRAP double getTimeSec() const
returns passed time in seconds.
Definition: utility.hpp:339
CV_WRAP TickMeter()
the default constructor
Definition: utility.hpp:298
CV_WRAP void start()
starts counting ticks.
Definition: utility.hpp:304
CV_WRAP double getTimeMilli() const
returns passed time in milliseconds.
Definition: utility.hpp:333
CV_WRAP double getAvgTimeMilli() const
returns average time in milliseconds
Definition: utility.hpp:368
CV_WRAP double getTimeMicro() const
returns passed time in microseconds.
Definition: utility.hpp:327
CV_WRAP int64 getCounter() const
returns internal counter value.
Definition: utility.hpp:345
CV_WRAP int64 getTimeTicks() const
returns counted ticks.
Definition: utility.hpp:321
CV_WRAP double getAvgTimeSec() const
returns average time in seconds
Definition: utility.hpp:360
InputArrayOfArrays InputArrayOfArrays InputOutputArray InputOutputArray InputOutputArray InputOutputArray Size InputOutputArray InputOutputArray T
Definition: calib3d.hpp:1867
Param
Definition: core.hpp:3299
void void * frame
Definition: core_c.h:1913
Cv_iplAllocateImageData Cv_iplDeallocate deallocate
Definition: core_c.h:1964
const int * idx
Definition: core_c.h:668
CvArr const CvMat * mat
Definition: core_c.h:1308
int index
Definition: core_c.h:634
int int type
Definition: core_c.h:221
int count
Definition: core_c.h:1413
CvMemStorage CvSeq CvCmpFunc void * userdata
Definition: core_c.h:1724
const char const char * err_msg
Definition: core_c.h:2623
void * data
Definition: core_c.h:427
const CvArr const CvArr CvArr * result
Definition: core_c.h:1423
const char * func_name
Definition: core_c.h:2622
const char const char const char * file_name
Definition: core_c.h:2623
int64_t int64
Definition: interface.h:61
@ LINES
Definition: opengl.hpp:480
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 cv::String findFileOrKeep(const cv::String &relative_path, bool silentMode=false)
Definition: utility.hpp:1187
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 _Tp * alignPtr(_Tp *ptr, int n=(int) sizeof(_Tp))
Aligns a pointer to the specified number of bytes.
Definition: utility.hpp:457
#define CV_EXPORTS
Definition: cvdef.h:435
static int roundUp(int a, unsigned int b)
Round first value up to the nearest multiple of second value.
Definition: utility.hpp:499
#define MIN(a, b)
Definition: cvdef.h:513
CV_EXPORTS_W void setNumThreads(int nthreads)
OpenCV will try to set the number of threads for subsequent parallel regions.
CV_EXPORTS_W std::string getCPUFeaturesLine()
Returns list of CPU features enabled during compilation.
#define CV_OVERRIDE
Definition: cvdef.h:792
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.
#define CV_Error(code, msg)
Call the error handler.
Definition: base.hpp:320
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.
#define CV_EXPORTS_W
Definition: cvdef.h:472
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 String tempfile(const char *suffix=0)
void forEach_impl(const Functor &operation)
Definition: utility.hpp:618
std::lock_guard< cv::Mutex > AutoLock
Definition: utility.hpp:719
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.
static size_t getElemSize(int type)
Definition: utility.hpp:568
CV_EXPORTS_W int getNumThreads()
Returns the number of threads used by OpenCV for parallel regions.
CV_EXPORTS void glob(String pattern, std::vector< String > &result, bool recursive=false)
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
#define CV_WRAP
Definition: cvdef.h:481
int(* ErrorCallback)(int status, const char *func_name, const char *err_msg, const char *file_name, int line, void *userdata)
Definition: utility.hpp:162
CV_EXPORTS_W const String & getBuildInformation()
Returns full configuration time cmake output.
CV_EXPORTS_W int64 getCPUTickCount()
Returns the number of CPU ticks.
std::recursive_mutex Mutex
Definition: utility.hpp:718
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
#define CV_ELEM_SIZE(type)
Definition: cvdef.h:510
std::ostream & operator<<(std::ostream &, const DualQuat< _Tp > &)
CvArr CvSize range
Definition: imgproc_c.h:781
CvArr CvArr IplConvKernel int operation
Definition: imgproc_c.h:330
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.
OutputArray dst
Definition: imgproc.hpp:3564
@ StsNotImplemented
the requested function/feature is not implemented
Definition: base.hpp:113
CV_EXPORTS int getThreadID()
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441
Definition: core.hpp:3182