-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvex.cpp
352 lines (282 loc) · 9 KB
/
Convex.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
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
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <vector>
#include <cmath>
using namespace cv;
using namespace std;
const int modK = 21;
const int modNt = 2;
const double infinity = 10000000000;
const int amount_of_samples = 4;
const double lambda = 100;
const double starting_ksi = 10;
const int total_iter = 100;
void save_and_show(int* res, const int width, const int height, string name, bool save = false)
{
Mat* result = new Mat[3];
for (int c = 0; c < 3; ++c)
{
result[c] = Mat::zeros(Size(width, height), CV_8UC1);
for (int x = 0; x < width; ++x)
for (int y = 0; y < height; ++y)
{
result[c].at<uchar>(y, x) = uchar(res[x * height + y] * 8);
}
}
Mat rez;
vector<Mat> channels;
channels.push_back(result[0]);
channels.push_back(result[1]);
channels.push_back(result[2]);
merge(channels, rez);
namedWindow(name, WINDOW_AUTOSIZE);
cv::imshow(name, rez);
if (save)
imwrite(name + ".png", rez);
delete[] result;
}
inline double get_q(double* q, int* lcolors, int* rcolors, const int t, const int k, const int height, double* L)
{
return (q[abs(lcolors[t * 3] - rcolors[(t + k * height) * 3])] +
q[abs(lcolors[t * 3 + 1] - rcolors[(t + k * height) * 3 + 1])] +
q[abs(lcolors[t * 3 + 2] - rcolors[(t + k * height) * 3 + 2])] -
L[t * modK + k]);
}
inline double get_g(double* g, const int c1, const int c2)
{
return g[abs(c1 - c2)];
}
int find(int* arr, const int start, const int length, const int t)
{
for (int i = 0; i < length; ++i)
if (arr[start + i] == t)
return i;
cout << "Something is wrong... I can feel it" << endl;
return -1;
}
int* dynamics(const int width, const int height,
int* lcolors, int* rcolors,
double* g, double* q, double* L)
{
const int modT = width * height;
int* result = new int[modT]();
for (int y = 0; y < height; ++y)
{
int* ks = new int[width * modK];
double* f = new double[width * modK]();
// For f_0(k_1) to f_n-1(k_n)
for (int x = 0; x < width - 1; ++x)
{
for (int k = 0; k < modK; ++k)
{
int ind = -1;
double best = infinity;
for (int k_ = 0; k_ < min(modK, width - x); ++k_)
{
double cur;
if (x == 0)
cur = get_q(q, lcolors, rcolors, x * height + y, k_, height, L) + get_g(g, k, k_);
else
cur = get_q(q, lcolors, rcolors, x * height + y, k_, height, L) + get_g(g, k, k_) + f[(x - 1) * modK + k_];
if (cur < best)
{
ind = k_;
best = cur;
}
}
ks[x * modK + k] = ind;
f[x * modK + k] = best;
}
}
// For max_k_n(f_n-1(k_n) + q_n)
int ind = -1;
double best = infinity;
for (int k_ = 0; k_ < 1; ++k_)
{
double cur = get_q(q, lcolors, rcolors, (width - 1) * height + y, k_, height, L) + f[(width - 1) * modK + k_];
if (cur < best)
{
ind = k_;
best = cur;
}
}
result[(width - 1) * height + y] = ind;
for (int x = width - 2; x >= 0; --x)
{
result[x * height + y] = ks[x * modK + result[(x + 1) * height + y]];
}
delete[] ks;
delete[] f;
}
return result;
}
inline double L_func(const int k1, const int k2)
{
return abs(k1 - k2);
}
void update_q_g(int** lcolors, int** rcolors, int** gcolors, int* widthes, int* heightes, double* q, double* g)
{
for (int it = 0; it < total_iter; ++it)
{
double ksi = starting_ksi / double(it + 1);
cout << it << " iteration of learning" << endl;
for (int i = 0; i < amount_of_samples; ++i)
{
cout << i << " sample" << endl;
const int modT = widthes[i] * heightes[i];
double* L = new double[modT * modK];
for (int t = 0; t < modT; ++t)
for (int k = 0; k < modK; ++k)
L[t * modK + k] = L_func(gcolors[i][t], k);
int* res = dynamics(widthes[i], heightes[i], lcolors[i], rcolors[i], g, q, L);
double* grad_q = new double[256]();
double* grad_g = new double[modK]();
// For safety
for (int a = 0; a < 256; ++a)
grad_q[a] = 0;
for (int b = 0; b < modK; ++b)
grad_g[b] = 0;
for (int a = 0; a < 256; ++a)
grad_q[a] -= 2 * lambda * q[a];
for (int b = 0; b < modK; ++b)
grad_g[b] -= 2 * lambda * g[b];
// Update q
for (int t = 0; t < modT; ++t)
{
if (res[t] == -1)
cout << "ALARM!!!" << endl;
if ((t + gcolors[i][t] * heightes[i]) < modT)
{
grad_q[abs(lcolors[i][t * 3] - rcolors[i][(t + res[t] * heightes[i]) * 3])] -= 1;
grad_q[abs(lcolors[i][t * 3 + 1] - rcolors[i][(t + res[t] * heightes[i]) * 3 + 1])] -= 1;
grad_q[abs(lcolors[i][t * 3 + 2] - rcolors[i][(t + res[t] * heightes[i]) * 3 + 2])] -= 1;
grad_q[abs(lcolors[i][t * 3] - rcolors[i][(t + gcolors[i][t] * heightes[i]) * 3])] += 1;
grad_q[abs(lcolors[i][t * 3 + 1] - rcolors[i][(t + gcolors[i][t] * heightes[i]) * 3 + 1])] += 1;
grad_q[abs(lcolors[i][t * 3 + 2] - rcolors[i][(t + gcolors[i][t] * heightes[i]) * 3 + 2])] += 1;
}
}
// Update g
for (int t = 0; t < modT - heightes[i]; ++t)
{
grad_g[abs(res[t] - res[t + heightes[i]])] -= 1;
grad_g[abs(gcolors[i][t] - gcolors[i][t + heightes[i]])] += 1;
}
// Normalizing grads
double sum_grad = 0.;
for (int a = 0; a < 256; ++a)
sum_grad += pow(grad_q[a], 2);
for (int b = 0; b < modK; ++b)
sum_grad += pow(grad_g[b], 2);
for (int a = 0; a < 256; ++a)
q[a] += grad_q[a] * ksi / sqrt(sum_grad);
for (int b = 0; b < modK; ++b)
g[b] += grad_g[b] * ksi / sqrt(sum_grad);
delete[] grad_q;
delete[] grad_g;
delete[] L;
delete[] res;
}
}
}
int main()
{
int** lcolors = new int* [amount_of_samples];
int** rcolors = new int* [amount_of_samples];
int** gcolors = new int* [amount_of_samples];
int* heightes = new int[amount_of_samples];
int* widthes = new int[amount_of_samples];
double* q = new double[256]();
for (int i = 0; i < 256; ++i)
q[i] = i;
double* g = new double[modK]();
for (int i = 0; i < modK; ++i)
g[i] = i;
for (int i = 0; i < amount_of_samples; ++i)
{
Mat limage_, limage[4];
limage_ = imread("./dataset/" + to_string(i + 1) + "/im6.ppm", IMREAD_UNCHANGED);
split(limage_, limage);
Mat rimage_, rimage[4];
rimage_ = imread("./dataset/" + to_string(i + 1) + "/im2.ppm", IMREAD_UNCHANGED);
split(rimage_, rimage);
Mat gimage;
gimage = imread("./dataset/" + to_string(i + 1) + "/disp2.pgm", IMREAD_UNCHANGED);
heightes[i] = limage[0].size().height;
widthes[i] = limage[0].size().width;
lcolors[i] = new int[widthes[i] * heightes[i] * 3];
rcolors[i] = new int[widthes[i] * heightes[i] * 3];
gcolors[i] = new int[widthes[i] * heightes[i]];
for (int x = 0; x < widthes[i]; ++x)
{
for (int y = 0; y < heightes[i]; ++y)
{
for (int c = 0; c < 3; ++c)
{
lcolors[i][x * heightes[i] * 3 + y * 3 + c] = int(limage[c].at<uchar>(y, x));
rcolors[i][x * heightes[i] * 3 + y * 3 + c] = int(rimage[c].at<uchar>(y, x));
}
gcolors[i][x * heightes[i] + y] = int((gimage.at<uchar>(y, x) + 1) / 8);
}
}
}
update_q_g(lcolors, rcolors, gcolors, widthes, heightes, q, g);
for (int i = 0; i < amount_of_samples; ++i)
{
const int modT = widthes[i] * heightes[i];
double* L = new double[modT * modK];
for (int t = 0; t < modT; ++t)
for (int k = 0; k < modK; ++k)
L[t * modK + k] = 0.;
int* res = dynamics(widthes[i], heightes[i], lcolors[i], rcolors[i], g, q, L);
save_and_show(res, widthes[i], heightes[i], "sample number " + to_string(i), false);
delete[] L;
delete[] res;
}
Mat limage_, limage[4];
limage_ = imread("./test/im6.ppm", IMREAD_UNCHANGED);
split(limage_, limage);
Mat rimage_, rimage[4];
rimage_ = imread("./test/im2.ppm", IMREAD_UNCHANGED);
split(rimage_, rimage);
Mat gimage;
gimage = imread("./test/disp2.pgm", IMREAD_UNCHANGED);
const int height = limage[0].size().height;
const int width = limage[0].size().width;
int* ltest = new int[width * height * 3];
int* rtest = new int[width * height * 3];
int* gtest = new int[width * height];
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
for (int c = 0; c < 3; ++c)
{
ltest[x * height * 3 + y * 3 + c] = int(limage[c].at<uchar>(y, x));
rtest[x * height * 3 + y * 3 + c] = int(rimage[c].at<uchar>(y, x));
}
gtest[x * height + y] = int(gimage.at<uchar>(y, x));
}
}
// Create neighbour structure
const int modT = width * height;
double* L = new double[modT * modK]();
for (int t = 0; t < modT; ++t)
for (int k = 0; k < modK; ++k)
L[t * modK + k] = 0.;
int* res = dynamics(width, height, ltest, rtest, g, q, L);
save_and_show(res, width, height, "result", true);
std::cout << "q:\n";
for (int i = 0; i < 256; ++i) {
std::cout << q[i] << " ";
}
std::cout << "\n\n";
std::cout << "g:\n";
for (int i = 0; i < modK; ++i) {
std::cout << g[i] << " ";
}
std::cout << "\n\n";
waitKey(0);
return 0;
}