-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdensecrf3d.cpp
More file actions
executable file
·163 lines (145 loc) · 5.94 KB
/
Copy pathdensecrf3d.cpp
File metadata and controls
executable file
·163 lines (145 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <Python.h>
#include "numpy/arrayobject.h"
#include "densecrf.h"
#include "densecrf3d_util.h"
#include <iostream>
//#define DEBUG
using namespace std;
// 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
static PyObject *
dense_crf_wrapper(PyObject *self, PyObject *args)
{
PyObject *I=NULL, *P=NULL, *param=NULL, *BilateralModsStds=NULL;
PyArrayObject *arr_I=NULL, *arr_P=NULL;
if (!PyArg_ParseTuple(args, "OOO", &I, &P, ¶m)) return NULL;
arr_I = (PyArrayObject*)PyArray_FROM_OTF(I, NPY_UINT8, NPY_IN_ARRAY);
if (arr_I == NULL) return NULL;
arr_P = (PyArrayObject*)PyArray_FROM_OTF(P, NPY_FLOAT32, NPY_IN_ARRAY);
if (arr_P == NULL) return NULL;
/*vv* code that makes use of arguments *vv*/
int nd_I = PyArray_NDIM(arr_I); //number of dimensions
int nd_P = PyArray_NDIM(arr_P);
if(nd_I != nd_P)
{
cerr<<"data dimension "<<nd_I<<" probability dimension "<<nd_P<<" do not match"<<endl;
return NULL;
}
npy_intp * shape_I = PyArray_DIMS(arr_I); // npy_intp array of length nd showing length in each dim.
npy_intp * shape_P = PyArray_DIMS(arr_P);
#ifdef DEBUG
cout<<"input data shape: ";
for(int i=0; i<nd_I; i++)
{
cout<<shape_I[i]<<" ";
}
cout<<std::endl;
cout<<"input probability shape: ";
for(int i=0; i<nd_P; i++)
{
cout<<shape_P[i]<<" ";
}
cout<<std::endl;
#endif
for(int i=0; i<nd_I-1; i++)
{
if(shape_I[i] !=shape_P[i] || shape_I[i]!=shape_P[i])
{
cerr<<"input shape does not match"<<endl;
return NULL;
}
}
double MaxIterations = PyFloat_AsDouble(PyDict_GetItemString(param, "MaxIterations"));
double PosRStd = PyFloat_AsDouble(PyDict_GetItemString(param, "PosRStd"));
double PosCStd = PyFloat_AsDouble(PyDict_GetItemString(param, "PosCStd"));
double PosZStd = PyFloat_AsDouble(PyDict_GetItemString(param, "PosZStd"));
double PosW = PyFloat_AsDouble(PyDict_GetItemString(param, "PosW"));
double BilateralRStd = PyFloat_AsDouble(PyDict_GetItemString(param, "BilateralRStd"));
double BilateralCStd = PyFloat_AsDouble(PyDict_GetItemString(param, "BilateralCStd"));
double BilateralZStd = PyFloat_AsDouble(PyDict_GetItemString(param, "BilateralZStd"));
double ModalityNum = PyFloat_AsDouble(PyDict_GetItemString(param, "ModalityNum"));
double BilateralW = PyFloat_AsDouble(PyDict_GetItemString(param, "BilateralW"));
BilateralModsStds = PyDict_GetItemString(param, "BilateralModsStds");
cout<<"MaxIterations :"<<MaxIterations<<endl;
cout<<"PosRStd :"<<PosRStd <<endl;
cout<<"PosCStd :"<<PosCStd <<endl;
cout<<"PosZStd :"<<PosZStd <<endl;
cout<<"PosW :"<<PosW <<endl;
cout<<"BilateralRStd :"<<BilateralRStd<<endl;
cout<<"BilateralCStd :"<<BilateralCStd<<endl;
cout<<"BilateralZStd :"<<BilateralZStd<<endl;
cout<<"ModalityNum :"<<ModalityNum <<endl;
cout<<"BilateralW :"<<BilateralW <<endl;
if(ModalityNum != shape_I[3])
{
cerr<<"input data channel "<<shape_I[3]<<" and modality number "<<ModalityNum<<" do not match"<<endl;
return NULL;
}
int modStds_size = PyTuple_GET_SIZE(BilateralModsStds);
if(ModalityNum != modStds_size)
{
cerr<<"input data channel "<<shape_I[3]<<" and BilateralModsStds size "<<modStds_size<<" do not match"<<endl;
return NULL;
}
cout<<"BilateralModsStds:(";
std::vector<double> mod_std_vector;
mod_std_vector.resize(modStds_size);
for(int i = 0; i< modStds_size; i++)
{
mod_std_vector[i] = PyFloat_AsDouble(PyTuple_GET_ITEM(BilateralModsStds, i));
cout<< mod_std_vector[i];
if(i< modStds_size - 1) cout<<", ";
}
cout<<")"<<std::endl;
//get unary potential array size num_class x num_voxel
int num_voxel = shape_P[0] * shape_P[1] * shape_P[2];
int num_class = shape_P[3];
MatrixXf matrixUnaries( num_class, num_voxel );
for(int z = 0; z< shape_P[0]; z++)
{
for(int y = 0; y< shape_P[1]; y++)
{
for(int x = 0; x< shape_P[2]; x++)
{
int voxel_id = z*shape_P[1]*shape_P[2] + y*shape_P[2] + x;
for(int c = 0; c< shape_P[3]; c++)
{
float prob = *(float *)(arr_P->data + z*arr_P->strides[0] + y*arr_P->strides[1] +
x*arr_P->strides[2] + c*arr_P->strides[3]);
float energy = -log(prob);
matrixUnaries(c, voxel_id) = energy;
}
}
}
}
DenseCRF3D crf3d(shape_I[0], shape_I[1], shape_I[2], num_class);
crf3d.setUnaryEnergy( matrixUnaries );
crf3d.addPairwiseGaussian( PosRStd , PosCStd, PosZStd, new PottsCompatibility(PosW));
addPairwiseBilateral(crf3d,
ModalityNum,
BilateralRStd,
BilateralCStd,
BilateralZStd,
mod_std_vector,
BilateralW,
(unsigned char *)arr_I->data);
MatrixXf probMapsMatrix = crf3d.inference(MaxIterations);
VectorXs segmentationVector = crf3d.currentMap(probMapsMatrix);
npy_intp outshape[3];
outshape[0] = shape_P[0];
outshape[1] = shape_P[1];
outshape[2] = shape_P[2];
PyArrayObject * labels = (PyArrayObject*) PyArray_SimpleNew(3, outshape, NPY_INT8);
for(int i = 0; i < num_voxel; i++)
{
*(labels->data + i) = segmentationVector(i);
}
Py_DECREF(arr_I);
Py_DECREF(arr_P);
Py_INCREF(labels);
return PyArray_Return(labels);
}
static PyMethodDef Methods[] = {
{"densecrf3d", dense_crf_wrapper, METH_VARARGS, "3D dense CRF"},
{NULL, NULL, 0, NULL}
};