-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGraphCut3dConstr.cpp
202 lines (185 loc) · 7.75 KB
/
GraphCut3dConstr.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "mex.h"
#include "GCoptimization.h"
#include "GraphCut.h"
#include <stdlib.h>
#include <math.h>
/* Defines */
// define A64BITS for compiling on a 64 bits machine...
/*
* Matlab wrapper for Weksler graph cut implementation
*
* usage:
* [gch] = GraphCut3dConstr(R, C, Z, num_labels, DataCost, SmoothnessCost, [Contrast])
*
* Note that data types are crucials!
*
* Inputs:
* R, C, Z - size of 3D grid.
* num_labels - number of labels.
* DataCost - of type float, array size [width*height*depth*#labels], the data term for pixel
* x,y,z recieving label l is stroed at [(x+y*width+z*width*depth)*#labels + l]
* SmoothnessCost - of type float, array size [#labels.^2] the cost between l1 and l2 is
* stored at [l1+l2*#labels] = Vpq(lp,lq)
* Contrast - of type float, array size[width*height*depth], the weight Wpq will be determined by the contrast:
* Wpq = exp(-|C(p)-C(q)|)
*
* Outputs:
* gch - of type int32, graph cut handle - do NOT mess with it!
*/
void mexFunction(
int nlhs, /* number of expected outputs */
mxArray *plhs[], /* mxArray output pointer array */
int nrhs, /* number of inputs */
const mxArray *prhs[] /* mxArray input pointer array */
)
{
int num_labels;
Graph::captype *DataCost;
Graph::captype *SmoothnessCost;
Graph::captype *Contrast = NULL;
GCoptimization::LabelType *Labels;
GCoptimization::PixelType R, C, Z;
GCoptimization *MyGraph = NULL;
/* check number of inout arguments - must be 6 */
if ((nrhs != 6)&&(nrhs!=7)) {
mexErrMsgIdAndTxt("GraphCut:NarginError","Wrong number of input argumnets");
}
GetScalar(prhs[0], R);
GetScalar(prhs[1], C);
GetScalar(prhs[2], Z);
GetScalar(prhs[3], num_labels);
/* check second input DataCost: must have #labels*height*width elements of type float */
if ( mxGetNumberOfElements(prhs[4]) != num_labels*R*C*Z ) {
mexErrMsgIdAndTxt("GraphCut:DataCost",
"DataCost argument does not contains the right number of elements");
}
if (mxGetClassID(prhs[4]) != mxSINGLE_CLASS ) {
mexErrMsgIdAndTxt("GraphCut:DataCost",
"DataCost argument is not of type float");
}
DataCost = (Graph::captype*)mxGetData(prhs[4]);
/* check fifth input SmoothnessCost: must have #labels.^2 elements of type float */
if ( mxGetNumberOfElements(prhs[5]) != num_labels*num_labels ) {
mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
"SmoothnessCost argument does not contains the right number of elements");
}
if (mxGetClassID(prhs[5]) != mxSINGLE_CLASS ) {
mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
"SmoothnessCost argument is not of type float");
}
SmoothnessCost = (Graph::captype*)mxGetData(prhs[5]);
if ( nrhs == 7 ) {
/* add Contrast cue */
if ( mxGetNumberOfElements(prhs[6]) != R*C*Z ) {
mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
"Contrast argument does not contains the right number of elements");
}
if (mxGetClassID(prhs[6]) != mxSINGLE_CLASS ) {
mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
"Contrast argument is not of type float");
}
Contrast = (Graph::captype*)mxGetData(prhs[6]);
}
/* prepare the output argument */
if ( nlhs != 1 ) {
mexErrMsgIdAndTxt("GraphCut:OutputArg","Wrong number of output arguments");
}
MyGraph = new GCoptimization(R*C*Z, num_labels, SET_ALL_AT_ONCE, SET_ALL_AT_ONCE);
/* neighborhod setup */
GCoptimization::PixelType c(0), r(0), z(0), p(0), q(0);
if (Contrast) {
for ( r = 0 ; r <= R - 2; r++ ) {
for ( c = 0 ; c <= C - 2; c++ ) {
for ( z = 0 ; z <= Z - 2; z++ ) {
/* all nodes with 3 nieghbors */
p = r+c*R+z*R*C;
q = r+1+c*R+z*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
q = r+(c+1)*R+z*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
q = r+c*R+(z+1)*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
}
/* top of Z has 2 n in c and r */
p = r+c*R+z*R*C;
q = r+1+c*R+z*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
q = r+(c+1)*R+z*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
}
/* end of c has 2 n in z and r */
for ( z = 0 ; z <= Z -2 ; z++ ) {
p = r+c*R+z*R*C;
q = r+1+c*R+z*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
q = r+c*R+(z+1)*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
}
/* end of c abd z has n in r */
p = r+c*R+z*R*C;
q = r+1+c*R+z*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
}
/* end of r has n in z and c */
for ( c = 0 ; c <= C - 2; c++ ) {
for ( z = 0 ; z <= Z - 2; z++ ) {
p = r+c*R+z*R*C;
q = r+c*R+(z+1)*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
q = r+(c+1)*R+z*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
}
p = r+c*R+z*R*C;
q = r+(c+1)*R+z*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
}
for ( z = 0 ; z <= Z - 2; z++ ) {
p = r+c*R+z*R*C;
q = r+c*R+(z+1)*R*C;
MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])));
}
/* end of graph construction with contrast */
} else {
for ( r = 0 ; r <= R - 2; r++ ) {
for ( c = 0 ; c <= C - 2; c++ ) {
for ( z = 0 ; z <= Z - 2; z++ ) {
/* all nodes with 3 nieghbors */
MyGraph->setNeighbors(r+c*R+z*R*C,r+1+c*R+z*R*C);
MyGraph->setNeighbors(r+c*R+z*R*C,r+(c+1)*R+z*R*C);
MyGraph->setNeighbors(r+c*R+z*R*C,r+c*R+(z+1)*R*C);
}
/* top of Z has 2 n in c and r */
MyGraph->setNeighbors(r+c*R+z*R*C,r+1+c*R+z*R*C);
MyGraph->setNeighbors(r+c*R+z*R*C,r+(c+1)*R+z*R*C);
}
/* end of c has 2 n in z and r */
for ( z = 0 ; z <= Z -2 ; z++ ) {
MyGraph->setNeighbors(r+c*R+z*R*C,r+1+c*R+z*R*C);
MyGraph->setNeighbors(r+c*R+z*R*C,r+c*R+(z+1)*R*C);
}
/* end of c abd z has n in r */
MyGraph->setNeighbors(r+c*R+z*R*C,r+1+c*R+z*R*C);
}
/* end of r has n in z and c */
for ( c = 0 ; c <= C - 2; c++ ) {
for ( z = 0 ; z <= Z - 2; z++ ) {
MyGraph->setNeighbors(r+c*R+z*R*C,r+c*R+(z+1)*R*C);
MyGraph->setNeighbors(r+c*R+z*R*C,r+(c+1)*R+z*R*C);
}
MyGraph->setNeighbors(r+c*R+z*R*C,r+(c+1)*R+z*R*C);
}
for ( z = 0 ; z <= Z - 2; z++ ) {
MyGraph->setNeighbors(r+c*R+z*R*C,r+c*R+(z+1)*R*C);
}
}
MyGraph->setData(DataCost);
/* if ( vCue != NULL && vCue != NULL )
MyGraph->setSmoothness(SmoothnessCost, hCue, vCue);
else*/
MyGraph->setSmoothness(SmoothnessCost);
/* create a container for the pointer */
plhs[0] = mxCreateNumericMatrix(1, 1, MATLAB_POINTER_TYPE, mxREAL);
GraphHandle* gh;
gh = (GraphHandle*) mxGetData(plhs[0]);
*gh = (GraphHandle)(MyGraph);
}