static PyObject *what_data(PyObject *o);
static PyObject *FROM_CvMat(CvMat *r);
static PyObject *FROM_ROCvMatPTR(ROCvMat *r);
+static PyObject *shareDataND(PyObject *donor, CvMatND *pdonor, CvMatND *precipient);
#define FROM_double(r) PyFloat_FromDouble(r)
#define FROM_float(r) PyFloat_FromDouble(r)
static PyGetSetDef iplimage_getseters[] = {
{(char*)"nChannels", (getter)iplimage_getnChannels, (setter)NULL, (char*)"nChannels", NULL},
+ {(char*)"channels", (getter)iplimage_getnChannels, (setter)NULL, (char*)"nChannels", NULL},
{(char*)"width", (getter)iplimage_getwidth, (setter)NULL, (char*)"width", NULL},
{(char*)"height", (getter)iplimage_getheight, (setter)NULL, (char*)"height", NULL},
{(char*)"depth", (getter)iplimage_getdepth, (setter)NULL, (char*)"depth", NULL},
return PyInt_FromLong(cva->a->cols);
}
+static PyObject *cvmat_getchannels(cvmat_t *cva)
+{
+ return PyInt_FromLong(CV_MAT_CN(cva->a->type));
+}
+
#if PYTHON_USE_NUMPY
#include "numpy/ndarrayobject.h"
{(char*)"step", (getter)cvmat_getstep, (setter)NULL, (char*)"step", NULL},
{(char*)"rows", (getter)cvmat_getrows, (setter)NULL, (char*)"rows", NULL},
{(char*)"cols", (getter)cvmat_getcols, (setter)NULL, (char*)"cols", NULL},
+ {(char*)"channels",(getter)cvmat_getchannels, (setter)NULL, (char*)"channels", NULL},
{(char*)"width", (getter)cvmat_getcols, (setter)NULL, (char*)"width", NULL},
{(char*)"height", (getter)cvmat_getrows, (setter)NULL, (char*)"height", NULL},
#if PYTHON_USE_NUMPY
{NULL, NULL}
};
+static PyObject *cvmatnd_getchannels(cvmatnd_t *cva)
+{
+ return PyInt_FromLong(CV_MAT_CN(cva->a->type));
+}
+
static PyGetSetDef cvmatnd_getseters[] = {
#if PYTHON_USE_NUMPY
{(char*)"__array_struct__", (getter)cvmatnd_array_struct, (setter)NULL, (char*)"__array_struct__", NULL},
#endif
+ {(char*)"channels",(getter)cvmatnd_getchannels, (setter)NULL, (char*)"channels", NULL},
{NULL} /* Sentinel */
};
int new_cn = 0;
PyObject *new_dims = NULL;
- if (!PyArg_ParseTuple(args, "O|iO", &o, &new_cn, &new_dims))
+ if (!PyArg_ParseTuple(args, "OiO", &o, &new_cn, &new_dims))
return NULL;
CvMatND *cva;
return NULL;
}
-#if 0
- if ((dims.count + 1) <= 2) {
- CvMat *m = cvCreateMatHeader(100, 100, 1); // these args do not matter, because overwritten
- if (new_dims != NULL) {
- printf("newcn=%d newdims=%d newSizes=%p\n", new_cn, dims.count + 1, dims.i);
- ERRWRAP(cvReshapeND(cva, m, new_cn, dims.count + 1, dims.i));
- } else {
- ERRWRAP(cvReshapeND(cva, m, new_cn, 0, NULL));
- }
+ if (new_cn == 0)
+ new_cn = CV_MAT_CN(cvGetElemType(cva));
- cvmat_t *om = PyObject_NEW(cvmat_t, &cvmat_Type);
- om->a = m;
- om->data = what_data(o);
- Py_INCREF(om->data);
- om->offset = 0;
- return (PyObject*)om;
- } else {
- int dummy[1] = { 1 };
- CvMatND *m = cvCreateMatNDHeader(1, dummy, 1); // these args do not matter, because overwritten
- if (new_dims != NULL) {
- printf("newcn=%d newdims=%d newSizes=%p\n", new_cn, dims.count + 1, dims.i);
- ERRWRAP(cvReshapeND(cva, m, new_cn, dims.count + 1, dims.i));
- } else {
- ERRWRAP(cvReshapeND(cva, m, new_cn, 0, NULL));
- }
+ int i;
+ int count = CV_MAT_CN(cvGetElemType(cva));
+ for (i = 0; i < cva->dims; i++)
+ count *= cva->dim[i].size;
- cvmatnd_t *om = PyObject_NEW(cvmatnd_t, &cvmatnd_Type);
- om->a = m;
- om->data = what_data(o);
- Py_INCREF(om->data);
- om->offset = 0;
- return (PyObject*)om;
- }
-#else
- {
- int size[] = { 2, 2, 2 };
- CvMatND* mat = cvCreateMatND(3, size, CV_32F);
- CvMat row_header;
- CvArr *row;
- row = cvReshapeND(mat, &row_header, 0, 1, 0);
+ int newcount = new_cn;
+ for (i = 0; i < dims.count; i++)
+ newcount *= dims.i[i];
+
+ if (count != newcount) {
+ PyErr_SetString(PyExc_TypeError, "Total number of elements must be unchanged");
+ return NULL;
}
- Py_RETURN_NONE;
-#endif
+
+ CvMatND *pn = cvCreateMatNDHeader(dims.count, dims.i, CV_MAKETYPE(CV_MAT_TYPE(cva->type), new_cn));
+ return shareDataND(o, cva, pn);
}
static void OnMouse(int event, int x, int y, int flags, void* param)
return recipient;
}
+static PyObject *shareDataND(PyObject *donor, CvMatND *pdonor, CvMatND *precipient)
+{
+ PyObject *recipient = (PyObject*)PyObject_NEW(cvmatnd_t, &cvmatnd_Type);
+ ((cvmatnd_t*)recipient)->a = precipient;
+ ((cvmatnd_t*)recipient)->offset = 0;
+
+ PyObject *arr_data;
+ arr_data = ((cvmatnd_t*)donor)->data;
+ ((cvmatnd_t*)recipient)->data = arr_data;
+ Py_INCREF(arr_data);
+ return recipient;
+}
+
static PyObject *pycvGetHuMoments(PyObject *self, PyObject *args)
{
CvMoments* moments;
self.assert_((nc * nr * nd) == elems)
# Now test ReshapeMatND
- mat = cv.CreateMatND([24], cv.CV_32F)
+ mat = cv.CreateMatND([24], cv.CV_32FC1)
cv.Set(mat, 1.0)
- self.assertEqual(cv.GetDims(cv.ReshapeMatND(mat, 0, [])), (24, 1))
- self.assertEqual(cv.GetDims(cv.ReshapeMatND(mat, 0, [1])), (6, 4))
+ self.assertEqual(cv.GetDims(cv.ReshapeMatND(mat, 0, [24, 1])), (24, 1))
+ self.assertEqual(cv.GetDims(cv.ReshapeMatND(mat, 0, [6, 4])), (6, 4))
+ self.assertEqual(cv.GetDims(cv.ReshapeMatND(mat, 24, [1])), (1,))
+ self.assertRaises(TypeError, lambda: cv.ReshapeMatND(mat, 12, [1]))
def test_Save(self):
for o in [ cv.CreateImage((128,128), cv.IPL_DEPTH_8U, 1), cv.CreateMat(16, 16, cv.CV_32FC1) ]:
cv.AddS(ones, 7, r)
self.assert_(numpy.alltrue(r == (8 * ones)))
+ # create arrays, use them in OpenCV and replace the the array
+ # looking for leaks
+ def randdim():
+ return [random.randrange(1,6) for i in range(random.randrange(1, 6))]
+ as = [numpy.ones(randdim()).astype(numpy.uint8) for i in range(10)]
+ cs = [cv.fromarray(a, True) for a in as]
+ for i in range(1000):
+ as[random.randrange(10)] = numpy.ones(randdim()).astype(numpy.uint8)
+ cs[random.randrange(10)] = cv.fromarray(as[random.randrange(10)], True)
+ for j in range(10):
+ self.assert_(all([c == chr(1) for c in cs[j].tostring()]))
+
else:
print "SKIPPING test_numpy - numpy support not built"