48 vsnprintf(str,
sizeof(str), fmt, ap);
51 PyErr_SetString(PyExc_TypeError, str);
63 PyEval_RestoreThread(_state);
69 #define ERRWRAP2(expr) \ 72 PyAllowThreads allowThreads; \ 75 catch (const cv::Exception &e) \ 77 PyErr_SetString(opencv_error, e.what()); \ 85 ( 0x12345678 != *(
const size_t* )
"\x78\x56\x34\x12\0\0\0\0\0" )*
sizeof(
int );
104 void allocate(
int dims,
const int* sizes,
int type,
int*& refcount,
105 uchar*& datastart, uchar*& data,
size_t* step );
107 void deallocate(
int* refcount, uchar* datastart, uchar* data );
110 void NumpyAllocator::allocate(
int dims,
const int* sizes,
int type,
int*& refcount, uchar*& datastart, uchar*& data,
size_t* step )
112 int depth = CV_MAT_DEPTH( type );
113 int cn = CV_MAT_CN( type );
114 const int f = ( int )(
sizeof(
size_t )/8 );
115 int typenum = depth == CV_8U ? NPY_UBYTE : depth == CV_8S ? NPY_BYTE :
116 depth == CV_16U ? NPY_USHORT : depth == CV_16S ? NPY_SHORT :
117 depth == CV_32S ? NPY_INT : depth == CV_32F ? NPY_FLOAT :
118 depth == CV_64F ? NPY_DOUBLE : f*NPY_ULONGLONG + (f^1)*NPY_UINT;
120 npy_intp _sizes[CV_MAX_DIM+1];
121 for( i = 0; i < dims; i++ )
122 _sizes[i] = sizes[i];
130 PyObject* o = PyArray_SimpleNew( dims, _sizes, typenum );
132 CV_Error_(CV_StsError, (
"The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims));
134 npy_intp* _strides = PyArray_STRIDES((PyArrayObject*) o);
135 for( i = 0; i < dims - (cn > 1); i++ )
136 step[i] = (
size_t)_strides[i];
137 datastart = data = (uchar*)PyArray_DATA((PyArrayObject*)o);
158 if(!o || o == Py_None)
165 if( !PyArray_Check(o) )
172 int typenum = PyArray_TYPE((PyArrayObject*) o);
173 int type = typenum == NPY_UBYTE ? CV_8U : typenum == NPY_BYTE ? CV_8S :
174 typenum == NPY_USHORT ? CV_16U : typenum == NPY_SHORT ? CV_16S :
175 typenum == NPY_INT || typenum == NPY_LONG ? CV_32S :
176 typenum == NPY_FLOAT ? CV_32F :
177 typenum == NPY_DOUBLE ? CV_64F : -1;
181 failmsg(
"data type = %d is not supported", typenum);
185 int ndims = PyArray_NDIM((PyArrayObject*) o);
186 if(ndims >= CV_MAX_DIM)
188 failmsg(
"dimensionality (=%d) is too high", ndims);
192 int size[CV_MAX_DIM+1];
193 size_t step[CV_MAX_DIM+1], elemsize = CV_ELEM_SIZE1(type);
194 const npy_intp* _sizes = PyArray_DIMS((PyArrayObject*) o);
195 const npy_intp* _strides = PyArray_STRIDES((PyArrayObject*) o);
196 bool transposed =
false;
198 for(
int i = 0; i < ndims; i++)
200 size[i] = (int)_sizes[i];
201 step[i] = (size_t)_strides[i];
204 if( ndims == 0 || step[ndims-1] > elemsize ) {
206 step[ndims] = elemsize;
210 if( ndims >= 2 && step[0] < step[1] )
212 std::swap(size[0], size[1]);
213 std::swap(step[0], step[1]);
217 if( ndims == 3 && size[2] <= CV_CN_MAX && step[1] == elemsize*size[2] )
220 type |= CV_MAKETYPE(0, size[2]);
225 failmsg(
"more than 2 dimensions");
229 m = cv::Mat(ndims, size, type, PyArray_DATA((PyArrayObject*) o), step);
253 Mat temp, *p = (Mat*)&m;
254 if(!p->refcount || p->allocator != &g_numpyAllocator)
static size_t REFCOUNT_OFFSET
static PyObject * opencv_error
static int * refcountFromPyObject(const PyObject *obj)
int convert_to_CvMat2(const PyObject *o, cv::Mat &m)
PyObject * pyopencv_from(const Mat &m)
static int failmsg(const char *fmt,...)
static PyObject * pyObjectFromRefcount(const int *refcount)
void deallocate(int *refcount, uchar *datastart, uchar *data)
void allocate(int dims, const int *sizes, int type, int *&refcount, uchar *&datastart, uchar *&data, size_t *step)
NumpyAllocator g_numpyAllocator