30 vsnprintf(str,
sizeof(str), fmt, ap);
33 PyErr_SetString(PyExc_TypeError, str);
40 PyAllowThreads() : _state(PyEval_SaveThread()) {}
43 PyEval_RestoreThread(_state);
46 PyThreadState* _state;
52 PyEnsureGIL() : _state(PyGILState_Ensure()) {}
55 PyGILState_Release(_state);
58 PyGILState_STATE _state;
76 class NumpyAllocator :
public ::cv::MatAllocator
79 NumpyAllocator() { stdAllocator = ::cv::Mat::getStdAllocator(); }
82 ::cv::UMatData* allocate(
PyObject* o,
int dims,
const int* sizes,
int type,
size_t* step)
const
84 ::cv::UMatData* u = new ::cv::UMatData(
this);
85 u->data = u->origdata = (uchar*)PyArray_DATA((PyArrayObject*) o);
86 npy_intp* _strides = PyArray_STRIDES((PyArrayObject*) o);
87 for(
int i = 0; i < dims - 1; i++ )
88 step[i] = (
size_t)_strides[i];
89 step[dims-1] = CV_ELEM_SIZE(type);
90 u->size = sizes[0]*step[0];
95 virtual ::cv::UMatData* allocate(
int dims0,
const int* sizes,
int type,
96 void* data,
size_t* step,
int flags,
97 ::cv::UMatUsageFlags usageFlags)
const
101 CV_Error(::cv::Error::StsAssert,
"The data should normally be NULL!");
103 return stdAllocator->allocate(dims0, sizes, type, data, step, flags, usageFlags);
107 int depth = CV_MAT_DEPTH(type);
108 int cn = CV_MAT_CN(type);
109 const int f = (int)(
sizeof(
size_t)/8);
110 int typenum = depth == CV_8U ? NPY_UBYTE : depth == CV_8S ? NPY_BYTE :
111 depth == CV_16U ? NPY_USHORT : depth == CV_16S ? NPY_SHORT :
112 depth == CV_32S ? NPY_INT : depth == CV_32F ? NPY_FLOAT :
113 depth == CV_64F ? NPY_DOUBLE : f*NPY_ULONGLONG + (f^1)*NPY_UINT;
115 ::cv::AutoBuffer<npy_intp> _sizes(dims + 1);
116 for( i = 0; i < dims; i++ )
117 _sizes[i] = sizes[i];
120 PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum);
122 CV_Error_(::cv::Error::StsError, (
"The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims));
123 return allocate(o, dims0, sizes, type, step);
126 bool allocate(::cv::UMatData* u,
int accessFlags, ::cv::UMatUsageFlags usageFlags)
const
128 return stdAllocator->allocate(u, accessFlags, usageFlags);
131 void deallocate(::cv::UMatData* u)
const
142 const ::cv::MatAllocator* stdAllocator;
145 class NumpyAllocator :
public ::cv::MatAllocator
151 void allocate(
int dims,
const int* sizes,
int type,
int*& refcount,
152 uchar*& datastart, uchar*& data,
size_t* step)
156 int depth = CV_MAT_DEPTH(type);
157 int cn = CV_MAT_CN(type);
158 const int f = (int)(
sizeof(
size_t)/8);
159 int typenum = depth == CV_8U ? NPY_UBYTE : depth == CV_8S ? NPY_BYTE :
160 depth == CV_16U ? NPY_USHORT : depth == CV_16S ? NPY_SHORT :
161 depth == CV_32S ? NPY_INT : depth == CV_32F ? NPY_FLOAT :
162 depth == CV_64F ? NPY_DOUBLE : f*NPY_ULONGLONG + (f^1)*NPY_UINT;
164 npy_intp _sizes[CV_MAX_DIM+1];
165 for( i = 0; i < dims; i++ )
166 _sizes[i] = sizes[i];
174 PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum);
176 CV_Error_(CV_StsError, (
"The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims));
178 npy_intp* _strides = PyArray_STRIDES((PyArrayObject*) o);
179 for( i = 0; i < dims - (cn > 1); i++ )
180 step[i] = (
size_t)_strides[i];
181 datastart = data = (uchar*)PyArray_DATA((PyArrayObject*) o);
184 void deallocate(
int* refcount, uchar*, uchar*)
211 if(!o || o == Py_None)
217 if( !PyArray_Check(o) )
219 failmsg(
"toMat: Object is not a numpy array");
222 int typenum = PyArray_TYPE(o);
223 int type = typenum == NPY_UBYTE ? CV_8U : typenum == NPY_BYTE ? CV_8S :
224 typenum == NPY_USHORT ? CV_16U : typenum == NPY_SHORT ? CV_16S :
225 typenum == NPY_INT || typenum == NPY_LONG ? CV_32S :
226 typenum == NPY_FLOAT ? CV_32F :
227 typenum == NPY_DOUBLE ? CV_64F : -1;
231 failmsg(
"toMat: Data type = %d is not supported", typenum);
234 int ndims = PyArray_NDIM(o);
236 if(ndims >= CV_MAX_DIM)
238 failmsg(
"toMat: Dimensionality (=%d) is too high", ndims);
241 int size[CV_MAX_DIM+1];
242 size_t step[CV_MAX_DIM+1], elemsize = CV_ELEM_SIZE1(type);
243 const npy_intp* _sizes = PyArray_DIMS(o);
244 const npy_intp* _strides = PyArray_STRIDES(o);
245 bool transposed =
false;
247 for(
int i = 0; i < ndims; i++)
249 size[i] = (int)_sizes[i];
250 step[i] = (size_t)_strides[i];
253 if( ndims == 0 || step[ndims-1] > elemsize ) {
255 step[ndims] = elemsize;
259 if( ndims >= 2 && step[0] < step[1] )
261 std::swap(size[0], size[1]);
262 std::swap(step[0], step[1]);
274 if( ndims == 3 && size[2] <= CV_CN_MAX && step[1] == elemsize*size[2] )
277 type |= CV_MAKETYPE(0, size[2]);
283 failmsg(
"toMat: Object has more than 2 dimensions");
286 m = ::cv::Mat(ndims, size, type, PyArray_DATA(o), step);
317 ::cv::Mat temp, *p = (::cv::Mat*)&m;
332 ::cv::Mat temp, *p = (::cv::Mat*)&m;
350 int* dim_data =
new int[2];
352 dim_data[1] = (int)(pt_v.size());
354 return PyArray_FromDimsAndData( 2, dim_data, NPY_DOUBLE, (
char*) &(pt_v._data[0]) );