-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImagetree_lib.cpp
497 lines (453 loc) · 15.9 KB
/
Imagetree_lib.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
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
#include "Imagetree_lib.h"
#include "Imagetree.h"
Imagetree *calculateQuadtreeBasedOnMask(Mat *mask, Mat *image,
Imagetree *itree) {
Mat *temp_mask = mask;
int depth = 0;
return insert(mask, image, itree, depth);
}
Imagetree *insert(Mat *mask, Mat *image, Imagetree *itree, int depth) {
int WIDTH = mask->cols;
int HEIGHT = mask->rows;
int HWIDTH = mask->cols / 2;
int HHEIGHT = mask->rows / 2;
vector<double> sum = {0, 0, 0, 0};
#if DEBUG
cout << "Quad WIDTH: " << HWIDTH << " Quad Height: " << HHEIGHT << endl;
#endif
int x = 0;
int y = 0;
cv::Rect NW(0, 0, HWIDTH, HHEIGHT);
for (int i = x; i < x + HHEIGHT; ++i) {
for (int j = y; j < y + HWIDTH; ++j) {
if (mask->at<uchar>(i, j) == 255) ++sum[0];
}
}
#if DEBUG
cout << "Sum [0]:" << sum[0] << " x: " << x << " y: " << y << endl;
#endif
/*****************************************************************/
x = 0;
y = HWIDTH;
cv::Rect NE(mask->cols / 2, 0, mask->cols / 2, mask->rows / 2);
for (int i = x; i < x + HHEIGHT; ++i) {
for (int j = y; j < y + HWIDTH; ++j) {
if (mask->at<uchar>(i, j) == 255) ++sum[1];
}
}
#if DEBUG
cout << "Sum [1]:" << sum[1] << " x: " << x << " y: " << y << endl;
#endif
x = HHEIGHT;
y = HWIDTH;
cv::Rect SE(mask->rows / 2, mask->cols / 2, mask->rows / 2, mask->cols / 2);
for (int i = x; i < x + HHEIGHT; ++i) {
for (int j = y; j < y + HWIDTH; ++j) {
if (mask->at<uchar>(i, j) == 255) ++sum[2];
}
}
#if DEBUG
cout << "Sum [2]:" << sum[2] << " x: " << x << " y: " << y << endl;
#endif
x = HHEIGHT;
y = 0;
Rect SW(0, mask->rows / 2, mask->cols / 2, mask->rows / 2);
for (int i = x; i < x + HHEIGHT; ++i) {
for (int j = y; j < y + HWIDTH; ++j) {
if (mask->at<uchar>(i, j) == 255) ++sum[3];
}
}
#if DEBUG
cout << "Sum [3]:" << sum[3] << " x: " << x << " y: " << y << endl;
#endif
#if DEBUG
print(sum);
#endif
cv::Mat ImageNW(*image, NW);
cv::Mat MaskNW(*mask, NW);
cv::Mat ImageNE(*image, NE);
cv::Mat MaskNE(*mask, NE);
cv::Mat ImageSE(*image, SE);
cv::Mat MaskSE(*mask, SE);
cv::Mat ImageSW(*image, SW);
cv::Mat MaskSW(*mask, SW);
#if DEBUGCAM
// imshow("NW", ImageNW);
// waitKey();
// imshow("NE", ImageNE);
// waitKey();
// imshow("SE", ImageSE);
// waitKey();
// imshow("SW", ImageSW);
// waitKey();
#endif
if (sum[0] > 1) {
// imshow("NW", ImageNW);
// auto charCheckForEscKey = cv::waitKey(1);
auto tmp_ptr = new QuadNode(0, 0, 0, 0);
auto avg_pixel = calculate_average_pixel(&ImageNW);
tmp_ptr->value() = Pixel(avg_pixel[0], avg_pixel[1], avg_pixel[2]);
itree->son(NorthWest) = tmp_ptr;
insert(&MaskNW, &ImageNW, itree->son(NorthWest), depth + 1);
} else if (sum[0] == 1) {
if ((ImageNW.rows > 1) || (ImageNW.cols > 1)) {
auto tmp_ptr = new QuadNode(0, 0, 0, 0);
auto avg_pixel = calculate_average_pixel(&ImageNW);
tmp_ptr->value() = Pixel(avg_pixel[0], avg_pixel[1], avg_pixel[2]);
itree->son(NorthWest) = tmp_ptr;
insert(&MaskNW, &ImageNW, itree->son(NorthWest), depth + 1);
} else {
auto curr_pixel = ImageNW.at<Vec3b>(0, 0);
auto tmp_ptr =
new QuadLeaf(Pixel(curr_pixel[0], curr_pixel[1], curr_pixel[2]));
itree->son(NorthWest) = tmp_ptr;
#if DEBUG
cout << "Current Depth in NW is: " << depth << endl;
cout << "Current pixel value: " << curr_pixel << "Size: " << ImageNW.rows
<< " " << ImageNW.cols << endl;
#endif
}
} else {
#if DEBUG
cout << "Reached Null in NW at depth" << depth << endl;
#endif
auto curr_pixel = calculate_average_pixel(&ImageNW);
auto tmp_ptr =
new QuadLeaf(Pixel(curr_pixel[0], curr_pixel[1], curr_pixel[2]));
itree->son(NorthWest) = tmp_ptr;
// return;
}
if (sum[1] > 1) {
// imshow("NE", ImageNE);
// auto charCheckForEscKey = cv::waitKey(1);
auto tmp_ptr = new QuadNode(0, 0, 0, 0);
auto avg_pixel = calculate_average_pixel(&ImageNE);
tmp_ptr->value() = Pixel(avg_pixel[0], avg_pixel[1], avg_pixel[2]);
itree->son(NorthEast) = tmp_ptr;
insert(&MaskNE, &ImageNE, itree->son(NorthEast), depth + 1);
} else if (sum[1] == 1) {
if ((ImageNE.rows > 1) || (ImageNE.cols > 1)) {
auto tmp_ptr = new QuadNode(0, 0, 0, 0);
auto avg_pixel = calculate_average_pixel(&ImageNE);
tmp_ptr->value() = Pixel(avg_pixel[0], avg_pixel[1], avg_pixel[2]);
itree->son(NorthEast) = tmp_ptr;
insert(&MaskNE, &ImageNE, itree->son(NorthEast), depth + 1);
} else {
auto curr_pixel = ImageNE.at<Vec3b>(0, 0);
auto tmp_ptr =
new QuadLeaf(Pixel(curr_pixel[0], curr_pixel[1], curr_pixel[2]));
itree->son(NorthEast) = tmp_ptr;
#if DEBUG
cout << "Current Depth in NE is: " << depth << endl;
cout << "Current pixel value: " << curr_pixel << "Size: " << ImageNE.rows
<< " " << ImageNE.cols << endl;
#endif
}
} else {
auto curr_pixel = calculate_average_pixel(&ImageNE);
#if DEBUG
cout << "Reached Null in NE at depth" << depth << endl;
cout << "Current Pixel: " << curr_pixel << endl;
#endif
auto tmp_ptr =
new QuadLeaf(Pixel(curr_pixel[0], curr_pixel[1], curr_pixel[2]));
itree->son(NorthEast) = tmp_ptr;
}
if (sum[2] > 1) {
// imshw("SE", ImageSE);
// auto charCheckForEscKey = cv::waitKey(1);
auto tmp_ptr = new QuadNode(0, 0, 0, 0);
auto avg_pixel = calculate_average_pixel(&ImageSE);
tmp_ptr->value() = Pixel(avg_pixel[0], avg_pixel[1], avg_pixel[2]);
itree->son(SouthEast) = tmp_ptr;
insert(&MaskSE, &ImageSE, itree->son(SouthEast), depth + 1);
} else if (sum[2] == 1) {
if ((ImageSE.rows > 1) || (ImageSE.cols > 1)) {
auto tmp_ptr = new QuadNode(0, 0, 0, 0);
auto avg_pixel = calculate_average_pixel(&ImageSE);
tmp_ptr->value() = Pixel(avg_pixel[0], avg_pixel[1], avg_pixel[2]);
itree->son(SouthEast) = tmp_ptr;
insert(&MaskSE, &ImageSE, itree->son(SouthEast), depth + 1);
} else {
auto curr_pixel = ImageSE.at<Vec3b>(0, 0);
auto tmp_ptr =
new QuadLeaf(Pixel(curr_pixel[0], curr_pixel[1], curr_pixel[2]));
itree->son(SouthEast) = tmp_ptr;
#if DEBUG
cout << "Current Depth in SE is" << depth << endl;
cout << "Current pixel value" << curr_pixel << "Size: " << ImageSE.rows
<< " " << ImageSE.cols << endl;
#endif
}
} else {
auto curr_pixel = calculate_average_pixel(&ImageSE);
#if DEBUG
cout << "Reached Null in SE at depth" << depth << endl;
cout << "Current Pixel: " << curr_pixel << endl;
#endif
auto tmp_ptr =
new QuadLeaf(Pixel(curr_pixel[0], curr_pixel[1], curr_pixel[2]));
itree->son(SouthEast) = tmp_ptr;
}
if ((sum[3] > 1)) {
// imshow("SW", ImageSW);
// auto charCheckForEscKey = cv::waitKey(1);
auto tmp_ptr = new QuadNode(0, 0, 0, 0);
auto avg_pixel = calculate_average_pixel(&ImageSW);
tmp_ptr->value() = Pixel(avg_pixel[0], avg_pixel[1], avg_pixel[2]);
itree->son(SouthWest) = tmp_ptr;
insert(&MaskSW, &ImageSW, itree->son(SouthWest), depth + 1);
} else if (sum[3] == 1) {
if ((ImageSW.rows > 1) || (ImageSW.cols > 1)) {
auto tmp_ptr = new QuadNode(0, 0, 0, 0);
auto avg_pixel = calculate_average_pixel(&ImageSW);
tmp_ptr->value() = Pixel(avg_pixel[0], avg_pixel[1], avg_pixel[2]);
itree->son(SouthWest) = tmp_ptr;
insert(&MaskSW, &ImageSW, itree->son(SouthWest), depth + 1);
} else {
auto curr_pixel = ImageSW.at<Vec3b>(0, 0);
auto tmp_ptr =
new QuadLeaf(Pixel(curr_pixel[0], curr_pixel[1], curr_pixel[2]));
itree->son(SouthWest) = tmp_ptr;
#if DEBUG
cout << "Current Depth in SW is" << depth << endl;
cout << "Current pixel value" << curr_pixel << "Size: " << ImageSW.rows
<< " " << ImageSW.cols << endl;
#endif
}
} else {
auto curr_pixel = calculate_average_pixel(&ImageSW);
auto tmp_ptr =
new QuadLeaf(Pixel(curr_pixel[0], curr_pixel[1], curr_pixel[2]));
#if DEBUG
cout << "Reached Null in SW at depth" << depth << endl;
cout << "Current Pixel: " << curr_pixel << endl;
#endif
itree->son(SouthWest) = tmp_ptr;
}
return itree;
}
void print(vector<double> &v) {
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
cout << endl;
}
// #if DEBUGCAM
// int x = 0;
// #endif
Vec3b calculate_average_pixel(cv::Mat *image) {
vector<double> sum(3);
sum[0] = 0;
sum[1] = 0;
sum[2] = 0;
// #if DEBUGCAM
// imshow(to_string(++x), *image);
// auto charCheckForEscKey = cv::waitKey(1000);
// #endif
int total_values = 0;
for (int i = 0; i < (image->rows); ++i) {
for (int j = 0; j < (image->cols); ++j) {
auto pixel = image->at<Vec3b>(i, j);
sum[0] += pixel[0];
sum[1] += pixel[1];
sum[2] += pixel[2];
++total_values;
}
}
sum[0] = sum[0] / total_values;
sum[1] = sum[1] / total_values;
sum[2] = sum[2] / total_values;
Vec3b final_sum;
final_sum[0] = sum[0];
final_sum[1] = sum[1];
final_sum[2] = sum[2];
return final_sum;
}
void clear_the_pixVector(vector<PixelDepthQuad *> &pixVectors) {
for (int i = 0; i < pixVectors.size(); ++i) {
delete pixVectors[i];
}
}
double calculate_RMSE(Mat *original, Mat *reconstructed) {
double error = 0;
// #if DEBUGCAM
// imshow(to_string(++x), *image);
// auto charCheckForEscKey = cv::waitKey(1000);
// #endif
int total_values = 0;
// calculate the
for (int i = 0; i < (reconstructed->rows); ++i) {
for (int j = 0; j < (reconstructed->cols); ++j) {
auto pixel = reconstructed->at<Vec3b>(i, j) - original->at<Vec3b>(i, j);
error += pixel[0] * pixel[0] + pixel[1] * pixel[1] + pixel[2] * pixel[2];
}
}
error = error / (reconstructed->rows * reconstructed->cols);
return error;
}
double calculate_average_pixel_RMSE(Mat *original, Pixel &p_avg) {
double error = 0;
// #if DEBUGCAM
// imshow(to_string(++x), *image);
// auto charCheckForEscKey = cv::waitKey(1000);
// #endif
int total_values = 0;
// calculate the
for (int i = 0; i < (original->rows); ++i) {
for (int j = 0; j < (original->cols); ++j) {
vector<int> pixel = {p_avg.R - original->at<Vec3b>(i, j)[0],
p_avg.G - original->at<Vec3b>(i, j)[1],
p_avg.B - -original->at<Vec3b>(i, j)[2]};
error += pixel[0] * pixel[0] + pixel[1] * pixel[1] + pixel[2] * pixel[2];
}
}
error = error / (original->rows * original->cols);
return error;
}
double calculate_average_pixel_RMSE_vec3b(Mat *original, Vec3b &p_avg) {
double error = 0;
// #if DEBUGCAM
// imshow(to_string(++x), *image);
// auto charCheckForEscKey = cv::waitKey(1000);
// #endif
int total_values = 0;
// calculate the
for (int i = 0; i < (original->rows); ++i) {
for (int j = 0; j < (original->cols); ++j) {
auto pixel = p_avg - original->at<Vec3b>(i, j);
error += pixel[0] * pixel[0] + pixel[1] * pixel[1] + pixel[2] * pixel[2];
}
}
error = error / (original->rows * original->cols);
return error;
}
void construct_RMSE_LinearQuadtree(vector<PixelDepthQuad *> &pixVectors,
Mat *imOriginal) {
// get the number of levels
int DEPTH = calculate_max_depth_of_quadtree(imOriginal);
vector<Mat> vec_mat;
// calculate the average pixel value
// Add the first PixelDepthQuad for representing the entire image
auto pix_whole = calculate_average_pixel(imOriginal);
// storing the first value of the pixVector storing the average pixel of the
// entire value
PixelDepthQuad root{Pixel{pix_whole[0], pix_whole[1], pix_whole[2], 0, 0}, 0,
0, 0, 0};
pixVectors.push_back(&root);
// calculate the RMSE
auto rmse = calculate_average_pixel_RMSE(imOriginal, pixVectors[0]->P);
cout << rmse << " is the rmse error" << endl;
// if the RMSE is greater than the required keep subdividing
int WIDTH = imOriginal->cols;
int HEIGHT = imOriginal->rows;
// int HWIDTH = imOriginal->cols / 2;
// int HHEIGHT = imOriginal->rows / 2;
// temporarily storing the image quadrants until I come up with a differrent
// logic
vec_mat.push_back(*imOriginal);
// storing the matrix index: here, it is set to one becasue an object is
// already pushed.
int matrix_index = 0;
int rex = 20;
// while ((rmse > 0.01) && (rex--)) {
while ((rmse > 0.01) ) {
auto my_current_index = matrix_index;
// The current quadrant that is being inspected is given by the matrix index
auto HHEIGHT = vec_mat[matrix_index].cols/2;
auto HWIDTH = vec_mat[matrix_index].rows/2;
auto depth = (WIDTH/HWIDTH) - 1 ;
// divide into four quadrants, calculate the average pixel and subdivide
// further if necessary.
vector<Vec3b> avgPixQ(4);
cout << "Matrix index: " << matrix_index << " pixVector Size"
<< pixVectors.size() << endl;
cout << "Half width: " << HWIDTH << " Half Height" << HHEIGHT << endl;
Rect NW(0, 0, HWIDTH, HHEIGHT);
Rect NE(0, HWIDTH, HWIDTH, HHEIGHT);
Rect SE(HHEIGHT, 0, HWIDTH, HHEIGHT);
Rect SW(HHEIGHT, HWIDTH, HWIDTH, HHEIGHT);
Mat quadImages[4];
quadImages[0] = Mat(vec_mat[matrix_index], NW);
quadImages[1] = Mat(vec_mat[matrix_index], NE);
quadImages[2] = Mat(vec_mat[matrix_index], SE);
quadImages[3] = Mat(vec_mat[matrix_index], SW);
rmse = 0;
PixelDepthQuad *temp;
for (int i = 0; i < 4; ++i) {
int x, y;
// Choosing the quadrant
switch (i) {
case 0: x = 0; y = 1; break;
case 1: x = 1; y = 1; break;
case 2: x = 1; y = 0; break;
case 3: x = 0; y = 0; break;
}
vec_mat.push_back(quadImages[i]);
avgPixQ[i] = calculate_average_pixel(&quadImages[i]);
// add the four quadrants to the pixvector table
PixelDepthQuad *my_parent = pixVectors[my_current_index];
// ,
temp = new PixelDepthQuad{
Pixel{avgPixQ[i][0], avgPixQ[i][1], avgPixQ[i][2],
my_parent->P.x + x * HWIDTH, my_parent->P.y + y * HHEIGHT},
depth, i, 0, pixVectors[my_current_index]};
pixVectors.push_back(temp);
int ind = pixVectors.size() - 1;
rmse += calculate_average_pixel_RMSE_vec3b(&quadImages[i], avgPixQ[i]);
cout << pixVectors[i]->P;
cout << "\tQ:" << pixVectors[ind]->quad
<< "\tI:" << pixVectors[ind]->depth << "\tRMSE: " << rmse
<< "\t Parent: " << pixVectors[i]->parentPixelDepth
<< "\tMe:" << pixVectors[i] << endl;
}
// HWIDTH /= 2;
// HHEIGHT /= 2;
// ++depth;
cout << "half height: " << HHEIGHT << " depth: " << depth
<< " rmse: " << rmse << " vector size: " << vec_mat.size() << "\n\n"
<< endl;
++matrix_index;
}
}
int calculate_max_depth_of_quadtree(Mat *img) {
auto height = img->rows;
int depth = 0;
while (height) {
++depth;
height /= 2;
}
return depth;
}
void reconstructImageFromTree(vector<PixelDepthQuad *> &pixVector,
Mat *reconImage) {
// for (int i = 30; i >= 1; --i) {
int i;
for (i = 1; i < pixVector.size(); ++i) {
// if (pixVector[i]->isLeaf) {
int factor = pow(2, pixVector[i]->depth);
// #ifdef DEBUG
// std::cout << "i: " << i << " Factor: " << factor
// << " Depth: " << pixVector[i]->depth
// << " Color: " << pixVector[i]->P;
// std::cout << " pixVector's x and y: (" << pixVector[i]->P.x << ","
// << pixVector[i]->P.y << ")\n";
// #endif
for (int row = pixVector[i]->P.x;
row <
pixVector[i]->P.x + (reconImage->rows / pow(2, pixVector[i]->depth));
++row) {
for (int col = pixVector[i]->P.y;
col <
pixVector[i]->P.y + (reconImage->cols / pow(2, pixVector[i]->depth));
++col) {
reconImage->at<Vec3b>(row, col) =
Vec3b(pixVector[i]->P.R, pixVector[i]->P.G, pixVector[i]->P.B);
// if(i==2) cout<<reconImage->at<Vec3b>(row, col)<<"\n ";
}
}
//}
// cv::imshow("Loop Image", *reconImage);
// waitKey(3);
}
}