forked from larrylindsey/imageprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphCut.cpp
More file actions
291 lines (201 loc) · 7.03 KB
/
Copy pathGraphCut.cpp
File metadata and controls
291 lines (201 loc) · 7.03 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <math.h>
#include <vigra/basicimage.hxx>
#include "GraphCut.h"
logger::LogChannel graphcutlog("graphcutlog", "[GraphCut] ");
GraphCut::GraphCut() :
_segmentation(boost::make_shared<Image>()),
_energy(0),
_graph(0, 0),
_imageChanged(true),
_gcParametersChanged(true),
_pottsImageChanged(true),
_graphWidth(0),
_graphHeight(0),
_warmStart(false) {
registerInput(_image,"image");
registerInput(_parameters,"parameters");
registerInput(_pottsImage, "potts image");
registerOutput(_segmentation, "segmentation");
registerOutput(_energy, "energy");
// register for input modification signals
_image.registerBackwardCallback(&GraphCut::onModifiedImage, this);
_parameters.registerBackwardCallback(&GraphCut::onModifiedGCParameters, this);
_pottsImage.registerBackwardCallback(&GraphCut::onModifiedPottsImage, this);
}
void
GraphCut::onModifiedImage(const pipeline::Modified&) {
LOG_DEBUG(graphcutlog) << "image modified!" << std::endl;
// send the 'modified' signal upstream
_imageChanged = true;
}
void
GraphCut::onModifiedPottsImage(const pipeline::Modified&) {
LOG_DEBUG(graphcutlog) << "potts image modified!" << std::endl;
// send the 'modified' signal upstream
_pottsImageChanged = true;
}
void
GraphCut::onModifiedGCParameters(const pipeline::Modified&) {
LOG_DEBUG(graphcutlog) << "parameters modified!" << std::endl;
// send the 'modified' signal upstream
_gcParametersChanged = true;
}
void
GraphCut::updateOutputs() {
_setTerminalWeights = false;
_setEdges = false;
if (_imageChanged)
_setTerminalWeights = true;
if (_pottsImageChanged)
_setEdges = true;
if (_gcParametersChanged) {
if (_parameters->pottsWeight != _prevParameters.pottsWeight)
_setEdges = true;
if (_parameters->contrastWeight != _prevParameters.contrastWeight)
_setEdges = true;
if (_parameters->contrastSigma != _prevParameters.contrastSigma)
_setEdges = true;
if (_parameters->foregroundPrior != _prevParameters.foregroundPrior)
_setTerminalWeights = true;
}
doMaxFlow();
_imageChanged = false;
_gcParametersChanged = false;
_pottsImageChanged = false;
// try to perform a warm start on the next invocation
_warmStart = true;
// remember the current settings
_prevParameters = *_parameters;
}
void
GraphCut::doMaxFlow() {
// check if there was a change that requires recreation of the graph
if (_graphWidth != _image->width() || _graphHeight != _image->height() ||
_parameters->eightNeighborhood != _prevParameters.eightNeighborhood ||
_parameters->foregroundPrior == 0.0 || _parameters->foregroundPrior == 1.0 ||
_prevParameters.foregroundPrior == 0.0 || _prevParameters.foregroundPrior == 1.0) {
LOG_DEBUG(graphcutlog) << "(re)creating graph" << std::endl;
_graph.reset();
_setTerminalWeights = true;
_setEdges = true;
_warmStart = false;
for (int i = 0; i < _image->width()*_image->height(); i++)
_graph.add_node();
_graphWidth = _image->width();
_graphHeight = _image->height();
}
// add terminal weights (source and sink)
if (_setTerminalWeights) {
LOG_DEBUG(graphcutlog) << "setting terminal weights..." << std::endl;
setTerminalWeights();
_setTerminalWeights = false;
}
// adding edges between neighbors
if (_setEdges) {
LOG_DEBUG(graphcutlog) << "setting edge weights..." << std::endl;
setEdgeWeights();
_setEdges = false;
}
LOG_DEBUG(graphcutlog) << "finding max flow" << (_warmStart ? " (performing warm start)" : "") << "..." << std::endl;
/* Althout this would be the desired call:
*
* *_energy = _graph.maxflow(_warmStart);
*
* we can't do it, since there seem to be problems with numerical
* stabilities in Pushmeet's code. After a lot of warmstarts, the results
* are just wrong. This version, however, does at least reuse the graph (but
* not the search trees).
*/
*_energy = _graph.maxflow(false);
// get the segmentation
getSegmentation();
}
float
GraphCut::getCapacity(float probability, float foreground) {
return -log(probability) - log(foreground);
}
int
GraphCut::getNodeId(int x, int y) {
return x*_image->height() + y;
}
void
GraphCut::setTerminalWeights() {
for (int x = 0; x < _image->width(); x++) {
for (int y = 0; y < _image->height(); y++) {
float value = (*_image)(x, y);
unsigned int nodeId = getNodeId(x, y);
_graph.edit_tweights_wt(
nodeId,
getCapacity(value, _parameters->foregroundPrior),
getCapacity(1.0f - value, 1.0f - _parameters->foregroundPrior));
}
}
}
void
GraphCut::setEdgeWeights() {
for (int x = 0; x < _image->width(); x++) {
for (int y = 0; y < _image->height(); y++) {
// adds edges to a node at x, y and assigns weights to those edges
int nodeId = getNodeId(x, y);
int neighborId;
if (y-1 >= 0){
neighborId = getNodeId(x, y-1);
if (_warmStart)
_graph.edit_edge_wt(nodeId, neighborId, getPairwiseCosts(x, y, x, y-1), getPairwiseCosts(x, y, x, y-1));
else
_graph.add_edge(nodeId, neighborId, getPairwiseCosts(x, y, x, y-1), getPairwiseCosts(x, y, x, y-1));
}
if (x-1 >= 0){
neighborId = getNodeId(x-1, y);
if (_warmStart)
_graph.edit_edge_wt(nodeId, neighborId, getPairwiseCosts(x, y, x-1, y), getPairwiseCosts(x, y, x-1, y));
else
_graph.add_edge(nodeId, neighborId, getPairwiseCosts(x, y, x-1, y), getPairwiseCosts(x, y, x-1, y));
}
if (_parameters->eightNeighborhood) {
if ( (x-1 >= 0) && (y-1 >= 0) ) {
neighborId = getNodeId(x-1, y-1);
if (_warmStart)
_graph.edit_edge_wt(nodeId, neighborId, getPairwiseCosts(x, y, x-1, y-1), getPairwiseCosts(x, y, x-1, y-1));
else
_graph.add_edge(nodeId, neighborId, getPairwiseCosts(x, y, x-1, y-1), getPairwiseCosts(x, y, x-1, y-1));
}
if ( (y+1 < _image->height()) && (x-1 >= 0) ) {
neighborId = getNodeId(x-1, y+1);
if (_warmStart)
_graph.edit_edge_wt(nodeId, neighborId, getPairwiseCosts(x, y, x-1, y+1), getPairwiseCosts(x, y, x-1, y+1));
else
_graph.add_edge(nodeId, neighborId, getPairwiseCosts(x, y, x-1, y+1), getPairwiseCosts(x, y, x-1, y+1));
}
}
}
}
}
void GraphCut::getSegmentation(){
LOG_DEBUG(graphcutlog) << "resizing image to " << _image->shape() << std::endl;
// adjust the size of the segmentation output to match the input image
_segmentationData.reshape(_image->shape());
*_segmentation = _segmentationData;
for (int x = 0; x < _image->width(); x++){
for (int y = 0; y < _image->height(); y++){
unsigned int nodeId = getNodeId(x,y);
if(_graph.what_segment(nodeId) == graph_type::SOURCE){
(*_segmentation)(x, y) = 0;
} else {
(*_segmentation)(x, y) = 1;
}
}
}
}
double GraphCut::getPairwiseCosts(int x1, int y1, int x2, int y2){
// the distance between the two pixels
double dist = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
double pottsTerm = _parameters->pottsWeight/dist;
double contrastTerm = 0.0;
if (_pottsImage) {
double g = (*_pottsImage)(x1, y1) - (*_pottsImage)(x2, y2);
double e = exp ( -pow(g, 2) / (2.0 * pow(_parameters->contrastSigma, 2)));
contrastTerm = _parameters->contrastWeight*e/dist;
}
return pottsTerm + contrastTerm;
}