example to use numpy object: http://blog.debao.me/2013/04/my-first-c-extension-to-numpy/ write a c extension ot Numpy: http://folk.uio.no/hpl/scripting/doc/python/NumPy/Numeric/numpy-13.html
| 8 | // example to use numpy object: http://blog.debao.me/2013/04/my-first-c-extension-to-numpy/ |
| 9 | // write a c extension ot Numpy: http://folk.uio.no/hpl/scripting/doc/python/NumPy/Numeric/numpy-13.html |
| 10 | static PyObject * |
| 11 | densecrf_wrapper(PyObject *self, PyObject *args) |
| 12 | { |
| 13 | PyObject *I=NULL, *fP=NULL, *param=NULL; |
| 14 | PyArrayObject *arr_I=NULL, *arr_fP=NULL; |
| 15 | |
| 16 | if (!PyArg_ParseTuple(args, "OOO", &I, &fP, ¶m)) return NULL; |
| 17 | |
| 18 | arr_I = (PyArrayObject*)PyArray_FROM_OTF(I, NPY_UINT8, NPY_IN_ARRAY); |
| 19 | if (arr_I == NULL) return NULL; |
| 20 | |
| 21 | arr_fP = (PyArrayObject*)PyArray_FROM_OTF(fP, NPY_FLOAT32, NPY_IN_ARRAY); |
| 22 | if (arr_fP == NULL) return NULL; |
| 23 | |
| 24 | |
| 25 | /*vv* code that makes use of arguments *vv*/ |
| 26 | |
| 27 | int nd_I = PyArray_NDIM(arr_I); //number of dimensions |
| 28 | npy_intp * shape = PyArray_DIMS(arr_I); // npy_intp array of length nd showing length in each dim. |
| 29 | |
| 30 | |
| 31 | int nd_P = PyArray_NDIM(arr_fP); |
| 32 | npy_intp * shape_fP = PyArray_DIMS(arr_fP); |
| 33 | |
| 34 | CRFParam crf_param; |
| 35 | crf_param.w1 = PyFloat_AsDouble(PyTuple_GET_ITEM(param, 0)); |
| 36 | crf_param.alpha = PyFloat_AsDouble(PyTuple_GET_ITEM(param, 1)); |
| 37 | crf_param.beta = PyFloat_AsDouble(PyTuple_GET_ITEM(param, 2)); |
| 38 | crf_param.w2 = PyFloat_AsDouble(PyTuple_GET_ITEM(param, 3)); |
| 39 | crf_param.gamma = PyFloat_AsDouble(PyTuple_GET_ITEM(param, 4)); |
| 40 | crf_param.iter = int(PyFloat_AsDouble(PyTuple_GET_ITEM(param, 5))); |
| 41 | |
| 42 | VectorXs map = dense_crf_inference((const unsigned char *)arr_I->data, (const float *)arr_fP->data, |
| 43 | shape_fP[0], shape_fP[1], shape_fP[2], crf_param); |
| 44 | |
| 45 | npy_intp outshape[2]; |
| 46 | outshape[0]=shape_fP[0]; |
| 47 | outshape[1]=shape_fP[1]; |
| 48 | PyArrayObject * labels = (PyArrayObject*) PyArray_SimpleNew(2, outshape, NPY_INT8); |
| 49 | for (int x=0;x<outshape[0]*outshape[1];x++) |
| 50 | { |
| 51 | *(labels->data + x*labels->strides[1]) = map[x]; |
| 52 | } |
| 53 | |
| 54 | Py_DECREF(arr_I); |
| 55 | Py_DECREF(arr_fP); |
| 56 | Py_INCREF(labels); |
| 57 | return PyArray_Return(labels); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static PyMethodDef Methods[] = { |
nothing calls this directly
no test coverage detected