2013-12-26 17:35:59 +08:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1800)
|
|
|
|
// eliminating duplicated round() declaration
|
2015-10-16 22:10:00 +08:00
|
|
|
#define HAVE_ROUND 1
|
2018-02-08 23:21:46 +08:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:5033) // 'register' is no longer a supported storage class
|
2013-12-26 17:35:59 +08:00
|
|
|
#endif
|
2018-03-26 02:39:29 +08:00
|
|
|
#include <math.h>
|
2011-05-04 00:00:31 +08:00
|
|
|
#include <Python.h>
|
2018-02-08 23:21:46 +08:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1800)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
2011-05-04 00:00:31 +08:00
|
|
|
|
|
|
|
#define MODULESTR "cv2"
|
2013-07-16 18:33:15 +08:00
|
|
|
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
2013-04-12 21:05:23 +08:00
|
|
|
#include <numpy/ndarrayobject.h>
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2017-12-10 02:54:52 +08:00
|
|
|
#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
|
|
|
|
|
2014-05-12 18:58:54 +08:00
|
|
|
#include "pyopencv_generated_include.h"
|
2014-07-30 20:11:08 +08:00
|
|
|
#include "opencv2/core/types_c.h"
|
2013-04-08 02:45:38 +08:00
|
|
|
|
2012-04-14 05:50:59 +08:00
|
|
|
#include "opencv2/opencv_modules.hpp"
|
|
|
|
|
2013-06-13 04:03:34 +08:00
|
|
|
#include "pycompat.hpp"
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
|
2011-05-04 00:00:31 +08:00
|
|
|
static PyObject* opencv_error = 0;
|
|
|
|
|
|
|
|
static int failmsg(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char str[1000];
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-05-04 00:00:31 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(str, sizeof(str), fmt, ap);
|
|
|
|
va_end(ap);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-05-04 00:00:31 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError, str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-08 02:03:17 +08:00
|
|
|
struct ArgInfo
|
|
|
|
{
|
|
|
|
const char * name;
|
|
|
|
bool outputarg;
|
|
|
|
// more fields may be added if necessary
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
ArgInfo(const char * name_, bool outputarg_)
|
2012-08-08 02:03:17 +08:00
|
|
|
: name(name_)
|
|
|
|
, outputarg(outputarg_) {}
|
|
|
|
|
|
|
|
// to match with older pyopencv_to function signature
|
|
|
|
operator const char *() const { return name; }
|
|
|
|
};
|
|
|
|
|
2012-03-10 13:56:40 +08:00
|
|
|
class PyAllowThreads
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PyAllowThreads() : _state(PyEval_SaveThread()) {}
|
2012-06-08 01:21:29 +08:00
|
|
|
~PyAllowThreads()
|
2012-03-10 13:56:40 +08:00
|
|
|
{
|
|
|
|
PyEval_RestoreThread(_state);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
PyThreadState* _state;
|
|
|
|
};
|
|
|
|
|
2012-05-22 19:21:25 +08:00
|
|
|
class PyEnsureGIL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PyEnsureGIL() : _state(PyGILState_Ensure()) {}
|
2012-06-08 01:21:29 +08:00
|
|
|
~PyEnsureGIL()
|
2012-05-22 19:21:25 +08:00
|
|
|
{
|
|
|
|
PyGILState_Release(_state);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
PyGILState_STATE _state;
|
|
|
|
};
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
#define ERRWRAP2(expr) \
|
|
|
|
try \
|
|
|
|
{ \
|
2012-03-10 13:56:40 +08:00
|
|
|
PyAllowThreads allowThreads; \
|
2010-10-29 21:09:49 +08:00
|
|
|
expr; \
|
|
|
|
} \
|
|
|
|
catch (const cv::Exception &e) \
|
|
|
|
{ \
|
|
|
|
PyErr_SetString(opencv_error, e.what()); \
|
|
|
|
return 0; \
|
|
|
|
}
|
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
using namespace cv;
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
typedef std::vector<uchar> vector_uchar;
|
2013-07-11 11:58:37 +08:00
|
|
|
typedef std::vector<char> vector_char;
|
2013-02-25 00:14:01 +08:00
|
|
|
typedef std::vector<int> vector_int;
|
|
|
|
typedef std::vector<float> vector_float;
|
|
|
|
typedef std::vector<double> vector_double;
|
2017-10-14 07:27:37 +08:00
|
|
|
typedef std::vector<size_t> vector_size_t;
|
2013-02-25 00:14:01 +08:00
|
|
|
typedef std::vector<Point> vector_Point;
|
|
|
|
typedef std::vector<Point2f> vector_Point2f;
|
2015-10-11 12:12:42 +08:00
|
|
|
typedef std::vector<Point3f> vector_Point3f;
|
2013-02-25 00:14:01 +08:00
|
|
|
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;
|
2015-10-30 17:02:33 +08:00
|
|
|
typedef std::vector<Rect2d> vector_Rect2d;
|
2013-02-25 00:14:01 +08:00
|
|
|
typedef std::vector<KeyPoint> vector_KeyPoint;
|
|
|
|
typedef std::vector<Mat> vector_Mat;
|
2017-10-14 07:27:37 +08:00
|
|
|
typedef std::vector<std::vector<Mat> > vector_vector_Mat;
|
2016-02-08 00:04:39 +08:00
|
|
|
typedef std::vector<UMat> vector_UMat;
|
2013-02-25 00:14:01 +08:00
|
|
|
typedef std::vector<DMatch> vector_DMatch;
|
2013-03-23 00:37:49 +08:00
|
|
|
typedef std::vector<String> vector_String;
|
2014-05-12 18:58:54 +08:00
|
|
|
typedef std::vector<Scalar> vector_Scalar;
|
2013-07-11 11:58:37 +08:00
|
|
|
|
|
|
|
typedef std::vector<std::vector<char> > vector_vector_char;
|
2013-02-25 00:14:01 +08:00
|
|
|
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;
|
2016-10-16 23:09:35 +08:00
|
|
|
typedef std::vector<std::vector<KeyPoint> > vector_vector_KeyPoint;
|
2011-09-07 17:38:22 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
class NumpyAllocator : public MatAllocator
|
|
|
|
{
|
|
|
|
public:
|
2013-10-23 03:34:16 +08:00
|
|
|
NumpyAllocator() { stdAllocator = Mat::getStdAllocator(); }
|
2010-10-29 21:09:49 +08:00
|
|
|
~NumpyAllocator() {}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-10-23 03:34:16 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-20 23:27:26 +08:00
|
|
|
UMatData* allocate(int dims0, const int* sizes, int type, void* data, size_t* step, int flags, UMatUsageFlags usageFlags) const CV_OVERRIDE
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2013-12-01 07:12:19 +08:00
|
|
|
if( data != 0 )
|
|
|
|
{
|
2017-07-30 18:54:54 +08:00
|
|
|
// issue #6969: CV_Error(Error::StsAssert, "The data should normally be NULL!");
|
2013-12-01 07:12:19 +08:00
|
|
|
// probably this is safe to do in such extreme case
|
2014-02-10 20:34:45 +08:00
|
|
|
return stdAllocator->allocate(dims0, sizes, type, data, step, flags, usageFlags);
|
2013-12-01 07:12:19 +08:00
|
|
|
}
|
2012-05-22 19:21:25 +08:00
|
|
|
PyEnsureGIL gil;
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
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 :
|
2013-10-23 03:34:16 +08:00
|
|
|
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;
|
2013-04-12 21:05:23 +08:00
|
|
|
cv::AutoBuffer<npy_intp> _sizes(dims + 1);
|
2010-10-29 21:09:49 +08:00
|
|
|
for( i = 0; i < dims; i++ )
|
|
|
|
_sizes[i] = sizes[i];
|
|
|
|
if( cn > 1 )
|
2013-04-12 21:05:23 +08:00
|
|
|
_sizes[dims++] = cn;
|
2010-10-29 21:09:49 +08:00
|
|
|
PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum);
|
|
|
|
if(!o)
|
2013-04-12 21:05:23 +08:00
|
|
|
CV_Error_(Error::StsError, ("The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims));
|
2013-10-23 03:34:16 +08:00
|
|
|
return allocate(o, dims0, sizes, type, step);
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2018-03-20 23:27:26 +08:00
|
|
|
bool allocate(UMatData* u, int accessFlags, UMatUsageFlags usageFlags) const CV_OVERRIDE
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2014-02-10 20:34:45 +08:00
|
|
|
return stdAllocator->allocate(u, accessFlags, usageFlags);
|
2013-10-23 03:34:16 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 23:27:26 +08:00
|
|
|
void deallocate(UMatData* u) const CV_OVERRIDE
|
2013-10-23 03:34:16 +08:00
|
|
|
{
|
2015-06-17 17:07:57 +08:00
|
|
|
if(!u)
|
|
|
|
return;
|
|
|
|
PyEnsureGIL gil;
|
|
|
|
CV_Assert(u->urefcount >= 0);
|
|
|
|
CV_Assert(u->refcount >= 0);
|
|
|
|
if(u->refcount == 0)
|
2013-10-23 03:34:16 +08:00
|
|
|
{
|
|
|
|
PyObject* o = (PyObject*)u->userdata;
|
2013-12-01 07:12:19 +08:00
|
|
|
Py_XDECREF(o);
|
2013-10-23 03:34:16 +08:00
|
|
|
delete u;
|
|
|
|
}
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2013-10-23 03:34:16 +08:00
|
|
|
|
|
|
|
const MatAllocator* stdAllocator;
|
2010-10-29 21:09:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NumpyAllocator g_numpyAllocator;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
enum { ARG_NONE = 0, ARG_MAT = 1, ARG_SCALAR = 2 };
|
|
|
|
|
2018-02-14 00:28:11 +08:00
|
|
|
// special case, when the converter needs full ArgInfo structure
|
2013-04-13 02:35:46 +08:00
|
|
|
static bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo info)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2013-04-13 02:35:46 +08:00
|
|
|
bool allowND = true;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!o || o == Py_None)
|
|
|
|
{
|
|
|
|
if( !m.data )
|
|
|
|
m.allocator = &g_numpyAllocator;
|
|
|
|
return true;
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-01-29 00:45:00 +08:00
|
|
|
if( PyInt_Check(o) )
|
|
|
|
{
|
2015-02-25 01:21:34 +08:00
|
|
|
double v[] = {static_cast<double>(PyInt_AsLong((PyObject*)o)), 0., 0., 0.};
|
2013-01-29 00:45:00 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
if( !PyArray_Check(o) )
|
|
|
|
{
|
2013-01-29 00:45:00 +08:00
|
|
|
failmsg("%s is not a numpy array, neither a scalar", info.name);
|
2010-11-03 01:58:22 +08:00
|
|
|
return false;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-07-16 18:33:15 +08:00
|
|
|
PyArrayObject* oarr = (PyArrayObject*) o;
|
|
|
|
|
2012-08-14 01:22:06 +08:00
|
|
|
bool needcopy = false, needcast = false;
|
2013-07-16 18:33:15 +08:00
|
|
|
int typenum = PyArray_TYPE(oarr), new_typenum = typenum;
|
2012-08-14 01:22:06 +08:00
|
|
|
int type = typenum == NPY_UBYTE ? CV_8U :
|
|
|
|
typenum == NPY_BYTE ? CV_8S :
|
|
|
|
typenum == NPY_USHORT ? CV_16U :
|
|
|
|
typenum == NPY_SHORT ? CV_16S :
|
2013-01-28 22:30:20 +08:00
|
|
|
typenum == NPY_INT ? CV_32S :
|
2012-08-14 01:22:06 +08:00
|
|
|
typenum == NPY_INT32 ? CV_32S :
|
2010-10-29 21:09:49 +08:00
|
|
|
typenum == NPY_FLOAT ? CV_32F :
|
|
|
|
typenum == NPY_DOUBLE ? CV_64F : -1;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
if( type < 0 )
|
|
|
|
{
|
2015-08-13 04:50:05 +08:00
|
|
|
if( typenum == NPY_INT64 || typenum == NPY_UINT64 || typenum == NPY_LONG )
|
2012-08-14 01:22:06 +08:00
|
|
|
{
|
|
|
|
needcopy = needcast = true;
|
2013-01-28 22:30:20 +08:00
|
|
|
new_typenum = NPY_INT;
|
2012-08-14 01:22:06 +08:00
|
|
|
type = CV_32S;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
failmsg("%s data type = %d is not supported", info.name, typenum);
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-04-12 21:05:23 +08:00
|
|
|
#ifndef CV_MAX_DIM
|
|
|
|
const int CV_MAX_DIM = 32;
|
|
|
|
#endif
|
|
|
|
|
2013-07-16 18:33:15 +08:00
|
|
|
int ndims = PyArray_NDIM(oarr);
|
2010-10-29 21:09:49 +08:00
|
|
|
if(ndims >= CV_MAX_DIM)
|
|
|
|
{
|
2012-08-08 02:03:17 +08:00
|
|
|
failmsg("%s dimensionality (=%d) is too high", info.name, ndims);
|
2010-11-03 01:58:22 +08:00
|
|
|
return false;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-10-29 21:09:49 +08:00
|
|
|
int size[CV_MAX_DIM+1];
|
2013-04-12 21:05:23 +08:00
|
|
|
size_t step[CV_MAX_DIM+1];
|
|
|
|
size_t elemsize = CV_ELEM_SIZE1(type);
|
2013-07-16 18:33:15 +08:00
|
|
|
const npy_intp* _sizes = PyArray_DIMS(oarr);
|
|
|
|
const npy_intp* _strides = PyArray_STRIDES(oarr);
|
2012-08-08 02:03:17 +08:00
|
|
|
bool ismultichannel = ndims == 3 && _sizes[2] <= CV_CN_MAX;
|
|
|
|
|
2012-08-14 01:22:06 +08:00
|
|
|
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
|
2015-06-12 00:41:12 +08:00
|
|
|
// 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]) )
|
2012-08-14 01:22:06 +08:00
|
|
|
needcopy = true;
|
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-10-18 19:29:03 +08:00
|
|
|
if( ismultichannel && _strides[1] != (npy_intp)elemsize*_sizes[2] )
|
|
|
|
needcopy = true;
|
|
|
|
|
2012-08-08 02:03:17 +08:00
|
|
|
if (needcopy)
|
|
|
|
{
|
|
|
|
if (info.outputarg)
|
|
|
|
{
|
2012-10-19 17:43:14 +08:00
|
|
|
failmsg("Layout of the output array %s is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)", info.name);
|
2012-08-08 02:03:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-16 18:33:15 +08:00
|
|
|
|
|
|
|
if( needcast ) {
|
|
|
|
o = PyArray_Cast(oarr, new_typenum);
|
|
|
|
oarr = (PyArrayObject*) o;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
oarr = PyArray_GETCONTIGUOUS(oarr);
|
|
|
|
o = (PyObject*) oarr;
|
|
|
|
}
|
|
|
|
|
|
|
|
_strides = PyArray_STRIDES(oarr);
|
2012-08-08 02:03:17 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2015-06-12 00:41:12 +08:00
|
|
|
// Normalize strides in case NPY_RELAXED_STRIDES is set
|
|
|
|
size_t default_step = elemsize;
|
|
|
|
for ( int i = ndims - 1; i >= 0; --i )
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
|
|
|
size[i] = (int)_sizes[i];
|
2015-06-12 00:41:12 +08:00
|
|
|
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];
|
|
|
|
}
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2012-08-08 02:03:17 +08:00
|
|
|
// handle degenerate case
|
|
|
|
if( ndims == 0) {
|
2010-10-29 21:09:49 +08:00
|
|
|
size[ndims] = 1;
|
|
|
|
step[ndims] = elemsize;
|
|
|
|
ndims++;
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2012-08-08 02:03:17 +08:00
|
|
|
if( ismultichannel )
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
ndims--;
|
|
|
|
type |= CV_MAKETYPE(0, size[2]);
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
if( ndims > 2 && !allowND )
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2012-08-08 02:03:17 +08:00
|
|
|
failmsg("%s has more than 2 dimensions", info.name);
|
2010-11-03 01:58:22 +08:00
|
|
|
return false;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-07-16 18:33:15 +08:00
|
|
|
m = Mat(ndims, size, type, PyArray_DATA(oarr), step);
|
2013-10-23 03:34:16 +08:00
|
|
|
m.u = g_numpyAllocator.allocate(o, ndims, size, type, step);
|
2014-02-03 18:46:22 +08:00
|
|
|
m.addref();
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-10-23 03:34:16 +08:00
|
|
|
if( !needcopy )
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2013-10-23 03:34:16 +08:00
|
|
|
Py_INCREF(o);
|
|
|
|
}
|
2010-10-29 21:09:49 +08:00
|
|
|
m.allocator = &g_numpyAllocator;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
return true;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2014-08-03 08:28:20 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* o, Mat& m, const char* name)
|
|
|
|
{
|
|
|
|
return pyopencv_to(o, m, ArgInfo(name, 0));
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:11:47 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2017-04-26 20:22:14 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Mat& m)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2011-06-14 05:47:35 +08:00
|
|
|
if( !m.data )
|
2011-06-14 06:25:21 +08:00
|
|
|
Py_RETURN_NONE;
|
2010-11-03 01:58:22 +08:00
|
|
|
Mat temp, *p = (Mat*)&m;
|
2013-10-24 20:08:15 +08:00
|
|
|
if(!p->u || p->allocator != &g_numpyAllocator)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2011-06-11 01:22:33 +08:00
|
|
|
temp.allocator = &g_numpyAllocator;
|
2013-04-05 12:43:10 +08:00
|
|
|
ERRWRAP2(m.copyTo(temp));
|
2010-11-03 01:58:22 +08:00
|
|
|
p = &temp;
|
|
|
|
}
|
2013-10-23 03:34:16 +08:00
|
|
|
PyObject* o = (PyObject*)p->u->userdata;
|
|
|
|
Py_INCREF(o);
|
|
|
|
return o;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2017-02-21 10:29:28 +08:00
|
|
|
template<typename _Tp, int m, int n>
|
|
|
|
PyObject* pyopencv_from(const Matx<_Tp, m, n>& matx)
|
|
|
|
{
|
|
|
|
return pyopencv_from(Mat(matx));
|
|
|
|
}
|
2016-02-08 00:04:39 +08:00
|
|
|
|
2017-04-26 20:22:14 +08:00
|
|
|
template<typename T>
|
|
|
|
PyObject* pyopencv_from(const cv::Ptr<T>& p)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
return pyopencv_from(*p);
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:04:39 +08:00
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
UMat* um;
|
|
|
|
} cv2_UMatWrapperObject;
|
|
|
|
|
2017-01-21 01:34:41 +08:00
|
|
|
static bool PyObject_IsUMat(PyObject *o);
|
|
|
|
|
|
|
|
// UMatWrapper init - try to map arguments from python to UMat constructors
|
2016-02-08 00:04:39 +08:00
|
|
|
static int UMatWrapper_init(cv2_UMatWrapperObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2017-01-21 01:34:41 +08:00
|
|
|
self->um = NULL;
|
|
|
|
{
|
|
|
|
// constructor ()
|
|
|
|
const char *kwlist[] = {NULL};
|
|
|
|
if (PyArg_ParseTupleAndKeywords(args, kwds, "", (char**) kwlist)) {
|
|
|
|
self->um = new UMat();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
2016-02-08 00:04:39 +08:00
|
|
|
}
|
2017-01-21 01:34:41 +08:00
|
|
|
{
|
|
|
|
// 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;
|
2016-02-08 00:04:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void UMatWrapper_dealloc(cv2_UMatWrapperObject* self)
|
|
|
|
{
|
2017-01-21 01:34:41 +08:00
|
|
|
if (self->um)
|
|
|
|
delete self->um;
|
2016-02-08 00:04:39 +08:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2017-04-20 02:41:48 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:04:39 +08:00
|
|
|
static PyMethodDef UMatWrapper_methods[] = {
|
|
|
|
{"get", (PyCFunction)UMatWrapper_get, METH_NOARGS,
|
|
|
|
"Returns numpy array"
|
|
|
|
},
|
2017-04-20 02:41:48 +08:00
|
|
|
{"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"
|
|
|
|
},
|
2016-02-08 00:04:39 +08:00
|
|
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2017-04-20 02:41:48 +08:00
|
|
|
static PyGetSetDef UMatWrapper_getset[] = {
|
|
|
|
{(char*) "offset", (getter) UMatWrapper_offset_getter, NULL, NULL, NULL},
|
|
|
|
{NULL, NULL, NULL, NULL, NULL} /* Sentinel */
|
|
|
|
};
|
2016-02-08 00:04:39 +08:00
|
|
|
|
|
|
|
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 */
|
2017-04-20 02:41:48 +08:00
|
|
|
UMatWrapper_getset, /* tp_getset */
|
2016-02-08 00:04:39 +08:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2017-01-21 01:34:41 +08:00
|
|
|
static bool PyObject_IsUMat(PyObject *o) {
|
|
|
|
return (o != NULL) && PyObject_TypeCheck(o, &cv2_UMatWrapperType);
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:04:39 +08:00
|
|
|
static bool pyopencv_to(PyObject* o, UMat& um, const ArgInfo info) {
|
2017-01-21 01:34:41 +08:00
|
|
|
if (PyObject_IsUMat(o)) {
|
2016-02-08 00:04:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:11:47 +08:00
|
|
|
static bool pyopencv_to(PyObject *o, Scalar& s, const ArgInfo info)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!o || o == Py_None)
|
|
|
|
return true;
|
2010-10-29 21:09:49 +08:00
|
|
|
if (PySequence_Check(o)) {
|
2017-10-11 19:11:47 +08:00
|
|
|
PyObject *fi = PySequence_Fast(o, info.name);
|
2010-10-29 21:09:49 +08:00
|
|
|
if (fi == NULL)
|
2010-11-03 01:58:22 +08:00
|
|
|
return false;
|
2010-10-29 21:09:49 +08:00
|
|
|
if (4 < PySequence_Fast_GET_SIZE(fi))
|
|
|
|
{
|
2017-10-11 19:11:47 +08:00
|
|
|
failmsg("Scalar value for argument '%s' is longer than 4", info.name);
|
2010-11-03 01:58:22 +08:00
|
|
|
return false;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
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)) {
|
2011-07-19 20:27:07 +08:00
|
|
|
s[(int)i] = PyFloat_AsDouble(item);
|
2010-10-29 21:09:49 +08:00
|
|
|
} else {
|
2017-10-11 19:11:47 +08:00
|
|
|
failmsg("Scalar value for argument '%s' is not numeric", info.name);
|
2010-11-03 01:58:22 +08:00
|
|
|
return false;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(fi);
|
|
|
|
} else {
|
|
|
|
if (PyFloat_Check(o) || PyInt_Check(o)) {
|
|
|
|
s[0] = PyFloat_AsDouble(o);
|
|
|
|
} else {
|
2017-10-11 19:11:47 +08:00
|
|
|
failmsg("Scalar value for argument '%s' is not numeric", info.name);
|
2010-11-03 01:58:22 +08:00
|
|
|
return false;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
}
|
2010-11-03 01:58:22 +08:00
|
|
|
return true;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 19:11:47 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject *o, Scalar& s, const char *name)
|
|
|
|
{
|
|
|
|
return pyopencv_to(o, s, ArgInfo(name, 0));
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Scalar& src)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return Py_BuildValue("(dddd)", src[0], src[1], src[2], src[3]);
|
|
|
|
}
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const bool& value)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
return PyBool_FromLong(value);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, bool& value, const char* name)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
|
|
|
int _val = PyObject_IsTrue(obj);
|
|
|
|
if(_val < 0)
|
|
|
|
return false;
|
|
|
|
value = _val > 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const size_t& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-11-06 04:02:53 +08:00
|
|
|
return PyLong_FromSize_t(value);
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, size_t& value, const char* name)
|
2011-09-07 17:38:22 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2011-09-07 17:38:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
|
|
|
value = (int)PyLong_AsUnsignedLong(obj);
|
2012-03-17 17:22:31 +08:00
|
|
|
return value != (size_t)-1 || !PyErr_Occurred();
|
2011-09-07 17:38:22 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const int& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return PyInt_FromLong(value);
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, int& value, const char* name)
|
2012-03-12 19:39:48 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2012-03-12 19:39:48 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
2013-01-29 01:03:59 +08:00
|
|
|
if(PyInt_Check(obj))
|
|
|
|
value = (int)PyInt_AsLong(obj);
|
|
|
|
else if(PyLong_Check(obj))
|
|
|
|
value = (int)PyLong_AsLong(obj);
|
|
|
|
else
|
|
|
|
return false;
|
2012-03-12 19:39:48 +08:00
|
|
|
return value != -1 || !PyErr_Occurred();
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const uchar& value)
|
2012-03-12 19:39:48 +08:00
|
|
|
{
|
|
|
|
return PyInt_FromLong(value);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, uchar& value, const char* name)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
2012-04-14 05:50:59 +08:00
|
|
|
int ivalue = (int)PyInt_AsLong(obj);
|
|
|
|
value = cv::saturate_cast<uchar>(ivalue);
|
|
|
|
return ivalue != -1 || !PyErr_Occurred();
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const double& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return PyFloat_FromDouble(value);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, double& value, const char* name)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
2013-03-11 22:40:40 +08:00
|
|
|
if(!!PyInt_CheckExact(obj))
|
2010-11-03 01:58:22 +08:00
|
|
|
value = (double)PyInt_AS_LONG(obj);
|
2010-10-29 21:09:49 +08:00
|
|
|
else
|
2010-11-03 01:58:22 +08:00
|
|
|
value = PyFloat_AsDouble(obj);
|
|
|
|
return !PyErr_Occurred();
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const float& value)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
return PyFloat_FromDouble(value);
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2010-11-03 01:58:22 +08:00
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, float& value, const char* name)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
2013-03-11 22:40:40 +08:00
|
|
|
if(!!PyInt_CheckExact(obj))
|
2010-11-03 01:58:22 +08:00
|
|
|
value = (float)PyInt_AS_LONG(obj);
|
|
|
|
else
|
|
|
|
value = (float)PyFloat_AsDouble(obj);
|
|
|
|
return !PyErr_Occurred();
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const int64& value)
|
2011-07-19 00:31:30 +08:00
|
|
|
{
|
2012-11-06 04:02:53 +08:00
|
|
|
return PyLong_FromLongLong(value);
|
2011-07-19 00:31:30 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const String& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return PyString_FromString(value.empty() ? "" : value.c_str());
|
|
|
|
}
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, String& value, const char* name)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
|
|
|
char* str = PyString_AsString(obj);
|
|
|
|
if(!str)
|
|
|
|
return false;
|
2013-03-23 00:37:49 +08:00
|
|
|
value = String(str);
|
2010-11-03 01:58:22 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, Size& sz, const char* name)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
2011-05-16 18:52:20 +08:00
|
|
|
return PyArg_ParseTuple(obj, "ii", &sz.width, &sz.height) > 0;
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Size& sz)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return Py_BuildValue("(ii)", sz.width, sz.height);
|
|
|
|
}
|
|
|
|
|
2017-03-01 18:52:25 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, Rect& r, const char* name)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
2011-05-16 18:52:20 +08:00
|
|
|
return PyArg_ParseTuple(obj, "iiii", &r.x, &r.y, &r.width, &r.height) > 0;
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Rect& r)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return Py_BuildValue("(iiii)", r.x, r.y, r.width, r.height);
|
|
|
|
}
|
|
|
|
|
2015-10-30 17:02:33 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, Range& r, const char* name)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
|
|
|
if(PyObject_Size(obj) == 0)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
r = Range::all();
|
|
|
|
return true;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2011-05-16 18:52:20 +08:00
|
|
|
return PyArg_ParseTuple(obj, "ii", &r.start, &r.end) > 0;
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Range& r)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return Py_BuildValue("(ii)", r.start, r.end);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, Point& p, const char* name)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
2013-03-11 22:40:40 +08:00
|
|
|
if(!!PyComplex_CheckExact(obj))
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
Py_complex c = PyComplex_AsCComplex(obj);
|
|
|
|
p.x = saturate_cast<int>(c.real);
|
|
|
|
p.y = saturate_cast<int>(c.imag);
|
|
|
|
return true;
|
|
|
|
}
|
2011-05-16 18:52:20 +08:00
|
|
|
return PyArg_ParseTuple(obj, "ii", &p.x, &p.y) > 0;
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject* obj, Point2f& p, const char* name)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
2013-03-11 22:40:40 +08:00
|
|
|
if(!!PyComplex_CheckExact(obj))
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
Py_complex c = PyComplex_AsCComplex(obj);
|
|
|
|
p.x = saturate_cast<float>(c.real);
|
|
|
|
p.y = saturate_cast<float>(c.imag);
|
|
|
|
return true;
|
|
|
|
}
|
2011-05-16 18:52:20 +08:00
|
|
|
return PyArg_ParseTuple(obj, "ff", &p.x, &p.y) > 0;
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
|
|
|
|
2013-09-09 17:55:35 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-11 12:12:42 +08:00
|
|
|
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;
|
|
|
|
}
|
2013-09-09 17:55:35 +08:00
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Point& p)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return Py_BuildValue("(ii)", p.x, p.y);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Point2f& p)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return Py_BuildValue("(dd)", p.x, p.y);
|
|
|
|
}
|
|
|
|
|
2015-10-11 12:12:42 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Point3f& p)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("(ddd)", p.x, p.y, p.z);
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:11:47 +08:00
|
|
|
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;
|
|
|
|
}
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
2017-10-11 19:11:47 +08:00
|
|
|
bool pyopencv_to(PyObject* obj, Vec4d& v, const char* name)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2017-10-11 19:11:47 +08:00
|
|
|
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)
|
2010-11-03 01:58:22 +08:00
|
|
|
return true;
|
2011-05-16 18:52:20 +08:00
|
|
|
return PyArg_ParseTuple(obj, "ddd", &v[0], &v[1], &v[2]) > 0;
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
2017-10-11 19:11:47 +08:00
|
|
|
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]);
|
|
|
|
}
|
2010-11-03 01:58:22 +08:00
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Vec3d& v)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return Py_BuildValue("(ddd)", v[0], v[1], v[2]);
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:11:47 +08:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Vec2d& v)
|
2013-03-27 18:58:57 +08:00
|
|
|
{
|
|
|
|
return Py_BuildValue("(dd)", v[0], v[1]);
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:11:47 +08:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Point2d& p)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return Py_BuildValue("(dd)", p.x, p.y);
|
|
|
|
}
|
|
|
|
|
2015-10-11 12:12:42 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Point3d& p)
|
|
|
|
{
|
2017-05-30 22:49:23 +08:00
|
|
|
return Py_BuildValue("(ddd)", p.x, p.y, p.z);
|
2015-10-11 12:12:42 +08:00
|
|
|
}
|
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
template<typename _Tp> struct pyopencvVecConverter
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
static bool to(PyObject* obj, std::vector<_Tp>& value, const ArgInfo info)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
typedef typename DataType<_Tp>::channel_type _Cp;
|
2011-06-11 01:22:33 +08:00
|
|
|
if(!obj || obj == Py_None)
|
2010-11-03 01:58:22 +08:00
|
|
|
return true;
|
|
|
|
if (PyArray_Check(obj))
|
|
|
|
{
|
|
|
|
Mat m;
|
2012-08-08 02:03:17 +08:00
|
|
|
pyopencv_to(obj, m, info);
|
2010-11-03 01:58:22 +08:00
|
|
|
m.copyTo(value);
|
|
|
|
}
|
|
|
|
if (!PySequence_Check(obj))
|
|
|
|
return false;
|
2012-08-08 02:03:17 +08:00
|
|
|
PyObject *seq = PySequence_Fast(obj, info.name);
|
2010-11-03 01:58:22 +08:00
|
|
|
if (seq == NULL)
|
|
|
|
return false;
|
|
|
|
int i, j, n = (int)PySequence_Fast_GET_SIZE(seq);
|
|
|
|
value.resize(n);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2017-02-09 19:52:27 +08:00
|
|
|
int type = traits::Type<_Tp>::value;
|
2010-11-03 01:58:22 +08:00
|
|
|
int depth = CV_MAT_DEPTH(type), channels = CV_MAT_CN(type);
|
|
|
|
PyObject** items = PySequence_Fast_ITEMS(seq);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
PyObject* item = items[i];
|
|
|
|
PyObject* seq_i = 0;
|
|
|
|
PyObject** items_i = &item;
|
|
|
|
_Cp* data = (_Cp*)&value[i];
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
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 )
|
|
|
|
{
|
2011-06-14 06:04:28 +08:00
|
|
|
if( PyArray_Check(item))
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
Mat src;
|
2012-08-08 02:03:17 +08:00
|
|
|
pyopencv_to(item, src, info);
|
2010-11-03 01:58:22 +08:00
|
|
|
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;
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2012-08-08 02:03:17 +08:00
|
|
|
seq_i = PySequence_Fast(item, info.name);
|
2010-11-03 01:58:22 +08:00
|
|
|
if( !seq_i || (int)PySequence_Fast_GET_SIZE(seq_i) != channels )
|
|
|
|
{
|
|
|
|
Py_XDECREF(seq_i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
items_i = PySequence_Fast_ITEMS(seq_i);
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
for( j = 0; j < channels; j++ )
|
|
|
|
{
|
|
|
|
PyObject* item_ij = items_i[j];
|
|
|
|
if( PyInt_Check(item_ij))
|
|
|
|
{
|
2013-01-29 01:03:59 +08:00
|
|
|
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);
|
2010-11-03 01:58:22 +08:00
|
|
|
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;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
static PyObject* from(const std::vector<_Tp>& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
if(value.empty())
|
|
|
|
return PyTuple_New(0);
|
2017-02-09 19:52:27 +08:00
|
|
|
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]);
|
2010-11-03 01:58:22 +08:00
|
|
|
return pyopencv_from(src);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-11 11:58:37 +08:00
|
|
|
template<typename _Tp>
|
2013-04-13 02:35:46 +08:00
|
|
|
bool pyopencv_to(PyObject* obj, std::vector<_Tp>& value, const ArgInfo info)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-08-08 02:03:17 +08:00
|
|
|
return pyopencvVecConverter<_Tp>::to(obj, value, info);
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<typename _Tp>
|
|
|
|
PyObject* pyopencv_from(const std::vector<_Tp>& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return pyopencvVecConverter<_Tp>::from(value);
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
template<typename _Tp> static inline bool pyopencv_to_generic_vec(PyObject* obj, std::vector<_Tp>& value, const ArgInfo info)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2011-06-11 01:22:33 +08:00
|
|
|
if(!obj || obj == Py_None)
|
|
|
|
return true;
|
2010-11-03 01:58:22 +08:00
|
|
|
if (!PySequence_Check(obj))
|
|
|
|
return false;
|
2012-08-08 02:03:17 +08:00
|
|
|
PyObject *seq = PySequence_Fast(obj, info.name);
|
2010-11-03 01:58:22 +08:00
|
|
|
if (seq == NULL)
|
|
|
|
return false;
|
|
|
|
int i, n = (int)PySequence_Fast_GET_SIZE(seq);
|
|
|
|
value.resize(n);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
PyObject** items = PySequence_Fast_ITEMS(seq);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
PyObject* item = items[i];
|
2012-08-08 02:03:17 +08:00
|
|
|
if(!pyopencv_to(item, value[i], info))
|
2010-11-03 01:58:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Py_DECREF(seq);
|
|
|
|
return i == n;
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
template<typename _Tp> static inline PyObject* pyopencv_from_generic_vec(const std::vector<_Tp>& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
int i, n = (int)value.size();
|
2011-06-11 01:22:33 +08:00
|
|
|
PyObject* seq = PyList_New(n);
|
2010-11-03 01:58:22 +08:00
|
|
|
for( i = 0; i < n; i++ )
|
2012-06-08 01:21:29 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
PyObject* item = pyopencv_from(value[i]);
|
|
|
|
if(!item)
|
|
|
|
break;
|
2011-06-11 01:22:33 +08:00
|
|
|
PyList_SET_ITEM(seq, i, item);
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
|
|
|
if( i < n )
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
Py_DECREF(seq);
|
2010-10-29 21:09:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2010-11-03 01:58:22 +08:00
|
|
|
return seq;
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:26:27 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2010-11-03 01:58:22 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
template<typename _Tp> struct pyopencvVecConverter<std::vector<_Tp> >
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2013-07-11 11:58:37 +08:00
|
|
|
static bool to(PyObject* obj, std::vector<std::vector<_Tp> >& value, const ArgInfo info)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2013-07-11 11:58:37 +08:00
|
|
|
return pyopencv_to_generic_vec(obj, value, info);
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
static PyObject* from(const std::vector<std::vector<_Tp> >& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return pyopencv_from_generic_vec(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct pyopencvVecConverter<Mat>
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
static bool to(PyObject* obj, std::vector<Mat>& value, const ArgInfo info)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-08-08 02:03:17 +08:00
|
|
|
return pyopencv_to_generic_vec(obj, value, info);
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
static PyObject* from(const std::vector<Mat>& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return pyopencv_from_generic_vec(value);
|
|
|
|
}
|
|
|
|
};
|
2010-10-29 21:09:49 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
template<> struct pyopencvVecConverter<KeyPoint>
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
static bool to(PyObject* obj, std::vector<KeyPoint>& value, const ArgInfo info)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2012-08-08 02:03:17 +08:00
|
|
|
return pyopencv_to_generic_vec(obj, value, info);
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
static PyObject* from(const std::vector<KeyPoint>& value)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
|
|
|
return pyopencv_from_generic_vec(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-07 17:38:22 +08:00
|
|
|
template<> struct pyopencvVecConverter<DMatch>
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
static bool to(PyObject* obj, std::vector<DMatch>& value, const ArgInfo info)
|
2011-09-07 17:38:22 +08:00
|
|
|
{
|
2012-08-08 02:03:17 +08:00
|
|
|
return pyopencv_to_generic_vec(obj, value, info);
|
2011-09-07 17:38:22 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
static PyObject* from(const std::vector<DMatch>& value)
|
2011-09-07 17:38:22 +08:00
|
|
|
{
|
|
|
|
return pyopencv_from_generic_vec(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
template<> struct pyopencvVecConverter<String>
|
2012-04-30 22:33:52 +08:00
|
|
|
{
|
2013-03-23 00:37:49 +08:00
|
|
|
static bool to(PyObject* obj, std::vector<String>& value, const ArgInfo info)
|
2012-04-30 22:33:52 +08:00
|
|
|
{
|
2012-08-08 02:03:17 +08:00
|
|
|
return pyopencv_to_generic_vec(obj, value, info);
|
2012-04-30 22:33:52 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
static PyObject* from(const std::vector<String>& value)
|
2012-04-30 22:33:52 +08:00
|
|
|
{
|
|
|
|
return pyopencv_from_generic_vec(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject *obj, TermCriteria& dst, const char *name)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
if(!obj)
|
|
|
|
return true;
|
|
|
|
return PyArg_ParseTuple(obj, "iid", &dst.type, &dst.maxCount, &dst.epsilon) > 0;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const TermCriteria& src)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
return Py_BuildValue("(iid)", src.type, src.maxCount, src.epsilon);
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
bool pyopencv_to(PyObject *obj, RotatedRect& dst, const char *name)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2012-06-21 01:57:26 +08:00
|
|
|
(void)name;
|
2010-11-03 01:58:22 +08:00
|
|
|
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;
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const RotatedRect& src)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
return Py_BuildValue("((ff)(ff)f)", src.center.x, src.center.y, src.size.width, src.size.height, src.angle);
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:35:46 +08:00
|
|
|
template<>
|
|
|
|
PyObject* pyopencv_from(const Moments& m)
|
2010-10-29 21:09:49 +08:00
|
|
|
{
|
2010-11-03 01:58:22 +08:00
|
|
|
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,
|
2013-04-02 16:14:43 +08:00
|
|
|
"nu30", m.nu30, "nu21", m.nu21, "nu12", m.nu12, "nu03", m.nu03);
|
2010-10-29 21:09:49 +08:00
|
|
|
}
|
|
|
|
|
2016-08-07 00:46:17 +08:00
|
|
|
#include "pyopencv_custom_headers.h"
|
2011-06-14 06:25:21 +08:00
|
|
|
|
|
|
|
static void OnMouse(int event, int x, int y, int flags, void* param)
|
|
|
|
{
|
|
|
|
PyGILState_STATE gstate;
|
|
|
|
gstate = PyGILState_Ensure();
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-06-14 06:25:21 +08:00
|
|
|
PyObject *o = (PyObject*)param;
|
|
|
|
PyObject *args = Py_BuildValue("iiiiO", event, x, y, flags, PyTuple_GetItem(o, 1));
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-06-14 06:25:21 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-07 07:57:10 +08:00
|
|
|
#ifdef HAVE_OPENCV_HIGHGUI
|
2012-06-21 01:57:26 +08:00
|
|
|
static PyObject *pycvSetMouseCallback(PyObject*, PyObject *args, PyObject *kw)
|
2011-06-14 06:25:21 +08:00
|
|
|
{
|
|
|
|
const char *keywords[] = { "window_name", "on_mouse", "param", NULL };
|
|
|
|
char* name;
|
|
|
|
PyObject *on_mouse;
|
|
|
|
PyObject *param = NULL;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-06-14 06:25:21 +08:00
|
|
|
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;
|
|
|
|
}
|
2013-04-12 21:05:23 +08:00
|
|
|
ERRWRAP2(setMouseCallback(name, OnMouse, Py_BuildValue("OO", on_mouse, param)));
|
2011-06-14 06:25:21 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2015-04-07 07:57:10 +08:00
|
|
|
#endif
|
2011-06-14 06:25:21 +08:00
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void OnChange(int pos, void *param)
|
2011-06-14 06:25:21 +08:00
|
|
|
{
|
|
|
|
PyGILState_STATE gstate;
|
|
|
|
gstate = PyGILState_Ensure();
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-06-14 06:25:21 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-07 07:57:10 +08:00
|
|
|
#ifdef HAVE_OPENCV_HIGHGUI
|
2012-06-21 01:57:26 +08:00
|
|
|
static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args)
|
2011-06-14 06:25:21 +08:00
|
|
|
{
|
|
|
|
PyObject *on_change;
|
|
|
|
char* trackbar_name;
|
|
|
|
char* window_name;
|
|
|
|
int *value = new int;
|
|
|
|
int count;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-06-14 06:25:21 +08:00
|
|
|
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;
|
|
|
|
}
|
2013-04-12 21:05:23 +08:00
|
|
|
ERRWRAP2(createTrackbar(trackbar_name, window_name, value, count, OnChange, Py_BuildValue("OO", on_change, Py_None)));
|
2011-06-14 06:25:21 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2016-06-21 05:24:15 +08:00
|
|
|
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;
|
2017-02-21 01:02:35 +08:00
|
|
|
int initial_button_state = 0;
|
2016-06-21 05:24:15 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-21 01:02:35 +08:00
|
|
|
ERRWRAP2(createButton(button_name, OnButtonChange, Py_BuildValue("OO", on_change, userdata), button_type, initial_button_state != 0));
|
2016-06-21 05:24:15 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-14 06:25:21 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-04-12 21:39:16 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 04:03:34 +08:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
#define MKTYPE2(NAME) pyopencv_##NAME##_specials(); if (!to_ok(&pyopencv_##NAME##_Type)) return NULL;
|
|
|
|
#else
|
2011-05-04 00:00:31 +08:00
|
|
|
#define MKTYPE2(NAME) pyopencv_##NAME##_specials(); if (!to_ok(&pyopencv_##NAME##_Type)) return
|
2013-06-13 04:03:34 +08:00
|
|
|
#endif
|
2011-05-04 00:00:31 +08:00
|
|
|
|
2012-06-21 01:57:26 +08:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
|
|
|
#endif
|
|
|
|
|
2011-05-04 00:00:31 +08:00
|
|
|
#include "pyopencv_generated_types.h"
|
|
|
|
#include "pyopencv_generated_funcs.h"
|
|
|
|
|
2014-08-19 18:57:47 +08:00
|
|
|
static PyMethodDef special_methods[] = {
|
2015-04-07 07:57:10 +08:00
|
|
|
#ifdef HAVE_OPENCV_HIGHGUI
|
2011-06-14 06:25:21 +08:00
|
|
|
{"createTrackbar", pycvCreateTrackbar, METH_VARARGS, "createTrackbar(trackbarName, windowName, value, count, onChange) -> None"},
|
2016-06-21 05:24:15 +08:00
|
|
|
{"createButton", (PyCFunction)pycvCreateButton, METH_VARARGS | METH_KEYWORDS, "createButton(buttonName, onChange [, userData, buttonType, initialButtonState]) -> None"},
|
2013-06-11 05:29:36 +08:00
|
|
|
{"setMouseCallback", (PyCFunction)pycvSetMouseCallback, METH_VARARGS | METH_KEYWORDS, "setMouseCallback(windowName, onMouse [, param]) -> None"},
|
2015-04-07 07:57:10 +08:00
|
|
|
#endif
|
2011-05-04 00:00:31 +08:00
|
|
|
{NULL, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* Module init */
|
|
|
|
|
2014-08-19 17:35:46 +08:00
|
|
|
struct ConstDef
|
|
|
|
{
|
|
|
|
const char * name;
|
|
|
|
long val;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void init_submodule(PyObject * root, const char * name, PyMethodDef * methods, ConstDef * consts)
|
2014-08-16 00:44:32 +08:00
|
|
|
{
|
2014-08-18 20:52:01 +08:00
|
|
|
// traverse and create nested submodules
|
2014-08-16 00:44:32 +08:00
|
|
|
std::string s = name;
|
2014-08-18 20:52:01 +08:00
|
|
|
size_t i = s.find('.');
|
|
|
|
while (i < s.length() && i != std::string::npos)
|
2014-08-16 00:44:32 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2015-03-02 12:04:14 +08:00
|
|
|
|
|
|
|
if (short_name != "")
|
|
|
|
root = submod;
|
2014-08-16 00:44:32 +08:00
|
|
|
}
|
|
|
|
|
2014-08-18 20:52:01 +08:00
|
|
|
// populate module's dict
|
2014-08-16 00:44:32 +08:00
|
|
|
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);
|
|
|
|
}
|
2014-08-19 17:35:46 +08:00
|
|
|
for (ConstDef * c = consts; c->name != NULL; ++c)
|
|
|
|
{
|
|
|
|
PyDict_SetItemString(d, c->name, PyInt_FromLong(c->val));
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:44:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "pyopencv_generated_ns_reg.h"
|
|
|
|
|
2011-05-04 00:00:31 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-06-13 04:03:34 +08:00
|
|
|
|
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
extern "C" CV_EXPORTS PyObject* PyInit_cv2();
|
|
|
|
static struct PyModuleDef cv2_moduledef =
|
|
|
|
{
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
MODULESTR,
|
|
|
|
"Python wrapper for OpenCV.",
|
|
|
|
-1, /* size of per-interpreter state of the module,
|
|
|
|
or -1 if the module keeps state in global variables. */
|
2014-08-19 18:57:47 +08:00
|
|
|
special_methods
|
2013-06-13 04:03:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
PyObject* PyInit_cv2()
|
|
|
|
#else
|
2013-04-18 14:30:15 +08:00
|
|
|
extern "C" CV_EXPORTS void initcv2();
|
2011-05-04 00:00:31 +08:00
|
|
|
|
|
|
|
void initcv2()
|
2013-06-13 04:03:34 +08:00
|
|
|
#endif
|
2011-05-04 00:00:31 +08:00
|
|
|
{
|
2013-04-12 21:05:23 +08:00
|
|
|
import_array();
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-05-04 00:00:31 +08:00
|
|
|
#include "pyopencv_generated_type_reg.h"
|
|
|
|
|
2013-06-13 04:03:34 +08:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
PyObject* m = PyModule_Create(&cv2_moduledef);
|
|
|
|
#else
|
2014-08-19 18:57:47 +08:00
|
|
|
PyObject* m = Py_InitModule(MODULESTR, special_methods);
|
2013-06-13 04:03:34 +08:00
|
|
|
#endif
|
2014-08-16 00:44:32 +08:00
|
|
|
init_submodules(m); // from "pyopencv_generated_ns_reg.h"
|
|
|
|
|
2011-05-04 00:00:31 +08:00
|
|
|
PyObject* d = PyModule_GetDict(m);
|
|
|
|
|
2012-04-30 22:33:52 +08:00
|
|
|
PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION));
|
2011-05-04 00:00:31 +08:00
|
|
|
|
|
|
|
opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, NULL);
|
|
|
|
PyDict_SetItemString(d, "error", opencv_error);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2016-02-08 00:04:39 +08:00
|
|
|
//Registering UMatWrapper python class in cv2 module:
|
|
|
|
if (PyType_Ready(&cv2_UMatWrapperType) < 0)
|
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2017-09-05 13:38:17 +08:00
|
|
|
|
2016-02-08 00:04:39 +08:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
2017-09-05 13:38:17 +08:00
|
|
|
#define PUBLISH_OBJECT(name, type) Py_INCREF(&type);\
|
|
|
|
PyModule_AddObject(m, name, (PyObject *)&type);
|
2016-02-08 00:04:39 +08:00
|
|
|
#else
|
2017-09-05 13:38:17 +08:00
|
|
|
// 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);
|
2016-02-08 00:04:39 +08:00
|
|
|
#endif
|
2017-09-05 13:38:17 +08:00
|
|
|
|
|
|
|
PUBLISH_OBJECT("UMat", cv2_UMatWrapperType);
|
|
|
|
|
|
|
|
#include "pyopencv_generated_type_publish.h"
|
2016-02-08 00:04:39 +08:00
|
|
|
|
2011-05-04 00:00:31 +08:00
|
|
|
#define PUBLISH(I) PyDict_SetItemString(d, #I, PyInt_FromLong(I))
|
2013-04-12 21:05:23 +08:00
|
|
|
//#define PUBLISHU(I) PyDict_SetItemString(d, #I, PyLong_FromUnsignedLong(I))
|
2011-05-04 00:00:31 +08:00
|
|
|
#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);
|
2011-06-06 22:18:25 +08:00
|
|
|
|
2013-06-13 04:03:34 +08:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
return m;
|
|
|
|
#endif
|
2013-04-12 21:05:23 +08:00
|
|
|
}
|