123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239 |
- #ifndef OPENCV_CORE_UTILITY_H
- #define OPENCV_CORE_UTILITY_H
- #ifndef __cplusplus
- # error utility.hpp header must be compiled as C++
- #endif
- #if defined(check)
- # warning Detected Apple 'check' macro definition, it can cause build conflicts. Please, include this header before any Apple headers.
- #endif
- #include "opencv2/core.hpp"
- #include <ostream>
- #ifdef CV_CXX11
- #include <functional>
- #endif
- namespace cv
- {
- #ifdef CV_COLLECT_IMPL_DATA
- CV_EXPORTS void setImpl(int flags);
- CV_EXPORTS void addImpl(int flag, const char* func = 0);
- CV_EXPORTS int getImpl(std::vector<int> &impl, std::vector<String> &funName);
- CV_EXPORTS bool useCollection();
- CV_EXPORTS void setUseCollection(bool flag);
- #define CV_IMPL_PLAIN 0x01
- #define CV_IMPL_OCL 0x02
- #define CV_IMPL_IPP 0x04
- #define CV_IMPL_MT 0x10
- #define CV_IMPL_ADD(impl) \
- if(cv::useCollection()) \
- { \
- cv::addImpl(impl, CV_Func); \
- }
- #else
- #define CV_IMPL_ADD(impl)
- #endif
- template<typename _Tp, size_t fixed_size = 1024/sizeof(_Tp)+8> class AutoBuffer
- {
- public:
- typedef _Tp value_type;
-
- AutoBuffer();
-
- AutoBuffer(size_t _size);
-
- AutoBuffer(const AutoBuffer<_Tp, fixed_size>& buf);
-
- AutoBuffer<_Tp, fixed_size>& operator = (const AutoBuffer<_Tp, fixed_size>& buf);
-
- ~AutoBuffer();
-
- void allocate(size_t _size);
-
- void deallocate();
-
- void resize(size_t _size);
-
- size_t size() const;
-
- operator _Tp* ();
-
- operator const _Tp* () const;
- protected:
-
- _Tp* ptr;
-
- size_t sz;
-
- _Tp buf[(fixed_size > 0) ? fixed_size : 1];
- };
- CV_EXPORTS bool setBreakOnError(bool flag);
- extern "C" typedef int (*ErrorCallback)( int status, const char* func_name,
- const char* err_msg, const char* file_name,
- int line, void* userdata );
- CV_EXPORTS ErrorCallback redirectError( ErrorCallback errCallback, void* userdata=0, void** prevUserdata=0);
- CV_EXPORTS String format( const char* fmt, ... );
- CV_EXPORTS String tempfile( const char* suffix = 0);
- CV_EXPORTS void glob(String pattern, std::vector<String>& result, bool recursive = false);
- CV_EXPORTS_W void setNumThreads(int nthreads);
- CV_EXPORTS_W int getNumThreads();
- CV_EXPORTS_W int getThreadNum();
- CV_EXPORTS_W const String& getBuildInformation();
- CV_EXPORTS_W int64 getTickCount();
- CV_EXPORTS_W double getTickFrequency();
- class CV_EXPORTS_W TickMeter
- {
- public:
-
- CV_WRAP TickMeter()
- {
- reset();
- }
-
- CV_WRAP void start()
- {
- startTime = cv::getTickCount();
- }
-
- CV_WRAP void stop()
- {
- int64 time = cv::getTickCount();
- if (startTime == 0)
- return;
- ++counter;
- sumTime += (time - startTime);
- startTime = 0;
- }
-
- CV_WRAP int64 getTimeTicks() const
- {
- return sumTime;
- }
-
- CV_WRAP double getTimeMicro() const
- {
- return getTimeMilli()*1e3;
- }
-
- CV_WRAP double getTimeMilli() const
- {
- return getTimeSec()*1e3;
- }
-
- CV_WRAP double getTimeSec() const
- {
- return (double)getTimeTicks() / getTickFrequency();
- }
-
- CV_WRAP int64 getCounter() const
- {
- return counter;
- }
-
- CV_WRAP void reset()
- {
- startTime = 0;
- sumTime = 0;
- counter = 0;
- }
- private:
- int64 counter;
- int64 sumTime;
- int64 startTime;
- };
- static inline
- std::ostream& operator << (std::ostream& out, const TickMeter& tm)
- {
- return out << tm.getTimeSec() << "sec";
- }
- CV_EXPORTS_W int64 getCPUTickCount();
- CV_EXPORTS_W bool checkHardwareSupport(int feature);
- CV_EXPORTS_W int getNumberOfCPUs();
- template<typename _Tp> static inline _Tp* alignPtr(_Tp* ptr, int n=(int)sizeof(_Tp))
- {
- CV_DbgAssert((n & (n - 1)) == 0);
- return (_Tp*)(((size_t)ptr + n-1) & -n);
- }
- static inline size_t alignSize(size_t sz, int n)
- {
- CV_DbgAssert((n & (n - 1)) == 0);
- return (sz + n-1) & -n;
- }
- static inline int divUp(int a, unsigned int b)
- {
- CV_DbgAssert(a >= 0);
- return (a + b - 1) / b;
- }
- static inline size_t divUp(size_t a, unsigned int b)
- {
- return (a + b - 1) / b;
- }
- CV_EXPORTS_W void setUseOptimized(bool onoff);
- CV_EXPORTS_W bool useOptimized();
- static inline size_t getElemSize(int type) { return (size_t)CV_ELEM_SIZE(type); }
- class CV_EXPORTS ParallelLoopBody
- {
- public:
- virtual ~ParallelLoopBody();
- virtual void operator() (const Range& range) const = 0;
- };
- CV_EXPORTS void parallel_for_(const Range& range, const ParallelLoopBody& body, double nstripes=-1.);
- #ifdef CV_CXX11
- class ParallelLoopBodyLambdaWrapper : public ParallelLoopBody
- {
- private:
- std::function<void(const Range&)> m_functor;
- public:
- ParallelLoopBodyLambdaWrapper(std::function<void(const Range&)> functor) :
- m_functor(functor)
- { }
- virtual void operator() (const cv::Range& range) const
- {
- m_functor(range);
- }
- };
- inline void parallel_for_(const Range& range, std::function<void(const Range&)> functor, double nstripes=-1.)
- {
- parallel_for_(range, ParallelLoopBodyLambdaWrapper(functor), nstripes);
- }
- #endif
- template<typename _Tp, typename Functor> inline
- void Mat::forEach_impl(const Functor& operation) {
- if (false) {
- operation(*reinterpret_cast<_Tp*>(0), reinterpret_cast<int*>(0));
-
-
-
-
- }
- CV_Assert(this->total() / this->size[this->dims - 1] <= INT_MAX);
- const int LINES = static_cast<int>(this->total() / this->size[this->dims - 1]);
- class PixelOperationWrapper :public ParallelLoopBody
- {
- public:
- PixelOperationWrapper(Mat_<_Tp>* const frame, const Functor& _operation)
- : mat(frame), op(_operation) {}
- virtual ~PixelOperationWrapper(){}
-
-
- virtual void operator()(const Range &range) const {
- const int DIMS = mat->dims;
- const int COLS = mat->size[DIMS - 1];
- if (DIMS <= 2) {
- for (int row = range.start; row < range.end; ++row) {
- this->rowCall2(row, COLS);
- }
- } else {
- std::vector<int> idx(DIMS);
- idx[DIMS - 2] = range.start - 1;
- for (int line_num = range.start; line_num < range.end; ++line_num) {
- idx[DIMS - 2]++;
- for (int i = DIMS - 2; i >= 0; --i) {
- if (idx[i] >= mat->size[i]) {
- idx[i - 1] += idx[i] / mat->size[i];
- idx[i] %= mat->size[i];
- continue;
- }
- else {
- break;
- }
- }
- this->rowCall(&idx[0], COLS, DIMS);
- }
- }
- }
- private:
- Mat_<_Tp>* const mat;
- const Functor op;
-
- inline void rowCall(int* const idx, const int COLS, const int DIMS) const {
- int &col = idx[DIMS - 1];
- col = 0;
- _Tp* pixel = &(mat->template at<_Tp>(idx));
- while (col < COLS) {
- op(*pixel, const_cast<const int*>(idx));
- pixel++; col++;
- }
- col = 0;
- }
-
- inline void rowCall2(const int row, const int COLS) const {
- union Index{
- int body[2];
- operator const int*() const {
- return reinterpret_cast<const int*>(this);
- }
- int& operator[](const int i) {
- return body[i];
- }
- } idx = {{row, 0}};
-
-
-
- _Tp* pixel = &(mat->template at<_Tp>(idx));
- const _Tp* const pixel_end = pixel + COLS;
- while(pixel < pixel_end) {
- op(*pixel++, static_cast<const int*>(idx));
- idx[1]++;
- }
- }
- PixelOperationWrapper& operator=(const PixelOperationWrapper &) {
- CV_Assert(false);
-
- return *this;
- }
- };
- parallel_for_(cv::Range(0, LINES), PixelOperationWrapper(reinterpret_cast<Mat_<_Tp>*>(this), operation));
- }
- class CV_EXPORTS Mutex
- {
- public:
- Mutex();
- ~Mutex();
- Mutex(const Mutex& m);
- Mutex& operator = (const Mutex& m);
- void lock();
- bool trylock();
- void unlock();
- struct Impl;
- protected:
- Impl* impl;
- };
- class CV_EXPORTS AutoLock
- {
- public:
- AutoLock(Mutex& m) : mutex(&m) { mutex->lock(); }
- ~AutoLock() { mutex->unlock(); }
- protected:
- Mutex* mutex;
- private:
- AutoLock(const AutoLock&);
- AutoLock& operator = (const AutoLock&);
- };
- class CV_EXPORTS TLSDataContainer
- {
- protected:
- TLSDataContainer();
- virtual ~TLSDataContainer();
- void gatherData(std::vector<void*> &data) const;
- #if OPENCV_ABI_COMPATIBILITY > 300
- void* getData() const;
- void release();
- private:
- #else
- void release();
- public:
- void* getData() const;
- #endif
- virtual void* createDataInstance() const = 0;
- virtual void deleteDataInstance(void* pData) const = 0;
- int key_;
- public:
- void cleanup();
- };
- template <typename T>
- class TLSData : protected TLSDataContainer
- {
- public:
- inline TLSData() {}
- inline ~TLSData() { release(); }
- inline T* get() const { return (T*)getData(); }
- inline T& getRef() const { T* ptr = (T*)getData(); CV_Assert(ptr); return *ptr; }
-
- inline void gather(std::vector<T*> &data) const
- {
- std::vector<void*> &dataVoid = reinterpret_cast<std::vector<void*>&>(data);
- gatherData(dataVoid);
- }
- inline void cleanup() { TLSDataContainer::cleanup(); }
- private:
- virtual void* createDataInstance() const {return new T;}
- virtual void deleteDataInstance(void* pData) const {delete (T*)pData;}
-
- TLSData(TLSData &) {}
- TLSData& operator =(const TLSData &) {return *this;}
- };
- class CV_EXPORTS CommandLineParser
- {
- public:
-
- CommandLineParser(int argc, const char* const argv[], const String& keys);
-
- CommandLineParser(const CommandLineParser& parser);
-
- CommandLineParser& operator = (const CommandLineParser& parser);
-
- ~CommandLineParser();
-
- String getPathToApplication() const;
-
- template <typename T>
- T get(const String& name, bool space_delete = true) const
- {
- T val = T();
- getByName(name, space_delete, ParamType<T>::type, (void*)&val);
- return val;
- }
-
- template <typename T>
- T get(int index, bool space_delete = true) const
- {
- T val = T();
- getByIndex(index, space_delete, ParamType<T>::type, (void*)&val);
- return val;
- }
-
- bool has(const String& name) const;
-
- bool check() const;
-
- void about(const String& message);
-
- void printMessage() const;
-
- void printErrors() const;
- protected:
- void getByName(const String& name, bool space_delete, int type, void* dst) const;
- void getByIndex(int index, bool space_delete, int type, void* dst) const;
- struct Impl;
- Impl* impl;
- };
- template<typename _Tp, size_t fixed_size> inline
- AutoBuffer<_Tp, fixed_size>::AutoBuffer()
- {
- ptr = buf;
- sz = fixed_size;
- }
- template<typename _Tp, size_t fixed_size> inline
- AutoBuffer<_Tp, fixed_size>::AutoBuffer(size_t _size)
- {
- ptr = buf;
- sz = fixed_size;
- allocate(_size);
- }
- template<typename _Tp, size_t fixed_size> inline
- AutoBuffer<_Tp, fixed_size>::AutoBuffer(const AutoBuffer<_Tp, fixed_size>& abuf )
- {
- ptr = buf;
- sz = fixed_size;
- allocate(abuf.size());
- for( size_t i = 0; i < sz; i++ )
- ptr[i] = abuf.ptr[i];
- }
- template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>&
- AutoBuffer<_Tp, fixed_size>::operator = (const AutoBuffer<_Tp, fixed_size>& abuf)
- {
- if( this != &abuf )
- {
- deallocate();
- allocate(abuf.size());
- for( size_t i = 0; i < sz; i++ )
- ptr[i] = abuf.ptr[i];
- }
- return *this;
- }
- template<typename _Tp, size_t fixed_size> inline
- AutoBuffer<_Tp, fixed_size>::~AutoBuffer()
- { deallocate(); }
- template<typename _Tp, size_t fixed_size> inline void
- AutoBuffer<_Tp, fixed_size>::allocate(size_t _size)
- {
- if(_size <= sz)
- {
- sz = _size;
- return;
- }
- deallocate();
- sz = _size;
- if(_size > fixed_size)
- {
- ptr = new _Tp[_size];
- }
- }
- template<typename _Tp, size_t fixed_size> inline void
- AutoBuffer<_Tp, fixed_size>::deallocate()
- {
- if( ptr != buf )
- {
- delete[] ptr;
- ptr = buf;
- sz = fixed_size;
- }
- }
- template<typename _Tp, size_t fixed_size> inline void
- AutoBuffer<_Tp, fixed_size>::resize(size_t _size)
- {
- if(_size <= sz)
- {
- sz = _size;
- return;
- }
- size_t i, prevsize = sz, minsize = MIN(prevsize, _size);
- _Tp* prevptr = ptr;
- ptr = _size > fixed_size ? new _Tp[_size] : buf;
- sz = _size;
- if( ptr != prevptr )
- for( i = 0; i < minsize; i++ )
- ptr[i] = prevptr[i];
- for( i = prevsize; i < _size; i++ )
- ptr[i] = _Tp();
- if( prevptr != buf )
- delete[] prevptr;
- }
- template<typename _Tp, size_t fixed_size> inline size_t
- AutoBuffer<_Tp, fixed_size>::size() const
- { return sz; }
- template<typename _Tp, size_t fixed_size> inline
- AutoBuffer<_Tp, fixed_size>::operator _Tp* ()
- { return ptr; }
- template<typename _Tp, size_t fixed_size> inline
- AutoBuffer<_Tp, fixed_size>::operator const _Tp* () const
- { return ptr; }
- template<> inline std::string CommandLineParser::get<std::string>(int index, bool space_delete) const
- {
- return get<String>(index, space_delete);
- }
- template<> inline std::string CommandLineParser::get<std::string>(const String& name, bool space_delete) const
- {
- return get<String>(name, space_delete);
- }
- template<class OBJECT>
- class CV_EXPORTS Node
- {
- public:
- Node()
- {
- m_pParent = 0;
- }
- Node(OBJECT& payload) : m_payload(payload)
- {
- m_pParent = 0;
- }
- ~Node()
- {
- removeChilds();
- if (m_pParent)
- {
- int idx = m_pParent->findChild(this);
- if (idx >= 0)
- m_pParent->m_childs.erase(m_pParent->m_childs.begin() + idx);
- }
- }
- Node<OBJECT>* findChild(OBJECT& payload) const
- {
- for(size_t i = 0; i < this->m_childs.size(); i++)
- {
- if(this->m_childs[i]->m_payload == payload)
- return this->m_childs[i];
- }
- return NULL;
- }
- int findChild(Node<OBJECT> *pNode) const
- {
- for (size_t i = 0; i < this->m_childs.size(); i++)
- {
- if(this->m_childs[i] == pNode)
- return (int)i;
- }
- return -1;
- }
- void addChild(Node<OBJECT> *pNode)
- {
- if(!pNode)
- return;
- CV_Assert(pNode->m_pParent == 0);
- pNode->m_pParent = this;
- this->m_childs.push_back(pNode);
- }
- void removeChilds()
- {
- for(size_t i = 0; i < m_childs.size(); i++)
- {
- m_childs[i]->m_pParent = 0;
- delete m_childs[i];
- }
- m_childs.clear();
- }
- int getDepth()
- {
- int count = 0;
- Node *pParent = m_pParent;
- while(pParent) count++, pParent = pParent->m_pParent;
- return count;
- }
- public:
- OBJECT m_payload;
- Node<OBJECT>* m_pParent;
- std::vector<Node<OBJECT>*> m_childs;
- };
- namespace instr
- {
- #if !defined OPENCV_ABI_CHECK
- enum TYPE
- {
- TYPE_GENERAL = 0,
- TYPE_MARKER,
- TYPE_WRAPPER,
- TYPE_FUN,
- };
- enum IMPL
- {
- IMPL_PLAIN = 0,
- IMPL_IPP,
- IMPL_OPENCL,
- };
- struct NodeDataTls
- {
- NodeDataTls()
- {
- m_ticksTotal = 0;
- }
- uint64 m_ticksTotal;
- };
- class CV_EXPORTS NodeData
- {
- public:
- 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);
- NodeData(NodeData &ref);
- ~NodeData();
- NodeData& operator=(const NodeData&);
- cv::String m_funName;
- cv::instr::TYPE m_instrType;
- cv::instr::IMPL m_implType;
- const char* m_fileName;
- int m_lineNum;
- void* m_retAddress;
- bool m_alwaysExpand;
- bool m_funError;
- volatile int m_counter;
- volatile uint64 m_ticksTotal;
- TLSData<NodeDataTls> m_tls;
- int m_threads;
-
- double getTotalMs() const { return ((double)m_ticksTotal / cv::getTickFrequency()) * 1000; }
- double getMeanMs() const { return (((double)m_ticksTotal/m_counter) / cv::getTickFrequency()) * 1000; }
- };
- bool operator==(const NodeData& lhs, const NodeData& rhs);
- typedef Node<NodeData> InstrNode;
- CV_EXPORTS InstrNode* getTrace();
- #endif
- CV_EXPORTS bool useInstrumentation();
- CV_EXPORTS void setUseInstrumentation(bool flag);
- CV_EXPORTS void resetTrace();
- enum FLAGS
- {
- FLAGS_NONE = 0,
- FLAGS_MAPPING = 0x01,
- FLAGS_EXPAND_SAME_NAMES = 0x02,
- };
- CV_EXPORTS void setFlags(FLAGS modeFlags);
- static inline void setFlags(int modeFlags) { setFlags((FLAGS)modeFlags); }
- CV_EXPORTS FLAGS getFlags();
- }
- namespace utils {
- CV_EXPORTS int getThreadID();
- }
- }
- #ifndef DISABLE_OPENCV_24_COMPATIBILITY
- #include "opencv2/core/core_c.h"
- #endif
- #endif
|