-
Notifications
You must be signed in to change notification settings - Fork 1
/
BP.hpp
416 lines (349 loc) · 9.94 KB
/
BP.hpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#ifndef BP_H
#define BP_H
#include <vector>
#include <utility>
#include <map>
#ifdef __linux__
#include <opencv2/imgcodecs.hpp>
#elif _WIN32
#include <opencv2\imgcodecs.hpp>
#endif
#include "birchTomasDisp.hpp"
#include "Matrix.hpp"
#include "pottsModel.hpp"
class BP
{
private:
cv::Mat lImg_, rImg_, opMat_;
std::vector< std::vector<double> > belief;
std::vector< std::map<int,std::vector<double> > > message;
std::vector<double> obs;
std::vector<double> states;
std::vector< std::vector< std::pair<int, int> > > pixOff;
std::vector< std::vector<double> > unaryTerm;
std::map< std::pair<int, int>, std::vector<std::vector<double> > > pairTerm;
std::vector<uchar> nodePos;
uchar pixLocation(int row, int col);
int nRow_, nCol_, nLabel_;
int maxIter;
double k;
int asyncUpdate();
std::vector<double> computeMessage(int row1, int col1, int row2, int col2);
int computeBelief(int row, int col);
int initMessageBelief();
int initBelief();
int initMessage();
int initPos();
int initFactorTerms();
int computeUnary();
int computePair();
public:
BP(cv::Mat lImg, cv::Mat rImg, int nLabel);
int loopyBPIterate();
};
BP::BP(cv::Mat lImg, cv::Mat rImg, int nLabel):nRow_(lImg.rows), nCol_(lImg.cols), nLabel_(nLabel)
{
lImg_ = lImg;
rImg_ = rImg;
opMat_ = lImg;
std::vector<std::pair<int, int> > pixVec;
std::pair<int, int> offPair;
//top left
offPair = std::make_pair(0,1); pixVec.push_back(offPair);
offPair = std::make_pair(1,0); pixVec.push_back(offPair);
pixOff.push_back(pixVec); pixVec.clear();
//top right
offPair = std::make_pair(0,-1); pixVec.push_back(offPair);
offPair = std::make_pair(1,0); pixVec.push_back(offPair);
pixOff.push_back(pixVec); pixVec.clear();
//bottom right
offPair = std::make_pair(0,-1); pixVec.push_back(offPair);
offPair = std::make_pair(-1,0); pixVec.push_back(offPair);
pixOff.push_back(pixVec); pixVec.clear();
//bottom left
offPair = std::make_pair(-1,0); pixVec.push_back(offPair);
offPair = std::make_pair(0,1); pixVec.push_back(offPair);
pixOff.push_back(pixVec); pixVec.clear();
//top bar
offPair = std::make_pair(0,-1); pixVec.push_back(offPair);
offPair = std::make_pair(0,1); pixVec.push_back(offPair);
offPair = std::make_pair(1,0); pixVec.push_back(offPair);
pixOff.push_back(pixVec); pixVec.clear();
//bottom bar
offPair = std::make_pair(0,-1); pixVec.push_back(offPair);
offPair = std::make_pair(0,1); pixVec.push_back(offPair);
offPair = std::make_pair(-1,0); pixVec.push_back(offPair);
pixOff.push_back(pixVec); pixVec.clear();
//left bar
offPair = std::make_pair(-1,0); pixVec.push_back(offPair);
offPair = std::make_pair(0,1); pixVec.push_back(offPair);
offPair = std::make_pair(1,0); pixVec.push_back(offPair);
pixOff.push_back(pixVec); pixVec.clear();
//right bar
offPair = std::make_pair(-1,0); pixVec.push_back(offPair);
offPair = std::make_pair(0,-1); pixVec.push_back(offPair);
offPair = std::make_pair(1,0); pixVec.push_back(offPair);
pixOff.push_back(pixVec); pixVec.clear();
//interior pixels
offPair = std::make_pair(0,-1); pixVec.push_back(offPair);
offPair = std::make_pair(0,1); pixVec.push_back(offPair);
offPair = std::make_pair(-1,0); pixVec.push_back(offPair);
offPair = std::make_pair(1,0); pixVec.push_back(offPair);
pixOff.push_back(pixVec); pixVec.clear();
initFactorTerms();
initMessageBelief();
maxIter = 20;
}
int BP::computeBelief(int row, int col)
{
int node = row*nCol_ + col;
//int pixOffInd = pixLocation(row, col);
std::vector<std::pair<int,int> > pixVec = pixOff[nodePos[row*nCol_+col]];
belief[node] = unaryTerm[node];
for (int i = 0; i != pixVec.size(); ++i)
{
int sendNode = (row+pixVec[i].first)*nCol_ + (col+pixVec[i].second);
belief[node] = belief[node]*message[sendNode][node];
}
double beliefSum = 0;
for (int i = 0; i != belief[node].size(); ++i)
{
beliefSum += belief[node][i];
}
belief[node] = belief[node]/beliefSum;
return 0;
}
int BP::loopyBPIterate()
{
for (int i = 0; i != maxIter; ++i)
{
asyncUpdate();
std::cout<<"asynchronous iteration: "<<i<<std::endl;
}
for (int i = 0; i != nRow_; ++i)
{
for (int j = 0; j != nCol_; ++j)
{
computeBelief(i,j);
double depthVal = 0;
for (int k = 0; k != nLabel_; ++k)
{
depthVal += belief[i*nCol_+j][k]*k;
}
opMat_.at<uchar>(i,j) = static_cast<uchar>((depthVal/static_cast<double>(nLabel_))*255);
}
}
cv::imwrite("depth_image.png",opMat_);
return 0;
}
//up-down-left-right update; refer tappen & freeman
int BP::asyncUpdate()
{
for (int i = 0; i != nCol_; ++i)//down all columns
{
for (int j = 0; j != (nRow_-1); ++j)
{
message[j*nCol_+i][(j+1)*nCol_+i] = computeMessage(j,i,j+1,i);
}
}
for (int i = 0; i != nCol_; ++i)//up all columns
{
for (int j = (nRow_-1); j != 0; --j)
{
message[j*nCol_+i][(j-1)*nCol_+i] = computeMessage(j,i,j-1,i);
}
}
for (int j = 0; j != nRow_; ++j)//left to right all rows
{
for (int i = 0; i != (nCol_-1); ++i)
{
message[j*nCol_+i][j*nCol_+i+1] = computeMessage(j,i,j,i+1);
}
}
for (int j = 0; j != nRow_; ++j)//right to left all rows
{
for (int i = (nCol_-1); i != 0; --i)
{
message[j*nCol_+i][j*nCol_+i-1] = computeMessage(j,i,j,i-1);
}
}
return 0;
}
std::vector<double> BP::computeMessage(int row1, int col1, int row2, int col2)
{
int nodeTx = row1*nCol_ + col1; //sending node
int nodeRx = row2*nCol_ + col2; //receiving node
std::pair<int, int> pairInd;
if (nodeTx < nodeRx)
{
pairInd = std::make_pair(nodeTx, nodeRx);
}
else
{
pairInd = std::make_pair(nodeRx, nodeTx);
}
std::vector<double> mVec = unaryTerm[nodeTx];
//int pixOffInd = pixLocation(row1,col1);
std::vector<std::pair<int,int> > pixVec = pixOff[nodePos[row1*nCol_+col1]];
for (int i = 0; i != pixVec.size(); ++i)
{
int neighNode = (row1 + pixVec[i].first)*nCol_ + (col1 + pixVec[i].second);
if (neighNode != nodeRx)
{
mVec = mVec*message[neighNode][nodeTx];
}
}
mVec = pairTerm[pairInd]*mVec;
double normSum = 0;
for (int i = 0; i != mVec.size(); ++i)
{
normSum += mVec[i];
}
mVec = mVec/normSum;
return mVec;
}
int BP::computeUnary()
{
for (int i = 0; i != nRow_; ++i)
{
for (int j = 0; j != nCol_; ++j)
{
std::vector<double> dispVec = birchTomasDisp(i, j, nLabel_, lImg_, rImg_);
//double normSum = 0;
for (int k = 0; k != dispVec.size(); ++k)
{
dispVec[k] = exp(-1*(dispVec[k]/16)); //raise to the negative power and normalize to suit BP; according to Tappen and Freeman
//normSum += dispVec[k];
}
//dispVec = dispVec/normSum;
unaryTerm.push_back(dispVec); //using Birchfield-Tomasi measure
}
}
return 0;
}
int BP::computePair()
{
for (int iR = 0; iR != nRow_; ++iR)
{
for (int iC = 0; iC != nCol_; ++iC)
{
//int pixOffInd = pixLocation(iR, iC);
std::vector<std::pair<int,int> > pixVec = pixOff[nodePos[iR*nCol_ + iC]];
for (int p = 0; p != pixVec.size(); ++p)
{
std::vector<std::vector<double> > pottsMat = pottsModel(iR, iC, pixVec[p].first, pixVec[p].second, nLabel_, lImg_);
for (int i = 0; i != pottsMat.size(); ++i)
{
std::vector<double> pottsVec = pottsMat[i];
//double normSum = 0;
for (int j = 0; j != pottsVec.size(); ++j)
{
pottsVec[j] = exp(-1*(pottsVec[j]/20)); //raise to the negative power and normalize to suit BP; according to Tappen and Freeman
//normSum += pottsVec[j];
}
pottsMat[i] = pottsVec; ///normSum;
}
pairTerm.insert(std::make_pair(std::make_pair(iR*nCol_ + iC, (iR+pixVec[p].first)*nCol_ + iC + pixVec[p].second), pottsMat));
}
}
}
return 0;
}
int BP::initFactorTerms()
{
initPos();
computeUnary();
computePair();
std::cout<<"done:initialization of energy terms"<<std::endl;
return 0;
}
int BP::initMessageBelief()
{
initBelief();
initMessage();
return 0;
}
int BP::initBelief()
{
std::vector<double> initVec(nLabel_, 0);
for (int i = 0; i != nRow_; ++i)
{
for (int j = 0; j != nCol_; ++j)
{
belief.push_back(initVec);
}
}
return 0;
}
int BP::initMessage()
{
std::vector<double> initVec(nLabel_, 1);
for (int i = 0; i != nRow_; ++i)
{
for (int j = 0; j != nCol_; ++j)
{
int nodeTx = i*nCol_ + j; //sending node
//int pixOffInd = pixLocation(i,j);
std::vector<std::pair<int,int> > pixVec = pixOff[nodePos[i*nCol_+j]];
std::map<int, std::vector<double> > messageMap;
for (int k = 0; k != pixVec.size(); ++k)
{
int neighNode = (i + pixVec[k].first)*nCol_ + (j + pixVec[k].second);
messageMap.insert(std::make_pair(neighNode,initVec));
}
message.push_back(messageMap);
}
}
return 0;
}
int BP::initPos()
{
for (int i = 0; i != nRow_; ++i)
{
for (int j = 0; j != nCol_; ++j)
{
nodePos.push_back(pixLocation(i,j));
}
}
return 0;
}
uchar BP::pixLocation(int row, int col)
{
if ((row == 0) && (col == 0))
{
return 0; //top left
}
else if ((row == 0) && (col == nCol_-1))
{
return 1; //top right
}
else if ((row == nRow_-1) && (col == nCol_-1))
{
return 2; //bottom right
}
else if ((row == nRow_-1) && (col == 0))
{
return 3; //bottom left
}
else if ((row == 0) && (col != 0) && (col != nCol_-1))
{
return 4; //top bar
}
else if ((row == nRow_-1) && (col != 0) && (col != nCol_-1))
{
return 5; //bottom bar
}
else if (col == 0)
{
return 6; //left bar
}
else if (col == nCol_-1)
{
return 7; //right bar
}
else
{
return 8; //interior pixels
}
}
#endif // BP_H