MCPcopy Create free account
hub / github.com/HiLab-git/SimpleCRF / maxflow2d_wrapper

Function maxflow2d_wrapper

maxflow_python/maxflow.cpp:10–59  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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
10static PyObject *
11maxflow2d_wrapper(PyObject *self, PyObject *args)
12{
13 PyObject *I=NULL, *P=NULL, *param=NULL;
14 PyArrayObject *arr_I=NULL, *arr_P=NULL;
15 if (!PyArg_ParseTuple(args, "OOO", &I, &P, &param)) return NULL;
16
17 arr_I = (PyArrayObject*)PyArray_FROM_OTF(I, NPY_FLOAT32, NPY_IN_ARRAY);
18 arr_P = (PyArrayObject*)PyArray_FROM_OTF(P, NPY_FLOAT32, NPY_IN_ARRAY);
19 if (arr_I == NULL || arr_P == NULL) return NULL;
20 float lambda = PyFloat_AsDouble(PyTuple_GET_ITEM(param, 0));
21 float sigma = PyFloat_AsDouble(PyTuple_GET_ITEM(param, 1));
22
23 /*vv* code that makes use of arguments *vv*/
24 int dimI = PyArray_NDIM(arr_I); // number of dimensions
25 int dimP = PyArray_NDIM(arr_P);
26 npy_intp * shapeI = PyArray_DIMS(arr_I); // npy_intp array of length nd showing length in each dim
27 npy_intp * shapeP = PyArray_DIMS(arr_P);
28
29 if(dimI > 3){
30 cout << "the input dimension can only be 2 or 3"<<endl;
31 return NULL;
32 }
33 if(dimP != 3){
34 cout << "dimension of probabilily map should be 3"<<endl;
35 return NULL;
36 }
37 if(shapeI[0] != shapeP[0] || shapeI[1] != shapeP[1]){
38 cout << "image and probability map have different sizes"<<endl;
39 return NULL;
40 }
41 if(shapeP[2] != 2){
42 cout << "probabilily map should have two channels"<<endl;
43 return NULL;
44 }
45
46 int chns = 1;
47 if(dimI == 3) chns = shapeI[2];
48 npy_intp outshape[2];
49 outshape[0]=shapeI[0];
50 outshape[1]=shapeI[1];
51 PyArrayObject * arr_L = (PyArrayObject*) PyArray_SimpleNew(2, outshape, NPY_INT8);
52 maxflow_inference((unsigned char *) arr_L->data, (const float *) arr_I->data, (const float *) arr_P->data, NULL,
53 shapeI[0], shapeI[1], chns, 2, lambda, sigma);
54
55 Py_DECREF(arr_I);
56 Py_DECREF(arr_P);
57 Py_INCREF(arr_L);
58 return PyArray_Return(arr_L);
59}
60
61static PyObject *
62interactive_maxflow2d_wrapper(PyObject *self, PyObject *args)

Callers

nothing calls this directly

Calls 1

maxflow_inferenceFunction · 0.85

Tested by

no test coverage detected