5 #include "opencv2/core/types_c.h" 7 #include "opencv2/opencv_modules.hpp" 9 #include "pycompat.hpp" 19 vsnprintf(str,
sizeof(str), fmt, ap);
22 PyErr_SetString(PyExc_TypeError, str);
32 ArgInfo(
const char * name_,
bool outputarg_)
34 , outputarg(outputarg_) {}
37 operator const char *()
const {
return name; }
46 PyEval_RestoreThread(_state);
49 PyThreadState* _state;
58 PyGILState_Release(_state);
64 #define ERRWRAP2(expr) \ 67 PyAllowThreads allowThreads; \ 70 catch (const cv::Exception &e) \ 72 PyErr_SetString(opencv_error, e.what()); \ 78 static PyObject*
failmsgp(
const char *fmt, ...)
84 vsnprintf(str,
sizeof(str), fmt, ap);
87 PyErr_SetString(PyExc_TypeError, str);
97 UMatData*
allocate(PyObject* o,
int dims,
const int* sizes,
int type,
size_t* step)
const 99 UMatData* u =
new UMatData(
this);
100 u->data = u->origdata = (uchar*)PyArray_DATA((PyArrayObject*) o);
101 npy_intp* _strides = PyArray_STRIDES((PyArrayObject*) o);
102 for(
int i = 0; i < dims - 1; i++ )
103 step[i] = (
size_t)_strides[i];
104 step[dims-1] = CV_ELEM_SIZE(type);
105 u->size = sizes[0]*step[0];
110 UMatData*
allocate(
int dims0,
const int* sizes,
int type,
void* data,
size_t* step,
int flags, UMatUsageFlags usageFlags)
const 114 CV_Error(Error::StsAssert,
"The data should normally be NULL!");
116 return stdAllocator->allocate(dims0, sizes, type, data, step, flags, usageFlags);
120 int depth = CV_MAT_DEPTH(type);
121 int cn = CV_MAT_CN(type);
122 const int f = (int)(
sizeof(
size_t)/8);
123 int typenum = depth == CV_8U ? NPY_UBYTE : depth == CV_8S ? NPY_BYTE :
124 depth == CV_16U ? NPY_USHORT : depth == CV_16S ? NPY_SHORT :
125 depth == CV_32S ? NPY_INT : depth == CV_32F ? NPY_FLOAT :
126 depth == CV_64F ? NPY_DOUBLE : f*NPY_ULONGLONG + (f^1)*NPY_UINT;
128 cv::AutoBuffer<npy_intp> _sizes(dims + 1);
129 for( i = 0; i < dims; i++ )
130 _sizes[i] = sizes[i];
133 PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum);
135 CV_Error_(Error::StsError, (
"The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims));
136 return allocate(o, dims0, sizes, type, step);
139 bool allocate(UMatData* u,
int accessFlags, UMatUsageFlags usageFlags)
const 141 return stdAllocator->allocate(u, accessFlags, usageFlags);
149 PyObject* o = (PyObject*)u->userdata;
161 template<
typename T>
static 164 template<
typename T>
static 177 if(!o || o == Py_None)
186 double v[] = {(double)PyInt_AsLong((PyObject*)o), 0., 0., 0.};
187 m = Mat(4, 1, CV_64F, v).clone();
190 if( PyFloat_Check(o) )
192 double v[] = {PyFloat_AsDouble((PyObject*)o), 0., 0., 0.};
193 m = Mat(4, 1, CV_64F, v).clone();
196 if( PyTuple_Check(o) )
198 int i, sz = (int)PyTuple_Size((PyObject*)o);
199 m = Mat(sz, 1, CV_64F);
200 for( i = 0; i < sz; i++ )
202 PyObject* oi = PyTuple_GET_ITEM(o, i);
203 if( PyInt_Check(oi) )
204 m.at<
double>(i) = (
double)PyInt_AsLong(oi);
205 else if( PyFloat_Check(oi) )
206 m.at<
double>(i) = (
double)PyFloat_AsDouble(oi);
217 if( !PyArray_Check(o) )
219 failmsg(
"%s is not a numpy array, neither a scalar", info.
name);
223 PyArrayObject* oarr = (PyArrayObject*) o;
225 bool needcopy =
false, needcast =
false;
226 int typenum = PyArray_TYPE(oarr), new_typenum = typenum;
227 int type = typenum == NPY_UBYTE ? CV_8U :
228 typenum == NPY_BYTE ? CV_8S :
229 typenum == NPY_USHORT ? CV_16U :
230 typenum == NPY_SHORT ? CV_16S :
231 typenum == NPY_INT ? CV_32S :
232 typenum == NPY_INT32 ? CV_32S :
233 typenum == NPY_FLOAT ? CV_32F :
234 typenum == NPY_DOUBLE ? CV_64F : -1;
238 if( typenum == NPY_INT64 || typenum == NPY_UINT64 || type == NPY_LONG )
240 needcopy = needcast =
true;
241 new_typenum = NPY_INT;
246 failmsg(
"%s data type = %d is not supported", info.
name, typenum);
252 const int CV_MAX_DIM = 32;
255 int ndims = PyArray_NDIM(oarr);
256 if(ndims >= CV_MAX_DIM)
258 failmsg(
"%s dimensionality (=%d) is too high", info.
name, ndims);
262 int size[CV_MAX_DIM+1];
263 size_t step[CV_MAX_DIM+1];
264 size_t elemsize = CV_ELEM_SIZE1(type);
265 const npy_intp* _sizes = PyArray_DIMS(oarr);
266 const npy_intp* _strides = PyArray_STRIDES(oarr);
267 bool ismultichannel = ndims == 3 && _sizes[2] <= CV_CN_MAX;
269 for(
int i = ndims-1; i >= 0 && !needcopy; i-- )
275 if( (i == ndims-1 && (
size_t)_strides[i] != elemsize) ||
276 (i < ndims-1 && _strides[i] < _strides[i+1]) )
280 if( ismultichannel && _strides[1] != (npy_intp)elemsize*_sizes[2] )
287 failmsg(
"Layout of the output array %s is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)", info.
name);
292 o = PyArray_Cast(oarr, new_typenum);
293 oarr = (PyArrayObject*) o;
296 oarr = PyArray_GETCONTIGUOUS(oarr);
297 o = (PyObject*) oarr;
300 _strides = PyArray_STRIDES(oarr);
303 for(
int i = 0; i < ndims; i++)
305 size[i] = (int)_sizes[i];
306 step[i] = (size_t)_strides[i];
312 step[ndims] = elemsize;
319 type |= CV_MAKETYPE(0, size[2]);
322 if( ndims > 2 && !allowND )
324 failmsg(
"%s has more than 2 dimensions", info.
name);
328 m = Mat(ndims, size, type, PyArray_DATA(oarr), step);
329 m.u = g_numpyAllocator.
allocate(o, ndims, size, type, step);
351 Mat temp, *p = (Mat*)&m;
352 if(!p->u || p->allocator != &g_numpyAllocator)
358 PyObject* o = (PyObject*)p->u->userdata;
365 pyopencv_to(const_cast<PyObject*>(o), m,
"unknown");
NumpyAllocator g_numpyAllocator
static PyObject * opencv_error
int convert_to_CvMat2(const PyObject *o, cv::Mat &m)
bool allocate(UMatData *u, int accessFlags, UMatUsageFlags usageFlags) const
const MatAllocator * stdAllocator
UMatData * allocate(int dims0, const int *sizes, int type, void *data, size_t *step, int flags, UMatUsageFlags usageFlags) const
static bool pyopencv_to(PyObject *obj, T &p, const char *name="<unknown>")
static int failmsg(const char *fmt,...)
ArgInfo(const char *name_, bool outputarg_)
UMatData * allocate(PyObject *o, int dims, const int *sizes, int type, size_t *step) const
void allocate(int dims, const int *sizes, int type, int *&refcount, uchar *&datastart, uchar *&data, size_t *step)
static PyObject * pyopencv_from(const T &src)
void deallocate(UMatData *u) const
static PyObject * failmsgp(const char *fmt,...)