-
Notifications
You must be signed in to change notification settings - Fork 0
/
sibson.h
532 lines (428 loc) · 16.4 KB
/
sibson.h
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include <list>
#include <fstream>
#include <iostream>
#include <cmath>
#include <iterator>
#include <Python.h>
#include <string>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/natural_neighbor_coordinates_2.h>
#include "cimg.h"
#include "point.h"
#include "random.h"
#include "console.h"
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> Delaunay_triangulation;
typedef std::vector< std::pair< K::Point_2, K::FT > >
Point_coordinate_vector;
using namespace cimg_library;
bool pair_cmp(const std::pair<std::pair<int, int>, int>& firstElem, const std::pair<std::pair<int, int>, int>& secondElem) {
return firstElem.second > secondElem.second;
}
class Sibson
{
public:
Sibson() {}
~Sibson() {}
public:
bool run(const int nb_points,
const int mode,
const char *pInput,
const char *pOutput)
{
// read input image
std::cout << "read input image...";
CImg<unsigned char> input(pInput);
// verify image size
const int width = input.width();
const int height = input.height();
if (width * height == 0)
{
std::cerr << red << "failed" << white << std::endl;
return false;
}
std::cout << "done" << std::endl;
// generate initial points to interpolate
// at the four image corners
std::list<Point> points;
points.push_back(Point(0.0, 0.0));
points.push_back(Point(width - 1.0, 0.0));
points.push_back(Point(0.0, height - 1.0));
points.push_back(Point(width - 1.0, height - 1.0));
if(mode == 1 || mode == 4){
// TODO: add random points
// tip: use eg function ::random_int(0, width - 1) to get a random x coordinate value
std::cout << "adding " << nb_points << " random points...";
for (int i = 0; i < nb_points; i++)
{
int x = random_int(0, width - 1);
int y = random_int(0, height - 1);
points.push_back(Point(x, y));
}
points.unique();
std::cout << "done" << std::endl;
}
int test_num = width * height - (int)points.size();
std::cout << test_num << " points to calculate..."<< std::endl;
Delaunay_triangulation dt;
// do a delaunay triangulation
for (std::list<Point>::iterator it = points.begin(); it != points.end(); it++)
dt.insert(K::Point_2(it->x(),it->y()));
CImg<unsigned char> diff(width, height, 1, 3, 0);
CImg<unsigned char> output(width, height, 1, 3, 0);
float error[3] = {0.0};
// interpolation
std::cout << "interpolating...";
for (int x = 0; x < width; x++)
{
// simple ASCII progress bar
std::cout << ".";
for (int y = 0; y < height; y++)
{
const Point query(x, y);
double r = 0.0;
double g = 0.0;
double b = 0.0;
std::list<Point>::iterator itc = points.end();
for(std::list<Point>::iterator it = points.begin();
it != points.end();
it++){
if(query.coincide(*it))
itc = it;
}
if(itc != points.end()){
r = (double)input(x, y, 0);
g = (double)input(x, y, 1);
b = (double)input(x, y, 2);
}
else{
K::Point_2 p(x, y);
Point_coordinate_vector coords;
CGAL::Triple<std::back_insert_iterator<Point_coordinate_vector>,
K::FT, bool> result =
CGAL::natural_neighbor_coordinates_2(dt, p,
std::back_inserter(coords));
for(int i = 0; i < coords.size(); i++){
const double w = coords[i].second / result.second;
const int xp = coords[i].first.x();
const int yp = coords[i].first.y();
r += w * (double)input(xp, yp, 0);
g += w * (double)input(xp, yp, 1);
b += w * (double)input(xp, yp, 2);
}
}
// write pixel color
output.atXY(x, y, 0) = (unsigned char)r;
output.atXY(x, y, 1) = (unsigned char)g;
output.atXY(x, y, 2) = (unsigned char)b;
// write error image
error[0] += abs(r - input(x, y, 0));
error[1] += abs(g - input(x, y, 1));
error[2] += abs(b - input(x, y, 2));
diff.atXY(x, y, 0) = (unsigned char)abs(r - input(x, y, 0));
diff.atXY(x, y, 1) = (unsigned char)abs(g - input(x, y, 1));
diff.atXY(x, y, 2) = (unsigned char)abs(b - input(x, y, 2));
}
}
std::cout << "done" << std::endl;
error[0] = error[0] / test_num;
error[1] = error[1] / test_num;
error[2] = error[2] / test_num;
std::cout << "The average error in R channel: " << error[0] << std::endl;
std::cout << "The average error in G channel: " << error[1] << std::endl;
std::cout << "The average error in B channel: " << error[2] << std::endl;
std::cout << "write image...";
output.save_bmp(pOutput);
diff.save_bmp("diff.bmp");
std::cout << "done" << std::endl << std::endl;
return true;
}
bool run(const float max_error,
const int mode,
const char *pInput,
const char *pOutput)
{
// read input image
std::cout << "read input image...";
CImg<unsigned char> input(pInput);
// verify image size
const int width = input.width();
const int height = input.height();
if (width * height == 0)
{
std::cerr << red << "failed" << white << std::endl;
return false;
}
std::cout << "done" << std::endl;
// generate initial points to interpolate
// at the four image corners
std::list<Point> points;
points.push_back(Point(0.0, 0.0));
points.push_back(Point(width - 1.0, 0.0));
points.push_back(Point(0.0, height - 1.0));
points.push_back(Point(width - 1.0, height - 1.0));
Delaunay_triangulation dt;
// do a delaunay triangulation
for (std::list<Point>::iterator it = points.begin(); it != points.end(); it++)
dt.insert(K::Point_2(it->x(),it->y()));
float error[3] = {256.0};
// Create an error image
CImg<unsigned char> diff(width, height, 1, 3, 0);
CImg<unsigned char> output(width, height, 1, 3, 0); // color image with pixels set to
while(error[0] > max_error || error[1] > max_error || error[2] > max_error){
if(mode == 2){
int x = random_int(0, width - 1);
int y = random_int(0, height - 1);
points.push_back(Point(x, y));
dt.insert(K::Point_2(x, y));
std::cout << "add one point..." << std::endl;
}
else{
int add_num = (int)points.size();
for(int i = 0; i < add_num; i++){
int x = random_int(0, width - 1);
int y = random_int(0, height - 1);
points.push_back(Point(x, y));
dt.insert(K::Point_2(x, y));
}
std::cout << "add "<< add_num << " points..." << std::endl;
}
// interpolation
error[0] = 0.0;
error[1] = 0.0;
error[2] = 0.0;
int test_num = width * height - (int)points.size();
std::cout << "interpolating...";
for (int x = 0; x < width; x++)
{
// simple ASCII progress bar
std::cout << ".";
for (int y = 0; y < height; y++)
{
const Point query(x, y);
double r = 0.0;
double g = 0.0;
double b = 0.0;
std::list<Point>::iterator itc = points.end();
for(std::list<Point>::iterator it = points.begin();
it != points.end();
it++){
if(query.coincide(*it))
itc = it;
}
if(itc != points.end()){
r = (double)input(x, y, 0);
g = (double)input(x, y, 1);
b = (double)input(x, y, 2);
}
else{
K::Point_2 p(x, y);
Point_coordinate_vector coords;
CGAL::Triple<std::back_insert_iterator<Point_coordinate_vector>,
K::FT, bool> result =
CGAL::natural_neighbor_coordinates_2(dt, p,
std::back_inserter(coords));
for(int i = 0; i < coords.size(); i++){
const double w = coords[i].second / result.second;
const int xp = coords[i].first.x();
const int yp = coords[i].first.y();
r += w * (double)input(xp, yp, 0);
g += w * (double)input(xp, yp, 1);
b += w * (double)input(xp, yp, 2);
}
}
// write pixel color
output.atXY(x, y, 0) = (unsigned char)r;
output.atXY(x, y, 1) = (unsigned char)g;
output.atXY(x, y, 2) = (unsigned char)b;
// write error image
error[0] += abs(r - input(x, y, 0));
error[1] += abs(g - input(x, y, 1));
error[2] += abs(b - input(x, y, 2));
diff.atXY(x, y, 0) = (unsigned char)abs(r - input(x, y, 0));
diff.atXY(x, y, 1) = (unsigned char)abs(g - input(x, y, 1));
diff.atXY(x, y, 2) = (unsigned char)abs(b - input(x, y, 2));
}
}
std::cout << "done" << std::endl;
error[0] = error[0] / test_num;
error[1] = error[1] / test_num;
error[2] = error[2] / test_num;
std::cout << "The average error in R channel: " << error[0] << std::endl;
std::cout << "The average error in G channel: " << error[1] << std::endl;
std::cout << "The average error in B channel: " << error[2] << std::endl;
}
std::cout << "write image...";
output.save_bmp(pOutput);
diff.save_bmp("diff.bmp");
std::cout << "done" << std::endl << std::endl;
return true;
}
bool run(const int nb_points,
const char *pInput,
const char *pOutput)
{
// read input image
std::cout << "read input image...";
CImg<unsigned char> input(pInput);
// verify image size
const int width = input.width();
const int height = input.height();
if (width * height == 0)
{
std::cerr << red << "failed" << white << std::endl;
return false;
}
std::cout << "done" << std::endl;
std::ofstream myfile;
// change it !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
myfile.open("/home/zt/Maillage/sibson_interpolation/errors.txt");
// generate initial points to interpolate
// at the four image corners
std::list<Point> points;
points.push_back(Point(0.0, 0.0));
points.push_back(Point(width - 1.0, 0.0));
points.push_back(Point(0.0, height - 1.0));
points.push_back(Point(width - 1.0, height - 1.0));
// TODO: add random points
// tip: use eg function ::random_int(0, width - 1) to get a random x coordinate value
std::cout << "adding " << nb_points << " random points...";
for (int i = 0; i < nb_points; i++)
{
int x = random_int(0, width - 1);
int y = random_int(0, height - 1);
points.push_back(Point(x, y));
}
points.unique();
std::cout << "done" << std::endl;
int test_num = width * height - (int)points.size();
std::cout << test_num << " points to calculate..."<< std::endl;
float p_error = 256.0 * 3;
CImg<unsigned char> diff(width, height, 1, 3, 0);
CImg<unsigned char> output(width, height, 1, 3, 0);
float min_error = 256.0 * 3;
std::list<Point> best_result;
bool flag = false;
int counter = 0;
while(true){
Delaunay_triangulation dt;
// do a delaunay triangulation
for (std::list<Point>::iterator it = points.begin(); it != points.end(); it++)
dt.insert(K::Point_2(it->x(),it->y()));
float error[3] = {0.0};
std::vector<std::pair<std::pair<int, int>, int> > pt_error;
// interpolation
std::cout << "interpolating...";
for (int x = 0; x < width; x++)
{
// simple ASCII progress bar
std::cout << ".";
for (int y = 0; y < height; y++)
{
const Point query(x, y);
double r = 0.0;
double g = 0.0;
double b = 0.0;
std::list<Point>::iterator itc = points.end();
for(std::list<Point>::iterator it = points.begin();
it != points.end();
it++){
if(query.coincide(*it))
itc = it;
}
if(itc != points.end()){
r = (double)input(x, y, 0);
g = (double)input(x, y, 1);
b = (double)input(x, y, 2);
}
else{
K::Point_2 p(x, y);
Point_coordinate_vector coords;
CGAL::Triple<std::back_insert_iterator<Point_coordinate_vector>,
K::FT, bool> result =
CGAL::natural_neighbor_coordinates_2(dt, p,
std::back_inserter(coords));
for(int i = 0; i < coords.size(); i++){
const double w = coords[i].second / result.second;
const int xp = coords[i].first.x();
const int yp = coords[i].first.y();
r += w * (double)input(xp, yp, 0);
g += w * (double)input(xp, yp, 1);
b += w * (double)input(xp, yp, 2);
}
}
// write pixel color
output.atXY(x, y, 0) = (unsigned char)r;
output.atXY(x, y, 1) = (unsigned char)g;
output.atXY(x, y, 2) = (unsigned char)b;
// write error image
int err_r = abs(r - input(x, y, 0));
int err_g = abs(g - input(x, y, 1));
int err_b = abs(b - input(x, y, 2));
pt_error.push_back(std::make_pair(std::make_pair(x, y), err_r + err_g + err_b));
error[0] += err_r;
error[1] += err_g;
error[2] += err_b;
diff.atXY(x, y, 0) = (unsigned char)err_r;
diff.atXY(x, y, 1) = (unsigned char)err_g;
diff.atXY(x, y, 2) = (unsigned char)err_b;
}
}
std::cout << "done" << std::endl;
error[0] = error[0] / test_num;
error[1] = error[1] / test_num;
error[2] = error[2] / test_num;
std::cout << "The average error in R channel: " << error[0] << std::endl;
std::cout << "The average error in G channel: " << error[1] << std::endl;
std::cout << "The average error in B channel: " << error[2] << std::endl;
myfile << error[0] << " " << error[1] << " " << error[2] << " ";
if(flag == true) break;
// early stop
if(error[0] + error[1] + error[2] < min_error){
counter = 0;
min_error = error[0] + error[1] + error[2];
best_result = points;
std::sort(pt_error.begin(), pt_error.end(), pair_cmp);
p_error = error[0] + error[1] + error[2];
for(int i = 0; i < nb_points / 100.0; i++){
points.pop_front();
points.push_back(Point(pt_error[i].first.first, pt_error[i].first.second));
}
points.push_front(Point(0.0, 0.0));
points.push_front(Point(width - 1.0, 0.0));
points.push_front(Point(0.0, height - 1.0));
points.push_front(Point(width - 1.0, height - 1.0));
}
else if(counter < 10){
counter++;
std::sort(pt_error.begin(), pt_error.end(), pair_cmp);
p_error = error[0] + error[1] + error[2];
for(int i = 0; i < nb_points / 100.0; i++){
points.pop_front();
points.push_back(Point(pt_error[i].first.first, pt_error[i].first.second));
}
points.push_front(Point(0.0, 0.0));
points.push_front(Point(width - 1.0, 0.0));
points.push_front(Point(0.0, height - 1.0));
points.push_front(Point(width - 1.0, height - 1.0));
}
else{
points = best_result;
flag = true;
}
}
myfile.close();
std::cout << "write image...";
output.save_bmp(pOutput);
diff.save_bmp("diff.bmp");
std::cout << "done" << std::endl << std::endl;
std::cout << "use python to draw figure..." << std::endl;
FILE * fp = NULL;
fp = fopen("/home/zt/Maillage/sibson_interpolation/draw_figure.py", "r");
Py_Initialize();
PyRun_SimpleFile(fp, "/home/zt/Maillage/sibson_interpolation/draw_figure.py");
Py_Finalize();
return true;
}
};