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_)
37 operator const char *()
const {
return name; }
46 PyEval_RestoreThread(
_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);
93 #if CV_MAJOR_VERSION == 3
95 typedef int AccessFlag;
102 UMatData*
allocate(PyObject* o,
int dims,
const int* sizes,
int type,
size_t* step)
const
104 UMatData* u =
new UMatData(
this);
105 u->data = u->origdata = (uchar*)PyArray_DATA((PyArrayObject*) o);
106 npy_intp* _strides = PyArray_STRIDES((PyArrayObject*) o);
107 for(
int i = 0; i < dims - 1; i++ )
108 step[i] = (
size_t)_strides[i];
109 step[dims-1] = CV_ELEM_SIZE(type);
110 u->size = sizes[0]*step[0];
115 UMatData*
allocate(
int dims0,
const int* sizes,
int type,
void* data,
size_t* step, AccessFlag flags, UMatUsageFlags usageFlags)
const
119 CV_Error(Error::StsAssert,
"The data should normally be NULL!");
121 return stdAllocator->allocate(dims0, sizes, type, data, step, flags, usageFlags);
125 int depth = CV_MAT_DEPTH(type);
126 int cn = CV_MAT_CN(type);
127 const int f = (int)(
sizeof(
size_t)/8);
128 int typenum = depth == CV_8U ? NPY_UBYTE : depth == CV_8S ? NPY_BYTE :
129 depth == CV_16U ? NPY_USHORT : depth == CV_16S ? NPY_SHORT :
130 depth == CV_32S ? NPY_INT : depth == CV_32F ? NPY_FLOAT :
131 depth == CV_64F ? NPY_DOUBLE :
f*NPY_ULONGLONG + (
f^1)*NPY_UINT;
133 cv::AutoBuffer<npy_intp> _sizes(dims + 1);
134 for( i = 0; i < dims; i++ )
135 _sizes[i] = sizes[i];
138 PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum);
140 CV_Error_(Error::StsError, (
"The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims));
141 return allocate(o, dims0, sizes, type, step);
144 bool allocate(UMatData* u, AccessFlag accessFlags, UMatUsageFlags usageFlags)
const
146 return stdAllocator->allocate(u, accessFlags, usageFlags);
154 PyObject* o = (PyObject*)u->userdata;
166 template<
typename T>
static
167 bool pyopencv_to(PyObject* obj, T& p,
const char* name =
"<unknown>");
169 template<
typename T>
static
182 if(!o || o == Py_None)
191 double v[] = {(double)PyInt_AsLong((PyObject*)o), 0., 0., 0.};
192 m = Mat(4, 1, CV_64F, v).clone();
195 if( PyFloat_Check(o) )
197 double v[] = {PyFloat_AsDouble((PyObject*)o), 0., 0., 0.};
198 m = Mat(4, 1, CV_64F, v).clone();
201 if( PyTuple_Check(o) )
203 int i, sz = (int)PyTuple_Size((PyObject*)o);
204 m = Mat(sz, 1, CV_64F);
205 for( i = 0; i < sz; i++ )
207 PyObject* oi = PyTuple_GET_ITEM(o, i);
208 if( PyInt_Check(oi) )
209 m.at<
double>(i) = (
double)PyInt_AsLong(oi);
210 else if( PyFloat_Check(oi) )
211 m.at<
double>(i) = (
double)PyFloat_AsDouble(oi);
222 if( !PyArray_Check(o) )
224 failmsg(
"%s is not a numpy array, neither a scalar", info.
name);
228 PyArrayObject* oarr = (PyArrayObject*) o;
230 bool needcopy =
false, needcast =
false;
231 int typenum = PyArray_TYPE(oarr), new_typenum = typenum;
232 int type = typenum == NPY_UBYTE ? CV_8U :
233 typenum == NPY_BYTE ? CV_8S :
234 typenum == NPY_USHORT ? CV_16U :
235 typenum == NPY_SHORT ? CV_16S :
236 typenum == NPY_INT ? CV_32S :
237 typenum == NPY_INT32 ? CV_32S :
238 typenum == NPY_FLOAT ? CV_32F :
239 typenum == NPY_DOUBLE ? CV_64F : -1;
243 if( typenum == NPY_INT64 || typenum == NPY_UINT64 || type == NPY_LONG )
245 needcopy = needcast =
true;
246 new_typenum = NPY_INT;
251 failmsg(
"%s data type = %d is not supported", info.
name, typenum);
257 const int CV_MAX_DIM = 32;
260 int ndims = PyArray_NDIM(oarr);
261 if(ndims >= CV_MAX_DIM)
263 failmsg(
"%s dimensionality (=%d) is too high", info.
name, ndims);
267 int size[CV_MAX_DIM+1];
268 size_t step[CV_MAX_DIM+1];
269 size_t elemsize = CV_ELEM_SIZE1(type);
270 const npy_intp* _sizes = PyArray_DIMS(oarr);
271 const npy_intp* _strides = PyArray_STRIDES(oarr);
272 bool ismultichannel = ndims == 3 && _sizes[2] <= CV_CN_MAX;
274 for(
int i = ndims-1; i >= 0 && !needcopy; i-- )
280 if( (i == ndims-1 && (
size_t)_strides[i] != elemsize) ||
281 (i < ndims-1 && _strides[i] < _strides[i+1]) )
285 if( ismultichannel && _strides[1] != (npy_intp)elemsize*_sizes[2] )
292 failmsg(
"Layout of the output array %s is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)", info.
name);
297 o = PyArray_Cast(oarr, new_typenum);
298 oarr = (PyArrayObject*) o;
301 oarr = PyArray_GETCONTIGUOUS(oarr);
302 o = (PyObject*) oarr;
305 _strides = PyArray_STRIDES(oarr);
308 for(
int i = 0; i < ndims; i++)
310 size[i] = (int)_sizes[i];
311 step[i] = (size_t)_strides[i];
317 step[ndims] = elemsize;
324 type |= CV_MAKETYPE(0, size[2]);
327 if( ndims > 2 && !allowND )
329 failmsg(
"%s has more than 2 dimensions", info.
name);
333 m = Mat(ndims, size, type, PyArray_DATA(oarr), step);
356 Mat temp, *p = (Mat*)&m;
363 PyObject* o = (PyObject*)p->u->userdata;
370 pyopencv_to(
const_cast<PyObject*
>(o), m,
"unknown");