123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884 |
- #if defined(_MSC_VER) && (_MSC_VER >= 1800)
- // eliminating duplicated round() declaration
- #define HAVE_ROUND 1
- #pragma warning(push)
- #pragma warning(disable:5033) // 'register' is no longer a supported storage class
- #endif
- #include <Python.h>
- #if defined(_MSC_VER) && (_MSC_VER >= 1800)
- #pragma warning(pop)
- #endif
- #ifndef MODULE_STR
- #define MODULE_STR cv2
- #endif
- #define MODULESTR QUOTEME(MODULE_STR)
- #define QUOTEME(xxstuff) QUOTEME_I(xxstuff)
- #define QUOTEME_I(xxstuff) #xxstuff
- #define INCLUDE_FILE(xxff) INCLUDE_FILE_J(MODULE_PREFIX ## xxff)
- #define INCLUDE_FILE_I(xxff) INCLUDE_FILE_J(xxff)
- #define INCLUDE_FILE_J(xxff) QUOTEME(build/xxff.h)
- #if PY_MAJOR_VERSION >= 3
- #define MKTYPE2(NAME) MKTYPE2_I(NAME)
- #define MKTYPE2_I(NAME) pybv##_##NAME##_specials(); if (!to_ok(&pybv##_##NAME##_Type)) return NULL;
- #else
- #define MKTYPE2(NAME) MKTYPE2_I(NAME)
- #define MKTYPE2_I(NAME) pybv##_##NAME##_specials(); if (!to_ok(&pybv##_##NAME##_Type)) return
- #endif
- #ifndef MODULE_PREFIX
- #define MODULE_PREFIX pyopencv
- #endif
- #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
- #include <numpy/ndarrayobject.h>
- #if PY_MAJOR_VERSION >= 3
- # define CV_PYTHON_TYPE_HEAD_INIT() PyVarObject_HEAD_INIT(&PyType_Type, 0)
- #else
- # define CV_PYTHON_TYPE_HEAD_INIT() PyObject_HEAD_INIT(&PyType_Type) 0,
- #endif
- #include QUOTEME(build/pybv_generated_include.h)
- #include "opencv2/core/types_c.h"
- #include <opencv2/core/ocl.hpp>
- #include "opencv2/opencv_modules.hpp"
- #include "pycompat.hpp"
- #define PYOPENCV_TO pybv_to
- #define PYOPENCV_FROM pybv_from
- static PyObject* opencv_error = 0;
- static int failmsg(const char *fmt, ...)
- {
- char str[1000];
- va_list ap;
- va_start(ap, fmt);
- vsnprintf(str, sizeof(str), fmt, ap);
- va_end(ap);
- PyErr_SetString(PyExc_TypeError, str);
- return 0;
- }
- struct ArgInfo
- {
- const char * name;
- bool outputarg;
- // more fields may be added if necessary
- ArgInfo(const char * name_, bool outputarg_)
- : name(name_)
- , outputarg(outputarg_) {}
- // to match with older PYOPENCV_TO function signature
- operator const char *() const { return name; }
- };
- class PyAllowThreads
- {
- public:
- PyAllowThreads() : _state(PyEval_SaveThread()) {}
- ~PyAllowThreads()
- {
- PyEval_RestoreThread(_state);
- }
- private:
- PyThreadState* _state;
- };
- class PyEnsureGIL
- {
- public:
- PyEnsureGIL() : _state(PyGILState_Ensure()) {}
- ~PyEnsureGIL()
- {
- PyGILState_Release(_state);
- }
- private:
- PyGILState_STATE _state;
- };
- #define ERRWRAP2(expr) \
- try \
- { \
- PyAllowThreads allowThreads; \
- expr; \
- } \
- catch (const cv::Exception &e) \
- { \
- PyErr_SetString(opencv_error, e.what()); \
- return 0; \
- }
- using namespace cv;
- typedef std::vector<uchar> vector_uchar;
- typedef std::vector<char> vector_char;
- typedef std::vector<int> vector_int;
- typedef std::vector<float> vector_float;
- typedef std::vector<double> vector_double;
- typedef std::vector<size_t> vector_size_t;
- typedef std::vector<Point> vector_Point;
- typedef std::vector<Point2f> vector_Point2f;
- typedef std::vector<Point3f> vector_Point3f;
- typedef std::vector<Vec2f> vector_Vec2f;
- typedef std::vector<Vec3f> vector_Vec3f;
- typedef std::vector<Vec4f> vector_Vec4f;
- typedef std::vector<Vec6f> vector_Vec6f;
- typedef std::vector<Vec4i> vector_Vec4i;
- typedef std::vector<Rect> vector_Rect;
- typedef std::vector<Rect2d> vector_Rect2d;
- typedef std::vector<KeyPoint> vector_KeyPoint;
- typedef std::vector<Mat> vector_Mat;
- typedef std::vector<std::vector<Mat> > vector_vector_Mat;
- typedef std::vector<UMat> vector_UMat;
- typedef std::vector<DMatch> vector_DMatch;
- typedef std::vector<String> vector_String;
- typedef std::vector<Scalar> vector_Scalar;
- typedef std::vector<std::vector<char> > vector_vector_char;
- typedef std::vector<std::vector<Point> > vector_vector_Point;
- typedef std::vector<std::vector<Point2f> > vector_vector_Point2f;
- typedef std::vector<std::vector<Point3f> > vector_vector_Point3f;
- typedef std::vector<std::vector<DMatch> > vector_vector_DMatch;
- typedef std::vector<std::vector<KeyPoint> > vector_vector_KeyPoint;
- static PyObject* failmsgp(const char *fmt, ...)
- {
- char str[1000];
- va_list ap;
- va_start(ap, fmt);
- vsnprintf(str, sizeof(str), fmt, ap);
- va_end(ap);
- PyErr_SetString(PyExc_TypeError, str);
- return 0;
- }
- class NumpyAllocator : public MatAllocator
- {
- public:
- NumpyAllocator() { stdAllocator = Mat::getStdAllocator(); }
- ~NumpyAllocator() {}
- UMatData* allocate(PyObject* o, int dims, const int* sizes, int type, size_t* step) const
- {
- UMatData* u = new UMatData(this);
- u->data = u->origdata = (uchar*)PyArray_DATA((PyArrayObject*) o);
- npy_intp* _strides = PyArray_STRIDES((PyArrayObject*) o);
- for( int i = 0; i < dims - 1; i++ )
- step[i] = (size_t)_strides[i];
- step[dims-1] = CV_ELEM_SIZE(type);
- u->size = sizes[0]*step[0];
- u->userdata = o;
- return u;
- }
- UMatData* allocate(int dims0, const int* sizes, int type, void* data, size_t* step, int flags, UMatUsageFlags usageFlags) const
- {
- if( data != 0 )
- {
- // issue #6969: CV_Error(Error::StsAssert, "The data should normally be NULL!");
- // probably this is safe to do in such extreme case
- return stdAllocator->allocate(dims0, sizes, type, data, step, flags, usageFlags);
- }
- PyEnsureGIL gil;
- int depth = CV_MAT_DEPTH(type);
- int cn = CV_MAT_CN(type);
- const int f = (int)(sizeof(size_t)/8);
- int typenum = depth == CV_8U ? NPY_UBYTE : depth == CV_8S ? NPY_BYTE :
- depth == CV_16U ? NPY_USHORT : depth == CV_16S ? NPY_SHORT :
- depth == CV_32S ? NPY_INT : depth == CV_32F ? NPY_FLOAT :
- depth == CV_64F ? NPY_DOUBLE : f*NPY_ULONGLONG + (f^1)*NPY_UINT;
- int i, dims = dims0;
- cv::AutoBuffer<npy_intp> _sizes(dims + 1);
- for( i = 0; i < dims; i++ )
- _sizes[i] = sizes[i];
- if( cn > 1 )
- _sizes[dims++] = cn;
- PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum);
- if(!o)
- CV_Error_(Error::StsError, ("The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims));
- return allocate(o, dims0, sizes, type, step);
- }
- bool allocate(UMatData* u, int accessFlags, UMatUsageFlags usageFlags) const
- {
- return stdAllocator->allocate(u, accessFlags, usageFlags);
- }
- void deallocate(UMatData* u) const
- {
- if(!u)
- return;
- PyEnsureGIL gil;
- CV_Assert(u->urefcount >= 0);
- CV_Assert(u->refcount >= 0);
- if(u->refcount == 0)
- {
- PyObject* o = (PyObject*)u->userdata;
- Py_XDECREF(o);
- delete u;
- }
- }
- const MatAllocator* stdAllocator;
- };
- NumpyAllocator g_numpyAllocator;
- template<typename T> static
- bool PYOPENCV_TO(PyObject* obj, T& p, const char* name = "<unknown>");
- template<typename T> static
- PyObject* PYOPENCV_FROM(const T& src);
- enum { ARG_NONE = 0, ARG_MAT = 1, ARG_SCALAR = 2 };
- // special case, when the converter needs full ArgInfo structure
- static bool PYOPENCV_TO(PyObject* o, Mat& m, const ArgInfo info)
- {
- bool allowND = true;
- if(!o || o == Py_None)
- {
- if( !m.data )
- m.allocator = &g_numpyAllocator;
- return true;
- }
- if( PyInt_Check(o) )
- {
- double v[] = {static_cast<double>(PyInt_AsLong((PyObject*)o)), 0., 0., 0.};
- m = Mat(4, 1, CV_64F, v).clone();
- return true;
- }
- if( PyFloat_Check(o) )
- {
- double v[] = {PyFloat_AsDouble((PyObject*)o), 0., 0., 0.};
- m = Mat(4, 1, CV_64F, v).clone();
- return true;
- }
- if( PyTuple_Check(o) )
- {
- int i, sz = (int)PyTuple_Size((PyObject*)o);
- m = Mat(sz, 1, CV_64F);
- for( i = 0; i < sz; i++ )
- {
- PyObject* oi = PyTuple_GET_ITEM(o, i);
- if( PyInt_Check(oi) )
- m.at<double>(i) = (double)PyInt_AsLong(oi);
- else if( PyFloat_Check(oi) )
- m.at<double>(i) = (double)PyFloat_AsDouble(oi);
- else
- {
- failmsg("%s is not a numerical tuple", info.name);
- m.release();
- return false;
- }
- }
- return true;
- }
- if( !PyArray_Check(o) )
- {
- failmsg("%s is not a numpy array, neither a scalar", info.name);
- return false;
- }
- PyArrayObject* oarr = (PyArrayObject*) o;
- bool needcopy = false, needcast = false;
- int typenum = PyArray_TYPE(oarr), new_typenum = typenum;
- int type = typenum == NPY_UBYTE ? CV_8U :
- typenum == NPY_BYTE ? CV_8S :
- typenum == NPY_USHORT ? CV_16U :
- typenum == NPY_SHORT ? CV_16S :
- typenum == NPY_INT ? CV_32S :
- typenum == NPY_INT32 ? CV_32S :
- typenum == NPY_FLOAT ? CV_32F :
- typenum == NPY_DOUBLE ? CV_64F : -1;
- if( type < 0 )
- {
- if( typenum == NPY_INT64 || typenum == NPY_UINT64 || typenum == NPY_LONG )
- {
- needcopy = needcast = true;
- new_typenum = NPY_INT;
- type = CV_32S;
- }
- else
- {
- failmsg("%s data type = %d is not supported", info.name, typenum);
- return false;
- }
- }
- #ifndef CV_MAX_DIM
- const int CV_MAX_DIM = 32;
- #endif
- int ndims = PyArray_NDIM(oarr);
- if(ndims >= CV_MAX_DIM)
- {
- failmsg("%s dimensionality (=%d) is too high", info.name, ndims);
- return false;
- }
- int size[CV_MAX_DIM+1];
- size_t step[CV_MAX_DIM+1];
- size_t elemsize = CV_ELEM_SIZE1(type);
- const npy_intp* _sizes = PyArray_DIMS(oarr);
- const npy_intp* _strides = PyArray_STRIDES(oarr);
- bool ismultichannel = ndims == 3 && _sizes[2] <= CV_CN_MAX;
- for( int i = ndims-1; i >= 0 && !needcopy; i-- )
- {
- // these checks handle cases of
- // a) multi-dimensional (ndims > 2) arrays, as well as simpler 1- and 2-dimensional cases
- // b) transposed arrays, where _strides[] elements go in non-descending order
- // c) flipped arrays, where some of _strides[] elements are negative
- // the _sizes[i] > 1 is needed to avoid spurious copies when NPY_RELAXED_STRIDES is set
- if( (i == ndims-1 && _sizes[i] > 1 && (size_t)_strides[i] != elemsize) ||
- (i < ndims-1 && _sizes[i] > 1 && _strides[i] < _strides[i+1]) )
- needcopy = true;
- }
- if( ismultichannel && _strides[1] != (npy_intp)elemsize*_sizes[2] )
- needcopy = true;
- if (needcopy)
- {
- if (info.outputarg)
- {
- failmsg("Layout of the output array %s is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)", info.name);
- return false;
- }
- if( needcast ) {
- o = PyArray_Cast(oarr, new_typenum);
- oarr = (PyArrayObject*) o;
- }
- else {
- oarr = PyArray_GETCONTIGUOUS(oarr);
- o = (PyObject*) oarr;
- }
- _strides = PyArray_STRIDES(oarr);
- }
- // Normalize strides in case NPY_RELAXED_STRIDES is set
- size_t default_step = elemsize;
- for ( int i = ndims - 1; i >= 0; --i )
- {
- size[i] = (int)_sizes[i];
- if ( size[i] > 1 )
- {
- step[i] = (size_t)_strides[i];
- default_step = step[i] * size[i];
- }
- else
- {
- step[i] = default_step;
- default_step *= size[i];
- }
- }
- // handle degenerate case
- if( ndims == 0) {
- size[ndims] = 1;
- step[ndims] = elemsize;
- ndims++;
- }
- if( ismultichannel )
- {
- ndims--;
- type |= CV_MAKETYPE(0, size[2]);
- }
- if( ndims > 2 && !allowND )
- {
- failmsg("%s has more than 2 dimensions", info.name);
- return false;
- }
- m = Mat(ndims, size, type, PyArray_DATA(oarr), step);
- m.u = g_numpyAllocator.allocate(o, ndims, size, type, step);
- m.addref();
- if( !needcopy )
- {
- Py_INCREF(o);
- }
- m.allocator = &g_numpyAllocator;
- return true;
- }
- template<>
- bool PYOPENCV_TO(PyObject* o, Mat& m, const char* name)
- {
- return PYOPENCV_TO(o, m, ArgInfo(name, 0));
- }
- template<typename _Tp, int m, int n>
- bool PYOPENCV_TO(PyObject* o, Matx<_Tp, m, n>& mx, const ArgInfo info)
- {
- Mat tmp;
- if (!PYOPENCV_TO(o, tmp, info)) {
- return false;
- }
- tmp.copyTo(mx);
- return true;
- }
- template<typename _Tp, int m, int n>
- bool PYOPENCV_TO(PyObject* o, Matx<_Tp, m, n>& mx, const char* name)
- {
- return PYOPENCV_TO(o, mx, ArgInfo(name, 0));
- }
- template <typename T>
- bool PYOPENCV_TO(PyObject *o, Ptr<T>& p, const char *name)
- {
- if (!o || o == Py_None)
- return true;
- p = makePtr<T>();
- return PYOPENCV_TO(o, *p, name);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Mat& m)
- {
- if( !m.data )
- Py_RETURN_NONE;
- Mat temp, *p = (Mat*)&m;
- if(!p->u || p->allocator != &g_numpyAllocator)
- {
- temp.allocator = &g_numpyAllocator;
- ERRWRAP2(m.copyTo(temp));
- p = &temp;
- }
- PyObject* o = (PyObject*)p->u->userdata;
- Py_INCREF(o);
- return o;
- }
- template<typename _Tp, int m, int n>
- PyObject* PYOPENCV_FROM(const Matx<_Tp, m, n>& matx)
- {
- return PYOPENCV_FROM(Mat(matx));
- }
- template<typename T>
- PyObject* PYOPENCV_FROM(const cv::Ptr<T>& p)
- {
- if (!p)
- Py_RETURN_NONE;
- return PYOPENCV_FROM(*p);
- }
- typedef struct {
- PyObject_HEAD
- UMat* um;
- } cv2_UMatWrapperObject;
- static bool PyObject_IsUMat(PyObject *o);
- // UMatWrapper init - try to map arguments from python to UMat constructors
- static int UMatWrapper_init(cv2_UMatWrapperObject *self, PyObject *args, PyObject *kwds)
- {
- self->um = NULL;
- {
- // constructor ()
- const char *kwlist[] = {NULL};
- if (PyArg_ParseTupleAndKeywords(args, kwds, "", (char**) kwlist)) {
- self->um = new UMat();
- return 0;
- }
- PyErr_Clear();
- }
- {
- // constructor (rows, cols, type)
- const char *kwlist[] = {"rows", "cols", "type", NULL};
- int rows, cols, type;
- if (PyArg_ParseTupleAndKeywords(args, kwds, "iii", (char**) kwlist, &rows, &cols, &type)) {
- self->um = new UMat(rows, cols, type);
- return 0;
- }
- PyErr_Clear();
- }
- {
- // constructor (m, rowRange, colRange)
- const char *kwlist[] = {"m", "rowRange", "colRange", NULL};
- PyObject *obj = NULL;
- int y0 = -1, y1 = -1, x0 = -1, x1 = -1;
- if (PyArg_ParseTupleAndKeywords(args, kwds, "O(ii)|(ii)", (char**) kwlist, &obj, &y0, &y1, &x0, &x1) && PyObject_IsUMat(obj)) {
- UMat *um_other = ((cv2_UMatWrapperObject *) obj)->um;
- Range rowRange(y0, y1);
- Range colRange = (x0 >= 0 && x1 >= 0) ? Range(x0, x1) : Range::all();
- self->um = new UMat(*um_other, rowRange, colRange);
- return 0;
- }
- PyErr_Clear();
- }
- {
- // constructor (m)
- const char *kwlist[] = {"m", NULL};
- PyObject *obj = NULL;
- if (PyArg_ParseTupleAndKeywords(args, kwds, "O", (char**) kwlist, &obj)) {
- // constructor (UMat m)
- if (PyObject_IsUMat(obj)) {
- UMat *um_other = ((cv2_UMatWrapperObject *) obj)->um;
- self->um = new UMat(*um_other);
- return 0;
- }
- // python specific constructor from array like object
- Mat m;
- if (PYOPENCV_TO(obj, m, ArgInfo("UMatWrapper.np_mat", 0))) {
- self->um = new UMat();
- m.copyTo(*self->um);
- return 0;
- }
- }
- PyErr_Clear();
- }
- PyErr_SetString(PyExc_TypeError, "no matching UMat constructor found/supported");
- return -1;
- }
- static void UMatWrapper_dealloc(cv2_UMatWrapperObject* self)
- {
- if (self->um)
- delete self->um;
- #if PY_MAJOR_VERSION >= 3
- Py_TYPE(self)->tp_free((PyObject*)self);
- #else
- self->ob_type->tp_free((PyObject*)self);
- #endif
- }
- // UMatWrapper.get() - returns numpy array by transferring UMat data to Mat and than wrapping it to numpy array
- // (using numpy allocator - and so without unnecessary copy)
- static PyObject * UMatWrapper_get(cv2_UMatWrapperObject* self)
- {
- Mat m;
- m.allocator = &g_numpyAllocator;
- self->um->copyTo(m);
- return PYOPENCV_FROM(m);
- }
- // UMatWrapper.handle() - returns the OpenCL handle of the UMat object
- static PyObject * UMatWrapper_handle(cv2_UMatWrapperObject* self, PyObject *args, PyObject *kwds)
- {
- const char *kwlist[] = {"accessFlags", NULL};
- int accessFlags;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", (char**) kwlist, &accessFlags))
- return 0;
- return PyLong_FromVoidPtr(self->um->handle(accessFlags));
- }
- // UMatWrapper.isContinuous() - returns true if the matrix data is continuous
- static PyObject * UMatWrapper_isContinuous(cv2_UMatWrapperObject* self)
- {
- return PyBool_FromLong(self->um->isContinuous());
- }
- // UMatWrapper.isContinuous() - returns true if the matrix is a submatrix of another matrix
- static PyObject * UMatWrapper_isSubmatrix(cv2_UMatWrapperObject* self)
- {
- return PyBool_FromLong(self->um->isSubmatrix());
- }
- // UMatWrapper.context() - returns the OpenCL context used by OpenCV UMat
- static PyObject * UMatWrapper_context(cv2_UMatWrapperObject*)
- {
- return PyLong_FromVoidPtr(cv::ocl::Context::getDefault().ptr());
- }
- // UMatWrapper.context() - returns the OpenCL queue used by OpenCV UMat
- static PyObject * UMatWrapper_queue(cv2_UMatWrapperObject*)
- {
- return PyLong_FromVoidPtr(cv::ocl::Queue::getDefault().ptr());
- }
- static PyObject * UMatWrapper_offset_getter(cv2_UMatWrapperObject* self, void*)
- {
- return PyLong_FromSsize_t(self->um->offset);
- }
- static PyMethodDef UMatWrapper_methods[] = {
- {"get", (PyCFunction)UMatWrapper_get, METH_NOARGS,
- "Returns numpy array"
- },
- {"handle", (PyCFunction)UMatWrapper_handle, METH_VARARGS | METH_KEYWORDS,
- "Returns UMat native handle"
- },
- {"isContinuous", (PyCFunction)UMatWrapper_isContinuous, METH_NOARGS,
- "Returns true if the matrix data is continuous"
- },
- {"isSubmatrix", (PyCFunction)UMatWrapper_isSubmatrix, METH_NOARGS,
- "Returns true if the matrix is a submatrix of another matrix"
- },
- {"context", (PyCFunction)UMatWrapper_context, METH_NOARGS | METH_STATIC,
- "Returns OpenCL context handle"
- },
- {"queue", (PyCFunction)UMatWrapper_queue, METH_NOARGS | METH_STATIC,
- "Returns OpenCL queue handle"
- },
- {NULL, NULL, 0, NULL} /* Sentinel */
- };
- static PyGetSetDef UMatWrapper_getset[] = {
- {(char*) "offset", (getter) UMatWrapper_offset_getter, NULL, NULL, NULL},
- {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
- };
- static PyTypeObject cv2_UMatWrapperType = {
- #if PY_MAJOR_VERSION >= 3
- PyVarObject_HEAD_INIT(NULL, 0)
- #else
- PyObject_HEAD_INIT(NULL)
- 0, /*ob_size*/
- #endif
- "cv2.UMat", /* tp_name */
- sizeof(cv2_UMatWrapperObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)UMatWrapper_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- "OpenCV 3 UMat wrapper. Used for T-API support.", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- UMatWrapper_methods, /* tp_methods */
- 0, /* tp_members */
- UMatWrapper_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)UMatWrapper_init, /* tp_init */
- 0, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- 0, /* tp_free */
- 0, /* tp_is_gc */
- 0, /* tp_bases */
- 0, /* tp_mro */
- 0, /* tp_cache */
- 0, /* tp_subclasses */
- 0, /* tp_weaklist */
- 0, /* tp_del */
- 0, /* tp_version_tag */
- #if PY_MAJOR_VERSION >= 3
- 0, /* tp_finalize */
- #endif
- };
- static bool PyObject_IsUMat(PyObject *o) {
- return (o != NULL) && PyObject_TypeCheck(o, &cv2_UMatWrapperType);
- }
- static bool PYOPENCV_TO(PyObject* o, UMat& um, const ArgInfo info) {
- if (PyObject_IsUMat(o)) {
- um = *((cv2_UMatWrapperObject *) o)->um;
- return true;
- }
- Mat m;
- if (!PYOPENCV_TO(o, m, info)) {
- return false;
- }
- m.copyTo(um);
- return true;
- }
- template<>
- bool PYOPENCV_TO(PyObject* o, UMat& um, const char* name)
- {
- return PYOPENCV_TO(o, um, ArgInfo(name, 0));
- }
- template<>
- PyObject* PYOPENCV_FROM(const UMat& m) {
- PyObject *o = PyObject_CallObject((PyObject *) &cv2_UMatWrapperType, NULL);
- *((cv2_UMatWrapperObject *) o)->um = m;
- return o;
- }
- static bool PYOPENCV_TO(PyObject *o, Scalar& s, const ArgInfo info)
- {
- if(!o || o == Py_None)
- return true;
- if (PySequence_Check(o)) {
- PyObject *fi = PySequence_Fast(o, info.name);
- if (fi == NULL)
- return false;
- if (4 < PySequence_Fast_GET_SIZE(fi))
- {
- failmsg("Scalar value for argument '%s' is longer than 4", info.name);
- return false;
- }
- for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(fi); i++) {
- PyObject *item = PySequence_Fast_GET_ITEM(fi, i);
- if (PyFloat_Check(item) || PyInt_Check(item)) {
- s[(int)i] = PyFloat_AsDouble(item);
- } else {
- failmsg("Scalar value for argument '%s' is not numeric", info.name);
- return false;
- }
- }
- Py_DECREF(fi);
- } else {
- if (PyFloat_Check(o) || PyInt_Check(o)) {
- s[0] = PyFloat_AsDouble(o);
- } else {
- failmsg("Scalar value for argument '%s' is not numeric", info.name);
- return false;
- }
- }
- return true;
- }
- template<>
- bool PYOPENCV_TO(PyObject *o, Scalar& s, const char *name)
- {
- return PYOPENCV_TO(o, s, ArgInfo(name, 0));
- }
- template<>
- PyObject* PYOPENCV_FROM(const Scalar& src)
- {
- return Py_BuildValue("(dddd)", src[0], src[1], src[2], src[3]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const bool& value)
- {
- return PyBool_FromLong(value);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, bool& value, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- int _val = PyObject_IsTrue(obj);
- if(_val < 0)
- return false;
- value = _val > 0;
- return true;
- }
- template<>
- PyObject* PYOPENCV_FROM(const size_t& value)
- {
- return PyLong_FromSize_t(value);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, size_t& value, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- value = (int)PyLong_AsUnsignedLong(obj);
- return value != (size_t)-1 || !PyErr_Occurred();
- }
- template<>
- PyObject* PYOPENCV_FROM(const int& value)
- {
- return PyInt_FromLong(value);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, int& value, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- if(PyInt_Check(obj))
- value = (int)PyInt_AsLong(obj);
- else if(PyLong_Check(obj))
- value = (int)PyLong_AsLong(obj);
- else
- return false;
- return value != -1 || !PyErr_Occurred();
- }
- template<>
- PyObject* PYOPENCV_FROM(const uchar& value)
- {
- return PyInt_FromLong(value);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, uchar& value, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- int ivalue = (int)PyInt_AsLong(obj);
- value = cv::saturate_cast<uchar>(ivalue);
- return ivalue != -1 || !PyErr_Occurred();
- }
- template<>
- PyObject* PYOPENCV_FROM(const double& value)
- {
- return PyFloat_FromDouble(value);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, double& value, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- if(!!PyInt_CheckExact(obj))
- value = (double)PyInt_AS_LONG(obj);
- else
- value = PyFloat_AsDouble(obj);
- return !PyErr_Occurred();
- }
- template<>
- PyObject* PYOPENCV_FROM(const float& value)
- {
- return PyFloat_FromDouble(value);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, float& value, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- if(!!PyInt_CheckExact(obj))
- value = (float)PyInt_AS_LONG(obj);
- else
- value = (float)PyFloat_AsDouble(obj);
- return !PyErr_Occurred();
- }
- template<>
- PyObject* PYOPENCV_FROM(const int64& value)
- {
- return PyLong_FromLongLong(value);
- }
- template<>
- PyObject* PYOPENCV_FROM(const String& value)
- {
- return PyString_FromString(value.empty() ? "" : value.c_str());
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, String& value, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- char* str = PyString_AsString(obj);
- if(!str)
- return false;
- value = String(str);
- return true;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Size& sz, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- return PyArg_ParseTuple(obj, "ii", &sz.width, &sz.height) > 0;
- }
- template<>
- PyObject* PYOPENCV_FROM(const Size& sz)
- {
- return Py_BuildValue("(ii)", sz.width, sz.height);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Size_<float>& sz, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- return PyArg_ParseTuple(obj, "ff", &sz.width, &sz.height) > 0;
- }
- template<>
- PyObject* PYOPENCV_FROM(const Size_<float>& sz)
- {
- return Py_BuildValue("(ff)", sz.width, sz.height);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Rect& r, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- return PyArg_ParseTuple(obj, "iiii", &r.x, &r.y, &r.width, &r.height) > 0;
- }
- template<>
- PyObject* PYOPENCV_FROM(const Rect& r)
- {
- return Py_BuildValue("(iiii)", r.x, r.y, r.width, r.height);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Rect2d& r, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- return PyArg_ParseTuple(obj, "dddd", &r.x, &r.y, &r.width, &r.height) > 0;
- }
- template<>
- PyObject* PYOPENCV_FROM(const Rect2d& r)
- {
- return Py_BuildValue("(dddd)", r.x, r.y, r.width, r.height);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Range& r, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- if(PyObject_Size(obj) == 0)
- {
- r = Range::all();
- return true;
- }
- return PyArg_ParseTuple(obj, "ii", &r.start, &r.end) > 0;
- }
- template<>
- PyObject* PYOPENCV_FROM(const Range& r)
- {
- return Py_BuildValue("(ii)", r.start, r.end);
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Point& p, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- if(!!PyComplex_CheckExact(obj))
- {
- Py_complex c = PyComplex_AsCComplex(obj);
- p.x = saturate_cast<int>(c.real);
- p.y = saturate_cast<int>(c.imag);
- return true;
- }
- return PyArg_ParseTuple(obj, "ii", &p.x, &p.y) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Point2f& p, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- if(!!PyComplex_CheckExact(obj))
- {
- Py_complex c = PyComplex_AsCComplex(obj);
- p.x = saturate_cast<float>(c.real);
- p.y = saturate_cast<float>(c.imag);
- return true;
- }
- return PyArg_ParseTuple(obj, "ff", &p.x, &p.y) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Point2d& p, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- if(!!PyComplex_CheckExact(obj))
- {
- Py_complex c = PyComplex_AsCComplex(obj);
- p.x = saturate_cast<double>(c.real);
- p.y = saturate_cast<double>(c.imag);
- return true;
- }
- return PyArg_ParseTuple(obj, "dd", &p.x, &p.y) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Point3f& p, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- return PyArg_ParseTuple(obj, "fff", &p.x, &p.y, &p.z) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Point3d& p, const char* name)
- {
- (void)name;
- if(!obj || obj == Py_None)
- return true;
- return PyArg_ParseTuple(obj, "ddd", &p.x, &p.y, &p.z) > 0;
- }
- template<>
- PyObject* PYOPENCV_FROM(const Point& p)
- {
- return Py_BuildValue("(ii)", p.x, p.y);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Point2f& p)
- {
- return Py_BuildValue("(dd)", p.x, p.y);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Point3f& p)
- {
- return Py_BuildValue("(ddd)", p.x, p.y, p.z);
- }
- static bool PYOPENCV_TO(PyObject* obj, Vec4d& v, ArgInfo info)
- {
- (void)info;
- if (!obj)
- return true;
- return PyArg_ParseTuple(obj, "dddd", &v[0], &v[1], &v[2], &v[3]) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Vec4d& v, const char* name)
- {
- return PYOPENCV_TO(obj, v, ArgInfo(name, 0));
- }
- static bool PYOPENCV_TO(PyObject* obj, Vec4f& v, ArgInfo info)
- {
- (void)info;
- if (!obj)
- return true;
- return PyArg_ParseTuple(obj, "ffff", &v[0], &v[1], &v[2], &v[3]) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Vec4f& v, const char* name)
- {
- return PYOPENCV_TO(obj, v, ArgInfo(name, 0));
- }
- static bool PYOPENCV_TO(PyObject* obj, Vec4i& v, ArgInfo info)
- {
- (void)info;
- if (!obj)
- return true;
- return PyArg_ParseTuple(obj, "iiii", &v[0], &v[1], &v[2], &v[3]) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Vec4i& v, const char* name)
- {
- return PYOPENCV_TO(obj, v, ArgInfo(name, 0));
- }
- static bool PYOPENCV_TO(PyObject* obj, Vec3d& v, ArgInfo info)
- {
- (void)info;
- if (!obj)
- return true;
- return PyArg_ParseTuple(obj, "ddd", &v[0], &v[1], &v[2]) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Vec3d& v, const char* name)
- {
- return PYOPENCV_TO(obj, v, ArgInfo(name, 0));
- }
- static bool PYOPENCV_TO(PyObject* obj, Vec3f& v, ArgInfo info)
- {
- (void)info;
- if (!obj)
- return true;
- return PyArg_ParseTuple(obj, "fff", &v[0], &v[1], &v[2]) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Vec3f& v, const char* name)
- {
- return PYOPENCV_TO(obj, v, ArgInfo(name, 0));
- }
- static bool PYOPENCV_TO(PyObject* obj, Vec3i& v, ArgInfo info)
- {
- (void)info;
- if (!obj)
- return true;
- return PyArg_ParseTuple(obj, "iii", &v[0], &v[1], &v[2]) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Vec3i& v, const char* name)
- {
- return PYOPENCV_TO(obj, v, ArgInfo(name, 0));
- }
- static bool PYOPENCV_TO(PyObject* obj, Vec2d& v, ArgInfo info)
- {
- (void)info;
- if (!obj)
- return true;
- return PyArg_ParseTuple(obj, "dd", &v[0], &v[1]) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Vec2d& v, const char* name)
- {
- return PYOPENCV_TO(obj, v, ArgInfo(name, 0));
- }
- static bool PYOPENCV_TO(PyObject* obj, Vec2f& v, ArgInfo info)
- {
- (void)info;
- if (!obj)
- return true;
- return PyArg_ParseTuple(obj, "ff", &v[0], &v[1]) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Vec2f& v, const char* name)
- {
- return PYOPENCV_TO(obj, v, ArgInfo(name, 0));
- }
- static bool PYOPENCV_TO(PyObject* obj, Vec2i& v, ArgInfo info)
- {
- (void)info;
- if (!obj)
- return true;
- return PyArg_ParseTuple(obj, "ii", &v[0], &v[1]) > 0;
- }
- template<>
- bool PYOPENCV_TO(PyObject* obj, Vec2i& v, const char* name)
- {
- return PYOPENCV_TO(obj, v, ArgInfo(name, 0));
- }
- template<>
- PyObject* PYOPENCV_FROM(const Vec4d& v)
- {
- return Py_BuildValue("(dddd)", v[0], v[1], v[2], v[3]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Vec4f& v)
- {
- return Py_BuildValue("(ffff)", v[0], v[1], v[2], v[3]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Vec4i& v)
- {
- return Py_BuildValue("(iiii)", v[0], v[1], v[2], v[3]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Vec3d& v)
- {
- return Py_BuildValue("(ddd)", v[0], v[1], v[2]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Vec3f& v)
- {
- return Py_BuildValue("(fff)", v[0], v[1], v[2]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Vec3i& v)
- {
- return Py_BuildValue("(iii)", v[0], v[1], v[2]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Vec2d& v)
- {
- return Py_BuildValue("(dd)", v[0], v[1]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Vec2f& v)
- {
- return Py_BuildValue("(ff)", v[0], v[1]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Vec2i& v)
- {
- return Py_BuildValue("(ii)", v[0], v[1]);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Point2d& p)
- {
- return Py_BuildValue("(dd)", p.x, p.y);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Point3d& p)
- {
- return Py_BuildValue("(ddd)", p.x, p.y, p.z);
- }
- template<typename _Tp> struct pyopencvVecConverter
- {
- static bool to(PyObject* obj, std::vector<_Tp>& value, const ArgInfo info)
- {
- typedef typename DataType<_Tp>::channel_type _Cp;
- if(!obj || obj == Py_None)
- return true;
- if (PyArray_Check(obj))
- {
- Mat m;
- PYOPENCV_TO(obj, m, info);
- m.copyTo(value);
- }
- if (!PySequence_Check(obj))
- return false;
- PyObject *seq = PySequence_Fast(obj, info.name);
- if (seq == NULL)
- return false;
- int i, j, n = (int)PySequence_Fast_GET_SIZE(seq);
- value.resize(n);
- int type = traits::Type<_Tp>::value;
- int depth = CV_MAT_DEPTH(type), channels = CV_MAT_CN(type);
- PyObject** items = PySequence_Fast_ITEMS(seq);
- for( i = 0; i < n; i++ )
- {
- PyObject* item = items[i];
- PyObject* seq_i = 0;
- PyObject** items_i = &item;
- _Cp* data = (_Cp*)&value[i];
- if( channels == 2 && PyComplex_CheckExact(item) )
- {
- Py_complex c = PyComplex_AsCComplex(obj);
- data[0] = saturate_cast<_Cp>(c.real);
- data[1] = saturate_cast<_Cp>(c.imag);
- continue;
- }
- if( channels > 1 )
- {
- if( PyArray_Check(item))
- {
- Mat src;
- PYOPENCV_TO(item, src, info);
- if( src.dims != 2 || src.channels() != 1 ||
- ((src.cols != 1 || src.rows != channels) &&
- (src.cols != channels || src.rows != 1)))
- break;
- Mat dst(src.rows, src.cols, depth, data);
- src.convertTo(dst, type);
- if( dst.data != (uchar*)data )
- break;
- continue;
- }
- seq_i = PySequence_Fast(item, info.name);
- if( !seq_i || (int)PySequence_Fast_GET_SIZE(seq_i) != channels )
- {
- Py_XDECREF(seq_i);
- break;
- }
- items_i = PySequence_Fast_ITEMS(seq_i);
- }
- for( j = 0; j < channels; j++ )
- {
- PyObject* item_ij = items_i[j];
- if( PyInt_Check(item_ij))
- {
- int v = (int)PyInt_AsLong(item_ij);
- if( v == -1 && PyErr_Occurred() )
- break;
- data[j] = saturate_cast<_Cp>(v);
- }
- else if( PyLong_Check(item_ij))
- {
- int v = (int)PyLong_AsLong(item_ij);
- if( v == -1 && PyErr_Occurred() )
- break;
- data[j] = saturate_cast<_Cp>(v);
- }
- else if( PyFloat_Check(item_ij))
- {
- double v = PyFloat_AsDouble(item_ij);
- if( PyErr_Occurred() )
- break;
- data[j] = saturate_cast<_Cp>(v);
- }
- else
- break;
- }
- Py_XDECREF(seq_i);
- if( j < channels )
- break;
- }
- Py_DECREF(seq);
- return i == n;
- }
- static PyObject* from(const std::vector<_Tp>& value)
- {
- if(value.empty())
- return PyTuple_New(0);
- int type = traits::Type<_Tp>::value;
- int depth = CV_MAT_DEPTH(type), channels = CV_MAT_CN(type);
- Mat src((int)value.size(), channels, depth, (uchar*)&value[0]);
- return PYOPENCV_FROM(src);
- }
- };
- template<typename _Tp>
- bool PYOPENCV_TO(PyObject* obj, std::vector<_Tp>& value, const ArgInfo info)
- {
- return pyopencvVecConverter<_Tp>::to(obj, value, info);
- }
- template<typename _Tp>
- PyObject* PYOPENCV_FROM(const std::vector<_Tp>& value)
- {
- return pyopencvVecConverter<_Tp>::from(value);
- }
- template<typename _Tp> static inline bool PYOPENCV_TO_generic_vec(PyObject* obj, std::vector<_Tp>& value, const ArgInfo info)
- {
- if(!obj || obj == Py_None)
- return true;
- if (!PySequence_Check(obj))
- return false;
- PyObject *seq = PySequence_Fast(obj, info.name);
- if (seq == NULL)
- return false;
- int i, n = (int)PySequence_Fast_GET_SIZE(seq);
- value.resize(n);
- PyObject** items = PySequence_Fast_ITEMS(seq);
- for( i = 0; i < n; i++ )
- {
- PyObject* item = items[i];
- if(!PYOPENCV_TO(item, value[i], info))
- break;
- }
- Py_DECREF(seq);
- return i == n;
- }
- template<typename _Tp> static inline PyObject* PYOPENCV_FROM_generic_vec(const std::vector<_Tp>& value)
- {
- int i, n = (int)value.size();
- PyObject* seq = PyList_New(n);
- for( i = 0; i < n; i++ )
- {
- PyObject* item = PYOPENCV_FROM(value[i]);
- if(!item)
- break;
- PyList_SET_ITEM(seq, i, item);
- }
- if( i < n )
- {
- Py_DECREF(seq);
- return 0;
- }
- return seq;
- }
- template<>
- PyObject* PYOPENCV_FROM(const std::pair<int, double>& src)
- {
- return Py_BuildValue("(id)", src.first, src.second);
- }
- template<typename _Tp, typename _Tr> struct pyopencvVecConverter<std::pair<_Tp, _Tr> >
- {
- static bool to(PyObject* obj, std::vector<std::pair<_Tp, _Tr> >& value, const ArgInfo info)
- {
- return PYOPENCV_TO_generic_vec(obj, value, info);
- }
- static PyObject* from(const std::vector<std::pair<_Tp, _Tr> >& value)
- {
- return PYOPENCV_FROM_generic_vec(value);
- }
- };
- template<typename _Tp> struct pyopencvVecConverter<std::vector<_Tp> >
- {
- static bool to(PyObject* obj, std::vector<std::vector<_Tp> >& value, const ArgInfo info)
- {
- return PYOPENCV_TO_generic_vec(obj, value, info);
- }
- static PyObject* from(const std::vector<std::vector<_Tp> >& value)
- {
- return PYOPENCV_FROM_generic_vec(value);
- }
- };
- template<> struct pyopencvVecConverter<Mat>
- {
- static bool to(PyObject* obj, std::vector<Mat>& value, const ArgInfo info)
- {
- return PYOPENCV_TO_generic_vec(obj, value, info);
- }
- static PyObject* from(const std::vector<Mat>& value)
- {
- return PYOPENCV_FROM_generic_vec(value);
- }
- };
- template<> struct pyopencvVecConverter<KeyPoint>
- {
- static bool to(PyObject* obj, std::vector<KeyPoint>& value, const ArgInfo info)
- {
- return PYOPENCV_TO_generic_vec(obj, value, info);
- }
- static PyObject* from(const std::vector<KeyPoint>& value)
- {
- return PYOPENCV_FROM_generic_vec(value);
- }
- };
- template<> struct pyopencvVecConverter<DMatch>
- {
- static bool to(PyObject* obj, std::vector<DMatch>& value, const ArgInfo info)
- {
- return PYOPENCV_TO_generic_vec(obj, value, info);
- }
- static PyObject* from(const std::vector<DMatch>& value)
- {
- return PYOPENCV_FROM_generic_vec(value);
- }
- };
- template<> struct pyopencvVecConverter<String>
- {
- static bool to(PyObject* obj, std::vector<String>& value, const ArgInfo info)
- {
- return PYOPENCV_TO_generic_vec(obj, value, info);
- }
- static PyObject* from(const std::vector<String>& value)
- {
- return PYOPENCV_FROM_generic_vec(value);
- }
- };
- template<>
- bool PYOPENCV_TO(PyObject *obj, TermCriteria& dst, const char *name)
- {
- (void)name;
- if(!obj)
- return true;
- return PyArg_ParseTuple(obj, "iid", &dst.type, &dst.maxCount, &dst.epsilon) > 0;
- }
- template<>
- PyObject* PYOPENCV_FROM(const TermCriteria& src)
- {
- return Py_BuildValue("(iid)", src.type, src.maxCount, src.epsilon);
- }
- template<>
- bool PYOPENCV_TO(PyObject *obj, RotatedRect& dst, const char *name)
- {
- (void)name;
- if(!obj)
- return true;
- return PyArg_ParseTuple(obj, "(ff)(ff)f", &dst.center.x, &dst.center.y, &dst.size.width, &dst.size.height, &dst.angle) > 0;
- }
- template<>
- PyObject* PYOPENCV_FROM(const RotatedRect& src)
- {
- return Py_BuildValue("((ff)(ff)f)", src.center.x, src.center.y, src.size.width, src.size.height, src.angle);
- }
- template<>
- PyObject* PYOPENCV_FROM(const Moments& m)
- {
- return Py_BuildValue("{s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d}",
- "m00", m.m00, "m10", m.m10, "m01", m.m01,
- "m20", m.m20, "m11", m.m11, "m02", m.m02,
- "m30", m.m30, "m21", m.m21, "m12", m.m12, "m03", m.m03,
- "mu20", m.mu20, "mu11", m.mu11, "mu02", m.mu02,
- "mu30", m.mu30, "mu21", m.mu21, "mu12", m.mu12, "mu03", m.mu03,
- "nu20", m.nu20, "nu11", m.nu11, "nu02", m.nu02,
- "nu30", m.nu30, "nu21", m.nu21, "nu12", m.nu12, "nu03", m.nu03);
- }
- static void OnMouse(int event, int x, int y, int flags, void* param)
- {
- PyGILState_STATE gstate;
- gstate = PyGILState_Ensure();
- PyObject *o = (PyObject*)param;
- PyObject *args = Py_BuildValue("iiiiO", event, x, y, flags, PyTuple_GetItem(o, 1));
- PyObject *r = PyObject_Call(PyTuple_GetItem(o, 0), args, NULL);
- if (r == NULL)
- PyErr_Print();
- else
- Py_DECREF(r);
- Py_DECREF(args);
- PyGILState_Release(gstate);
- }
- #ifdef HAVE_OPENCV_HIGHGUI
- static PyObject *pycvSetMouseCallback(PyObject*, PyObject *args, PyObject *kw)
- {
- const char *keywords[] = { "window_name", "on_mouse", "param", NULL };
- char* name;
- PyObject *on_mouse;
- PyObject *param = NULL;
- if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|O", (char**)keywords, &name, &on_mouse, ¶m))
- return NULL;
- if (!PyCallable_Check(on_mouse)) {
- PyErr_SetString(PyExc_TypeError, "on_mouse must be callable");
- return NULL;
- }
- if (param == NULL) {
- param = Py_None;
- }
- ERRWRAP2(setMouseCallback(name, OnMouse, Py_BuildValue("OO", on_mouse, param)));
- Py_RETURN_NONE;
- }
- #endif
- static void OnChange(int pos, void *param)
- {
- PyGILState_STATE gstate;
- gstate = PyGILState_Ensure();
- PyObject *o = (PyObject*)param;
- PyObject *args = Py_BuildValue("(i)", pos);
- PyObject *r = PyObject_Call(PyTuple_GetItem(o, 0), args, NULL);
- if (r == NULL)
- PyErr_Print();
- Py_DECREF(args);
- PyGILState_Release(gstate);
- }
- #ifdef HAVE_OPENCV_HIGHGUI
- static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args)
- {
- PyObject *on_change;
- char* trackbar_name;
- char* window_name;
- int *value = new int;
- int count;
- if (!PyArg_ParseTuple(args, "ssiiO", &trackbar_name, &window_name, value, &count, &on_change))
- return NULL;
- if (!PyCallable_Check(on_change)) {
- PyErr_SetString(PyExc_TypeError, "on_change must be callable");
- return NULL;
- }
- ERRWRAP2(createTrackbar(trackbar_name, window_name, value, count, OnChange, Py_BuildValue("OO", on_change, Py_None)));
- Py_RETURN_NONE;
- }
- static void OnButtonChange(int state, void *param)
- {
- PyGILState_STATE gstate;
- gstate = PyGILState_Ensure();
- PyObject *o = (PyObject*)param;
- PyObject *args;
- if(PyTuple_GetItem(o, 1) != NULL)
- {
- args = Py_BuildValue("(iO)", state, PyTuple_GetItem(o,1));
- }
- else
- {
- args = Py_BuildValue("(i)", state);
- }
- PyObject *r = PyObject_Call(PyTuple_GetItem(o, 0), args, NULL);
- if (r == NULL)
- PyErr_Print();
- Py_DECREF(args);
- PyGILState_Release(gstate);
- }
- static PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw)
- {
- const char* keywords[] = {"buttonName", "onChange", "userData", "buttonType", "initialButtonState", NULL};
- PyObject *on_change;
- PyObject *userdata = NULL;
- char* button_name;
- int button_type = 0;
- int initial_button_state = 0;
- if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|Oii", (char**)keywords, &button_name, &on_change, &userdata, &button_type, &initial_button_state))
- return NULL;
- if (!PyCallable_Check(on_change)) {
- PyErr_SetString(PyExc_TypeError, "onChange must be callable");
- return NULL;
- }
- if (userdata == NULL) {
- userdata = Py_None;
- }
- ERRWRAP2(createButton(button_name, OnButtonChange, Py_BuildValue("OO", on_change, userdata), button_type, initial_button_state != 0));
- Py_RETURN_NONE;
- }
- #endif
- ///////////////////////////////////////////////////////////////////////////////////////
- static int convert_to_char(PyObject *o, char *dst, const char *name = "no_name")
- {
- if (PyString_Check(o) && PyString_Size(o) == 1) {
- *dst = PyString_AsString(o)[0];
- return 1;
- } else {
- (*dst) = 0;
- return failmsg("Expected single character string for argument '%s'", name);
- }
- }
- #ifdef __GNUC__
- # pragma GCC diagnostic ignored "-Wunused-parameter"
- # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
- #endif
- #include QUOTEME(build/pybv_generated_types.h)
- #include QUOTEME(build/pybv_generated_funcs.h)
- static PyMethodDef special_methods[] = {
- #ifdef HAVE_OPENCV_HIGHGUI
- {"createTrackbar", pycvCreateTrackbar, METH_VARARGS, "createTrackbar(trackbarName, windowName, value, count, onChange) -> None"},
- {"createButton", (PyCFunction)pycvCreateButton, METH_VARARGS | METH_KEYWORDS, "createButton(buttonName, onChange [, userData, buttonType, initialButtonState]) -> None"},
- {"setMouseCallback", (PyCFunction)pycvSetMouseCallback, METH_VARARGS | METH_KEYWORDS, "setMouseCallback(windowName, onMouse [, param]) -> None"},
- #endif
- {NULL, NULL},
- };
- /************************************************************************/
- /* Module init */
- struct ConstDef
- {
- const char * name;
- long val;
- };
- static void init_submodule(PyObject * root, const char * name, PyMethodDef * methods, ConstDef * consts)
- {
- // traverse and create nested submodules
- std::string s = name;
- size_t i = s.find('.');
- while (i < s.length() && i != std::string::npos)
- {
- size_t j = s.find('.', i);
- if (j == std::string::npos)
- j = s.length();
- std::string short_name = s.substr(i, j-i);
- std::string full_name = s.substr(0, j);
- i = j+1;
- PyObject * d = PyModule_GetDict(root);
- PyObject * submod = PyDict_GetItemString(d, short_name.c_str());
- if (submod == NULL)
- {
- submod = PyImport_AddModule(full_name.c_str());
- PyDict_SetItemString(d, short_name.c_str(), submod);
- }
- if (short_name != "")
- root = submod;
- }
- // populate module's dict
- PyObject * d = PyModule_GetDict(root);
- for (PyMethodDef * m = methods; m->ml_name != NULL; ++m)
- {
- PyObject * method_obj = PyCFunction_NewEx(m, NULL, NULL);
- PyDict_SetItemString(d, m->ml_name, method_obj);
- Py_DECREF(method_obj);
- }
- for (ConstDef * c = consts; c->name != NULL; ++c)
- {
- PyDict_SetItemString(d, c->name, PyInt_FromLong(c->val));
- }
- }
- #include QUOTEME(build/pybv_generated_ns_reg.h)
- static int to_ok(PyTypeObject *to)
- {
- to->tp_alloc = PyType_GenericAlloc;
- to->tp_new = PyType_GenericNew;
- to->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
- return (PyType_Ready(to) == 0);
- }
- #define INIT_FUNC_NAME(xxmm) INIT_FUNC_NAME_I(xxmm)
- #define INIT_FUNC_NAME_I(xxmm) PyInit_##xxmm
- #define MODULE_DEF_NAME(xxmm) MODULE_DEF_NAME_I(xxmm)
- #define MODULE_DEF_NAME_I(xxmm) xxmm##_moduledef
- #if PY_MAJOR_VERSION >= 3
- extern "C" CV_EXPORTS PyObject* INIT_FUNC_NAME(MODULE_STR)();
- static struct PyModuleDef MODULE_DEF_NAME(MODULE_STR) =
- {
- PyModuleDef_HEAD_INIT,
- MODULESTR,
- "Python wrapper for ...",
- -1, /* size of per-interpreter state of the module,
- or -1 if the module keeps state in global variables. */
- special_methods
- };
- PyObject* INIT_FUNC_NAME(MODULE_STR)()
- #else
- extern "C" CV_EXPORTS void initcv2();
- void initcv2()
- #endif
- {
- import_array();
- #include QUOTEME(build/pybv_generated_type_reg.h)
- #if PY_MAJOR_VERSION >= 3
- PyObject* m = PyModule_Create(&MODULE_DEF_NAME(MODULE_STR));
- #else
- PyObject* m = Py_InitModule(MODULESTR, special_methods);
- #endif
-
- init_submodules(m); // from "pyopencv_generated_ns_reg.h"
- PyObject* d = PyModule_GetDict(m);
- PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION));
- opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, NULL);
- PyDict_SetItemString(d, "error", opencv_error);
- //Registering UMatWrapper python class in cv2 module:
- if (PyType_Ready(&cv2_UMatWrapperType) < 0)
- #if PY_MAJOR_VERSION >= 3
- return NULL;
- #else
- return;
- #endif
- #if PY_MAJOR_VERSION >= 3
- #define PUBLISH_OBJECT(name, type) Py_INCREF(&type);\
- PyModule_AddObject(m, name, (PyObject *)&type);
- #else
- // Unrolled Py_INCREF(&type) without (PyObject*) cast
- // due to "warning: dereferencing type-punned pointer will break strict-aliasing rules"
- #define PUBLISH_OBJECT(name, type) _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA (&type)->ob_refcnt++;\
- PyModule_AddObject(m, name, (PyObject *)&type);
- #endif
- PUBLISH_OBJECT("UMat", cv2_UMatWrapperType);
- #include QUOTEME(build/pybv_generated_type_publish.h)
- #define PUBLISH(I) PyDict_SetItemString(d, #I, PyInt_FromLong(I))
- //#define PUBLISHU(I) PyDict_SetItemString(d, #I, PyLong_FromUnsignedLong(I))
- #define PUBLISH2(I, value) PyDict_SetItemString(d, #I, PyLong_FromLong(value))
- PUBLISH(CV_8U);
- PUBLISH(CV_8UC1);
- PUBLISH(CV_8UC2);
- PUBLISH(CV_8UC3);
- PUBLISH(CV_8UC4);
- PUBLISH(CV_8S);
- PUBLISH(CV_8SC1);
- PUBLISH(CV_8SC2);
- PUBLISH(CV_8SC3);
- PUBLISH(CV_8SC4);
- PUBLISH(CV_16U);
- PUBLISH(CV_16UC1);
- PUBLISH(CV_16UC2);
- PUBLISH(CV_16UC3);
- PUBLISH(CV_16UC4);
- PUBLISH(CV_16S);
- PUBLISH(CV_16SC1);
- PUBLISH(CV_16SC2);
- PUBLISH(CV_16SC3);
- PUBLISH(CV_16SC4);
- PUBLISH(CV_32S);
- PUBLISH(CV_32SC1);
- PUBLISH(CV_32SC2);
- PUBLISH(CV_32SC3);
- PUBLISH(CV_32SC4);
- PUBLISH(CV_32F);
- PUBLISH(CV_32FC1);
- PUBLISH(CV_32FC2);
- PUBLISH(CV_32FC3);
- PUBLISH(CV_32FC4);
- PUBLISH(CV_64F);
- PUBLISH(CV_64FC1);
- PUBLISH(CV_64FC2);
- PUBLISH(CV_64FC3);
- PUBLISH(CV_64FC4);
- #if PY_MAJOR_VERSION >= 3
- return m;
- #endif
- }
|