-
Notifications
You must be signed in to change notification settings - Fork 1
/
local_faldoi.cpp
executable file
·2112 lines (1865 loc) · 92.4 KB
/
local_faldoi.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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This program is free software: you can use, modify and/or redistribute it
// under the terms of the simplified BSD License. You should have received a
// copy of this license along this program. If not, see
// <http://www.opensource.org/licenses/bsd-license.html>.
//
// Copyright (C) 2014, Roberto P.Palomares <r.perezpalomares@gmail.com>
// Copyright (C) 2017, Onofre Martorell <onofremartorelln@gmail.com>
// Copyright (C) 2018, Ferran Pérez <fperez.gamonal@gmail.com>
// All rights reserved.
#ifndef LOCAL_FALDOI
#define LOCAL_FALDOI
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <random>
#include <future>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include "energy_structures.h"
#include "energy_model.h"
#include "utils_preprocess.h"
#include "aux_partitions.h"
extern "C" {
#include "iio.h"
#include "bicubic_interpolation.h"
#include "elap_recsep.h"
}
#include <omp.h>
#include <iostream>
#include <fstream>
#include <string>
#include "utils.h"
#include "parameters.h"
#include <ctime>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////OUTLIERS FUNCTIONS/////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* @brief Returns a sample of a 3D array with dimensions w x h x pd (with boundary checking)
*
* @param x input array, the one to be sampled at the specified location
* @param w width of the input array 'x'
* @param h height of the input array 'x'
* @param pd number of channels (depth) of the input array 'x'
* @param i column' sample index corresponding to the first dimension
* @param j row' sample index corresponding to the second dimension
* @param l channel' sample index corresponding to the third dimension
* @return returns the array value sampled at the targeted coordinate
*/
static float getsample_inf(float *x, int w, int h, int pd, int i, int j, int l) {
if (i < 0 || i >= w || j < 0 || j >= h || l < 0 || l >= pd)
return INFINITY;
return x[(i + j * w) * pd + l];
}
/**
* @brief Checks if the patch values are too uniform to pass the consistency check
*
* @param a the input array with the patch's values
* @param tol the threshold that defines the uniformity
* @param i the column index of the pixel that is being analysed
* @param j the row index of the pixel that is being analysed
* @param w width of the image where 'a' is sampled from
* @param h height of the image where 'a' is sampled from
* @param pd depth of the image where 'a' is sampled from
* @return returns '1' if the patch values are too uniform; '0' otherwise.
*/
static int too_uniform(float *a, float tol, int i, int j, int w, int h, int pd) {
float difference = 0;
int neighbours[4][2] = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0}};
for (int l = 0; l < pd; l++) {
float center = getsample_inf(a, w, h, pd, i, j, l);
for (int k = 0; k < 4; k++) {
int px = i + neighbours[k][0];
int py = j + neighbours[k][1];
float neighborhood = getsample_inf(a, w, h, pd, px, py, l);
if (isfinite(center) && isfinite(neighborhood)) {
float tmp = abs(neighborhood - center);
//printf("Tmp: %f, Tol: %f neighborhood: %f Center: %f\n", tmp, difference, neighborhood, center);
if (difference < tmp) {
difference = tmp;
}
}
}
}
if (difference < tol) {
return 1;
}
return 0;
}
/**
* @brief Checks if the areas within the frames are too uniform or not
*
* @param a source frame (fwd: 'i0' at time 't', bwd: 'i1' at time 't+1')
* @param b second frame (fwd: 'i1' at time 't+1', bwd: 'i0' at time 't')
* @param in0 input flow vector
* @param trust_in0 array to be filled with 1's or 0's depending on the return value of 'too_uniform'
* @param w width of the input frames
* @param h height of the input frames
* @param tol threshold that defines the uniformity
*
* @sa too_uniform, pruning_method
*/
void too_uniform_areas(float *a, float *b, float *in0, int *trust_in0, int w, int h, float tol)
{
auto *bw = new float[w * h];
int size = w * h;
int n = 0;
bicubic_interpolation_warp(b, in0, in0 + size, bw, w, h, true);
for (int j = 0; j < h; j++)
for (int i = 0; i < w; i++) {
//If both areas present too uniform pixels, remove the flow.
if ((too_uniform(a, tol, i, j, w, h, 1) == 1) || (too_uniform(bw, tol, i, j, w, h, 1) == 1)) {
trust_in0[j * w + i] = 0;
} else {
trust_in0[j * w + i] = 1;
n++;
}
}
printf("Too-chosen: %f\n", (n * 1.0) / size);
delete[] bw;
}
/**
* @brief Check forward-backward consistency check for optical flow |u(x) + v(x+u(x))| < eps.
* @details Energy map related to the pixels that do not pass the check are put to INFINITY.
*
* @param in0 array containing the forward flow (in time)
* @param in1 array containing the backward flow (in time)
* @param trust_in0 array to be filled with 1's or 0's depending on whether the flow passes the check or not
* @param w width of the input flow fields
* @param h height of the input flow fields
* @param epsilon threshold that defines the maximum difference between backward and forward flows (consistency)
*
* @sa pruning_method
*/
void fb_consistency_check(float *in0, float *in1, int *trust_in0, int w, int h, float epsilon)
{
auto *u1w = new float[w * h];
auto *u2w = new float[w * h];
int size = w * h;
int n = 0;
bicubic_interpolation_warp(in1, in0, in0 + size, u1w, w, h, true);
bicubic_interpolation_warp(in1 + w * h, in0, in0 + size, u2w, w, h, true);
for (int i = 0; i < size; i++) {
float tolerance = hypotf(in0[i] + u1w[i], in0[size + i] + u2w[i]);
if (tolerance > epsilon) {
//(tolerance > epsilon) means pixel is occluded
trust_in0[i] = 0;
} else {
trust_in0[i] = 1;
n++;
}
}
printf("FB-Chosen: %f\n", (n * 1.0) / size);
delete[] u2w;
delete[] u1w;
}
/**
* @brief Calls the selected pruning method(s) ('fb-consistency' and/or 'too_uniform')
*
* @param i0 source frame (fwd: 'i0' at time 't', bwd: 'i1' at time 't+1')
* @param i1 second frame (fwd: 'i1' at time 't+1', bwd: 'i0' at time 't')
* @param w width of the input frames
* @param h height of the input frames
* @param tol array that contains the tolerances for both pruning methods
* @param method defines which method(s) will be used; method[0]=1: 'fb-consistency', method[1]=1: 'too_uniform'
* @param trust_Go array to be filled with the pruning decisions for each pixel (forward flow)
* @param go forward flow field
* @param trust_Ba array to be filled with the pruning decisions for each pixel (backward flow)
* @param ba backward flow field
*
* @sa fb_consistency_check, too_uniform_areas
*/
void pruning_method(float *i0, float *i1, int w, int h, float *tol, const int *method, int *trust_Go, float *go,
int *trust_Ba, float *ba)
{
int *go_fb_check = new int[w*h];
int *go_cons_check = new int[w*h];
int *ba_fb_check = new int[w*h];
int *ba_cons_check = new int[w*h];
for (int i = 0; i < w*h; i++)
{
//0 - Invalid pixel 1 - Trustable pixel.
trust_Go[i] = 1;
trust_Ba[i] = 1;
}
//FB - consistency check
if (method[0]==1)
{
std::printf("FB-Consistency: %f\n",tol[0]);
fb_consistency_check(go, ba, go_fb_check, w, h, tol[0]);
fb_consistency_check(ba, go, ba_fb_check, w, h, tol[0]);
}
//Too-uniform consistency check
if (method[1]==1)
{
std::printf("Too Uniform -Consistency:%f\n",tol[1]);
too_uniform_areas(i0, i1, go, go_cons_check, w, h, tol[1]);
too_uniform_areas(i0, i1, ba, ba_cons_check, w, h, tol[1]);
}
for (int i = 0; i < w*h; i++){
if (method[0] == 1)
{
//FB-Consistency
if (go_fb_check[i] == 0)
{
trust_Go[i] = 0;
}
if (ba_fb_check[i] == 0)
{
trust_Ba[i] = 0;
}
}
//Too uniform -Consistency
if (method[1] == 1)
{
if (go_cons_check[i] == 0)
{
trust_Go[i] = 0;
}
if (ba_cons_check[i] == 0)
{
trust_Ba[i] = 0;
}
}
}
delete [] go_fb_check;
delete [] go_cons_check;
delete [] ba_fb_check;
delete [] ba_cons_check;
}
/**
* @brief deletes non-trustable candidates by setting their flow to NAN and its energy to INF
* @details uses the pruning decisions returned by 'pruning_method' to choose what candidates to delete
*
* @param ofD optical flow data struct containing the flow fields and other useful information
* @param in output optical flow field to be updated accordingly
* @param ene_val energy values to be updated accordingly
* @param w width of the input flow field
* @param h height of the input flow field
*/
void delete_not_trustable_candidates(OpticalFlowData *ofD, float *in, float *ene_val, const int w, const int h)
{
int *mask = ofD->trust_points;
float *u1 = ofD->u1;
float *u2 = ofD->u2;
float *chi = ofD->chi;
int n = 0;
for (int i = 0; i < w*h; i++)
{
if (mask[i] == 0)
{
//printf("%f\n", ene_val[i]);
if (ene_val[i]==0.0)
{
n++;
}
in[i] = NAN;
in[i + w*h] = NAN;
u1[i] = NAN;
u2[i] = NAN;
ene_val[i] = INFINITY;
// If the flow is non trustable, is considered
// to be an occlusion
chi[i] = 1;
}
}
printf("Total seeds: %d\n", n);
}
////////////////////////////////////////////////////////////////////////////////
//////////////////LOCAL INITIALIZATION//////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Poisson Interpolation
/**
* @brief performs the poisson interpolation for the optical flow values in the 'patch'
*
* @param ofD OpticalFlowData struct containing the updated optical flow fields
* @param patch patch that is currently being analysed
*/
void interpolate_poisson(OpticalFlowData *ofD, const PatchIndexes &patch, const int w_src)
{
int w = patch.ei - patch.ii;
int h = patch.ej - patch.ij;
int *fixed_points = ofD->fixed_points;
int wR = w_src; //ofD->params.w;
float *u1 = ofD->u1;
float *u2 = ofD->u2;
float buf_in[2 * MAX_PATCH * MAX_PATCH];
float buf_out[2 * MAX_PATCH * MAX_PATCH];
assert(w * h < MAX_PATCH * MAX_PATCH);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int x = i + patch.ii;
int y = j + patch.ij;
int xy = y * wR + x;
// 1 fixed - 0 not
if (fixed_points[xy] == 1) {
buf_in[j * w + i] = u1[xy];
buf_in[j * w + i + w * h] = u2[xy];
} else { // not fixed
buf_in[j * w + i] = NAN;
buf_in[j * w + i + w * h] = NAN;
}
}
}
elap_recursive_separable(buf_out, buf_in, w, h, 2, 0.4, 3, 7);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int x = i + patch.ii;
int y = j + patch.ij;
int xy = y * wR + x;
u1[xy] = buf_out[j * w + i];
u2[xy] = buf_out[j * w + i + w * h];
}
}
}
/**
* @brief obtains undefined (not fixed) optical flow values by applying a bilateral filter on the patch
*
* @param ofD OpticalFlowData struct containing the updated optical flow fields
* @param BiFilt BilateralFilterData struct containing the values of the filter on the patch
* @param patch patch that is currently being analysed
* @param w width of the optical flow data being processed (to img_width or partition_width if parallelizing)
* @param h height of the optical flow data being processed (to img_height or partition_height if parallelizing)
*/
void bilateral_filter(OpticalFlowData *ofD, BilateralFilterData *BiFilt, const PatchIndexes &patch, const int w,
const int h)
{
int *trust_points = ofD->trust_points;
int *fixed_points = ofD->fixed_points;
const int w_patch = patch.ei - patch.ii;
const int h_patch = patch.ej - patch.ij;
const int wr_patch = ofD->params.w_radio;
const int wr_filter = PATCH_BILATERAL_FILTER;
const int wr = wr_filter + wr_patch;
float *u1 = ofD->u1;
float *u2 = ofD->u2;
float *u1_filter = ofD->u1_filter;
float *u2_filter = ofD->u2_filter;
PatchIndexes area_interp = get_index_patch(wr, w, h, patch.i, patch.j, 1);
// Copy all flow values to minimize and surroundings
for (int j = 0; j < area_interp.ej - area_interp.ij; j++) {
for (int i = 0; i < area_interp.ei - area_interp.ii; i++) {
// Coordinates of pixel over whole image
const int x = i + area_interp.ii;
const int y = j + area_interp.ij;
const int xy = y * w + x;
// Initialize flow in patch for filtering
// We use values of trust points and fixed points
if (trust_points[xy] == 1 || fixed_points[xy] == 1) {
u1_filter[xy] = u1[xy];
u2_filter[xy] = u2[xy];
} else {
u1_filter[xy] = 0.0;
u2_filter[xy] = 0.0;
}
}
}
// For each pixel in the patch non trustable, do bilateral filtering
int iter = ITER_BILATERAL_FILTER;
for (int it = 0; it < iter; it++) {
for (int j = 0; j < h_patch; j++) {
for (int i = 0; i < w_patch; i++) {
// Coordinates of pixel over whole image
int x = i + patch.ii;
int y = j + patch.ij;
int xy = y * w + x;
// If pixel has not survived the previous prunning or
// if it has not been fixed, we make interpolation
if (trust_points[xy] == 0 && fixed_points[xy] == 0) {
// Index of points around ij
const PatchIndexes index_interp = BiFilt->indexes_filtering[xy];
// Variable that contains the precalculated weights
const float *weights = BiFilt->weights_filtering[xy].weight;
const int w_neighbor = index_interp.ei - index_interp.ii;
const int h_neighbor = index_interp.ej - index_interp.ij;
float numerator_u1 = 0.0;
float numerator_u2 = 0.0;
float denominator = 0.0;
for (int idx_j = 0; idx_j < h_neighbor; idx_j++) {
for (int idx_i = 0; idx_i < w_neighbor; idx_i++) {
const int idx_x = idx_i + index_interp.ii;
const int idx_y = idx_j + index_interp.ij;
const int idx_xy = idx_y * w + idx_x;
const int idx_ij = idx_j * w_neighbor + idx_i;
numerator_u1 += u1_filter[idx_xy] * weights[idx_ij];
numerator_u2 += u2_filter[idx_xy] * weights[idx_ij];
denominator += weights[idx_ij];
}
}
const float new_flow_u1 = numerator_u1 / denominator;
const float new_flow_u2 = numerator_u2 / denominator;
u1_filter[i] = new_flow_u1;
u2_filter[i] = new_flow_u2;
}
}
}
}
// Save filtering in flow variable
for (int j = 0; j < h_patch; j++) {
for (int i = 0; i < w_patch; i++) {
int x = i + patch.ii;
int y = j + patch.ij;
int xy = y * w + x;
if (trust_points[xy] == 0 && fixed_points[xy] == 0) {
u1[xy] = u1_filter[xy];
u2[xy] = u2_filter[xy];
}
}
}
}
/**
* @brief Insert n_neigh-connected candidates into the priority queue with their energies.
*
* @param queue priority queue with candidates
* @param ene_val array storing energy values (updated only if the new energy is better/lower)
* @param ofD OpticalFlowData struct containing the updated optical flow fields
* @param i column index of the pixel that is being currently processed
* @param j row index of the pixel that is being currently processed
* @param ener_N auxiliar variable to store the new computed energy (and compare against old one in ene_val)
* @param w width of the optical flow data being processed (to img_width or partition_width if parallelizing)
* @param h height of the optical flow data being processed (to img_height or partition_height if parallelizing)
*/
void insert_candidates(pq_cand &queue, float *ene_val, OpticalFlowData *ofD, const int i, const int j,
const float ener_N, const int w, const int h)
{
int n_neigh = 4;
int neighborhood[8][2] = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0},
{1, 1},
{1, -1},
{-1, 1},
{-1, -1}};
const float *sal = ofD->saliency;
for (int k = 0; k < n_neigh; k++) {
int px = i + neighborhood[k][0];
int py = j + neighborhood[k][1];
if (px >= 0 && px < w && py >= 0 && py < h) {
float new_ener = ener_N * sal[py * w + px];
//printf("Ener_N: %f Sim: %f \n", ener_N, ene_val[py*w + px]);
if (!ofD->fixed_points[py * w + px] && new_ener < ene_val[py * w + px]) {
ene_val[py * w + px] = ener_N;
SparseOF element{};
element.i = px; // column
element.j = py; // row
element.u = ofD->u1[py * w + px];
element.v = ofD->u2[py * w + px];
element.sim_node = new_ener;
if(ofD->params.val_method >= 8) {
element.occluded = ofD->chi[py * w + px];
}
queue.push(element);
}
}
}
}
/**
* @brief returns the relative weights corresponding to the current index i, j
*
* @param iiw column weights
* @param ijw row weights
* @param wr windows radius (patch_size = 2 * wr + 1 in each direction)
* @param i current pixel's column index
* @param j current pixel's row index
*
* @sa get_index_weight
*/
inline void get_relative_index_weight(int *iiw, int *ijw, const int wr, const int i, const int j)
{
(*iiw) = (((i - wr) < 0) ? -(i - wr) : 0);
(*ijw) = (((j - wr) < 0) ? -(j - wr) : 0);
assert(*iiw >= 0);
assert(*ijw >= 0);
}
/**
* @brief save the index weights generated by 'get_relative_index_weight' to the chosen functional struct
*
* @param method functional chosen
* @param ofS SpecificOFStuff struct where the weights should be stored
* @param wr windows radius (patch_size = 2 * wr + 1 in each direction)
* @param i current pixel's column index
* @param j current pixel's row index
*
* @sa get_relative_index_weight
*/
static void get_index_weight(int method, SpecificOFStuff *ofS, const int wr, int i, int j)
{
int iiw, ijw;
if (method == M_TVL1_W || method == M_NLTVCSAD_W || method == M_NLTVL1_W || method == M_TVCSAD_W) {
get_relative_index_weight(&iiw, &ijw, wr, i, j);
}
switch (method) {
case M_TVL1_W:
ofS->tvl2w.iiw = iiw;
ofS->tvl2w.ijw = ijw;
break;
case M_NLTVCSAD_W:
ofS->nltvcsadw.iiw = iiw;
ofS->nltvcsadw.ijw = ijw;
break;
case M_NLTVL1_W:
ofS->nltvl1w.iiw = iiw;
ofS->nltvl1w.ijw = ijw;
break;
case M_TVCSAD_W:
ofS->tvcsadw.iiw = iiw;
ofS->tvcsadw.ijw = ijw;
break;
default:
break;
}
}
/**
* @brief Copy over ofD->u1 and ofD->u2 the presented values in out.
*
* @param ofD OpticalFlowData struct containing the updated optical flow fields
* @param out output flow array with original values
* @param index indices of the patch that is currently being processed
* @param w width of the optical flow data being processed (to img_width or partition_width if parallelizing)
* @param h height of the optical flow data being processed (to img_height or partition_height if parallelizing)
*/
inline void copy_fixed_coordinates(OpticalFlowData *ofD, const float *out, const PatchIndexes &index, const int w,
const int h
) {
float *u1 = ofD->u1;
float *u2 = ofD->u2;
int *fixed = ofD->fixed_points;
for (int l = index.ij; l < index.ej; l++) {
for (int k = index.ii; k < index.ei; k++) {
// Copy only fixed values from the patch
const int i = l * w + k;
if (fixed[i] == 1) {
u1[i] = out[i];
u2[i] = out[w * h + i];
assert(isfinite(u1[i]));
assert(isfinite(u2[i]));
}
}
}
}
/**
* @brief Check if there is at least one pixel that hasn't survived to the prunning.
*
* @param ofD OpticalFlowData struct containing the updated optical flow fields
* @param index indices of the patch that is currently being processed
* @param w width of the optical flow data being processed (to img_width or partition_width if parallelizing)
* @return 1's if the pixel's flow is trustable, 0's otherwise
*/
int check_trustable_patch(
OpticalFlowData *ofD,
const PatchIndexes &index,
const int w
) {
int *fixed = ofD->trust_points;
for (int l = index.ij; l < index.ej; l++)
for (int k = index.ii; k < index.ei; k++) {
// Return 0 if it detects that at least one point it is not fixed
const int i = l * w + k;
// If the pixel it is not trustable.
if (fixed[i] == 0) {
return 0;
}
}
return 1;
}
/**
* @brief adds neigbours of the pixel that is currently being processed to the queue
* @details computes the new energy for the patch and then inserts the candidates that lowered their energy
*
* @param i0 source frame at time 't'
* @param i1 second frame at time 't+1'
* @param i_1 previous frame at time 't-1' (used for occlusions only)
* @param ene_val array that stores the energy values updated (maybe) during the previous candidate's iteration
* @param ofD OpticalFlowData struct containing the updated optical flow fields
* @param ofS SpecificOFStuff struct where functional-specific variables reside
* @param queue priority queue that contains the candidates
* @param i column index of the current pixel being processed
* @param j row index of the current pixel being processed
* @param iteration iteration index of the local minimization step
* @param out array that stores the output flow (maybe) updated during the previous candidate's iteration
* @param out_occ array that stores the occlusions' map (maybe) updated during the previous candidate's iteration
* @param BiFilt struct that contains the indices and weights of the bilateral filter
* @param w width of the optical flow data being processed (to img_width or partition_width if parallelizing)
* @param h height of the optical flow data being processed (to img_height or partition_height if parallelizing)
*/
static void add_neighbors(const float *i0, const float *i1, const float *i_1, float *ene_val, OpticalFlowData *ofD,
SpecificOFStuff *ofS, pq_cand *queue, const int i, const int j, const int iteration,
float *out, float *out_occ, BilateralFilterData *BiFilt, const int w, const int h)
{
const int wr = ofD->params.w_radio;
float ener_N;
const PatchIndexes index = get_index_patch(wr, w, h, i, j, 1);
int method = ofD->params.val_method; // used to include no occ.
// In first iteration, Poisson interpolation
if (iteration == 0) {
// Interpolate by poisson on initialization
copy_fixed_coordinates(ofD, out, index, w, h);
// Poisson Interpolation (4wr x 4wr + 1)
interpolate_poisson(ofD, index, w);
} else if (check_trustable_patch(ofD, index, w) == 0) {
// Interpolate by bilateral filtering if some points do not survive to prunning
//if (check_trustable_patch(ofD, index, w) == 0) {
copy_fixed_coordinates(ofD, out, index, w, h);
interpolate_poisson(ofD, index, w);
// TODO: fix bilateral filter as it is quite faster
//bilateral_filter(ofD, BiFilt, index, w, h); // yields a far worse estimation
// check implementation details
// }
}
// get index's weight (if the functional uses them)
get_index_weight(method, ofS, wr, i, j);
// Optical flow method on patch (2*wr x 2wr + 1)
of_estimation(ofS, ofD, &ener_N, i0, i1, i_1, index, w, h);
// Insert new candidates to the queue
insert_candidates(*queue, ene_val, ofD, i, j, ener_N, w, h);
// It is a strange step, if the energy over the patch is lower thant the
// stored energy, we put the new one, if it's not, we leave the old one.
if (ene_val[j * w + i] > ener_N) {
out[j * w + i] = ofD->u1[j * w + i];
out[w * h + j * w + i] = ofD->u2[j * w + i];
ene_val[j * w + i] = ener_N;
// Only if 'occlusions'
if (method >= 8) {
out_occ[j * w + i] = ofD->chi[j * w + i];
}
}
}
/**
* @brief inserts the initial seeds to the priority queue by using initial flow derived from the sparse matches (SIFT or deepmatching)
* @details initialises to default values: flow to NAN, energy to INF and occlusions to 0 (if it applies)
*
* @param i0 source frame at time 't'
* @param i1 second frame at time 't+1'
* @param i_1 previous frame at time 't-1' (used for occlusions only)
* @param in_flow array that contains the initial flow obtained from the sparse matches
* @param queue priority queue where the initial candidates will be inserted
* @param ofD OpticalFlowData struct with default values
* @param ofS SpecificOFStuff struct with default values
* @param ene_val array that will store the energy of all pixels in the image
* @param out_flow array that will store the optical flow values of all pixels in the image
* @param out_occ array that will store the occlusion map for the image
* @param BiFilt struct that contains the indices and weights of the bilateral filter
* @param w width of the optical flow data being processed
* @param h height of the optical flow data being processed
*/
void insert_initial_seeds(const float *i0, const float *i1, const float *i_1, float *in_flow, pq_cand *queue,
OpticalFlowData *ofD, SpecificOFStuff *ofS, float *ene_val, float *out_flow, float *out_occ,
BilateralFilterData *BiFilt, const int w, const int h)
{
int wr = ofD->params.w_radio;
//Set to the initial conditions all the stuff
for (int i = 0; i < w*h; i++)
{
ofD->fixed_points[i] = 0;
ene_val[i] = INFINITY;
out_flow[i] = NAN;
out_flow[w*h + i] = NAN;
}
ofD->params.w_radio = 1;
//Fixed the initial seeds.
for (int j = 0; j < h; j++)
for (int i = 0; i < w; i++)
{
//Indicates the initial seed in the similarity map
if (std::isfinite(in_flow[j*w +i]) && std::isfinite(in_flow[w*h + j*w +i]))
{
out_flow[j*w + i] = in_flow[j*w + i];
out_flow[w*h + j*w + i] = in_flow[w*h + j*w +i];
ofD->fixed_points[j*w + i] = 1;
// add_neigbors 0 means that during the propagation interpolates the patch
// based on the energy.
add_neighbors(i0, i1, i_1, ene_val, ofD, ofS, queue, i, j, 0, out_flow, out_occ, BiFilt, w, h);
out_flow[j*w + i] = NAN;
out_flow[w*h + j*w + i] = NAN;
ofD->fixed_points[j*w + i] = 0;
}
}
ofD->params.w_radio = wr;
//Propagate the information of the initial seeds to their neighbours.
for (int j = 0; j < h; j++)
for (int i = 0; i < w; i++)
{
if (std::isfinite(in_flow[j*w +i]) && std::isfinite(in_flow[w*h + j*w +i]))
{
out_flow[j*w + i] = in_flow[j*w + i];
out_flow[w*h + j*w + i] = in_flow[w*h + j*w +i];
ofD->fixed_points[j*w + i] = 1;
ene_val[j*w + i] = 0.0;
}
}
}
// Insert each pixel into the queue as possible candidate. Its related energy comes
// from the energy stored at the moment that the pixel was fixed.
/**
* @brief Insert each pixel into the queue as possible candidate with its energy.
* @details its related energy comes from the energy stored at the moment that the pixel was fixed
*
* @param in array containing the flow field values
* @param ofD OpticalFlowData struct containing the updated optical flow fields
* @param queue priority queue where the new candidates will be inserted
* @param ene_val array that stores the energy values updated (maybe) during previous iterations
* @param out_occ array that stores the occlusions' map (maybe) updated during previous iterations
* @param w width of the optical flow data being processed
* @param h height of the optical flow data being processed
*/
void insert_potential_candidates(const float *in, OpticalFlowData *ofD, pq_cand &queue, float *ene_val,
float *out_flow, const float *out_occ, const int w, const int h)
{
//Fixed the initial seeds.
for (int j = 0; j < h; j++)
for (int i = 0; i < w; i++)
{
//Indicates the initial seed in the similarity map
if (std::isfinite(in[j*w +i]) && std::isfinite(in[w*h + j*w +i]))
{
SparseOF element;
element.i = i; // column
element.j = j; // row
element.u = in[j*w +i];
element.v = in[w*h + j*w +i];
//Obs: Notice that ene_val contains (en)*saliency
element.sim_node = ene_val[j*w +i];
if (ofD->params.val_method >= 8) {
element.occluded = out_occ[j * w + i];
}
assert(std::isfinite(ene_val[j*w +i]));
queue.push(element);
}
}
//Set to the initial conditions all the stuff
for (int i = 0; i < w*h; i++)
{
ofD->fixed_points[i] = 0;
ene_val[i] = INFINITY;
out_flow[i] = NAN;
out_flow[w*h + i] = NAN;
}
}
// Initialize the data to prepare everything for the region growing
/**
* @brief Initialize the data to prepare everything for the region growing
* @details energy, flow and fixed (computed or not) variables set to default values (Inf, NAN and 0)
*
* @param ofD OpticalFlowData struct containing fixed/not fixed variable to be reset to default
* @param ene_val array that stores the energy values to be reset to default
* @param out output flow array to be reset to default
* @param w width of the optical flow data being processed
* @param h height of the optical flow data being processed
*/
void prepare_data_for_growing(OpticalFlowData *ofD, float *ene_val, float *out, const int w, const int h)
{
// Set to the initial conditions all the stuff
for (int i = 0; i < w*h; i++)
{
ofD->fixed_points[i] = 0;
ene_val[i] = INFINITY;
out[i] = NAN;
out[w*h + i] = NAN;
}
}
/**
* @brief function that manages a specific iteration of the local minimization, processing all the queue's candidates
*
* @param i0 source frame at time 't'
* @param i1 second frame at time 't+1'
* @param i_1 previous frame at time 't-1' (used for occlusions only)
* @param queue priority queue from/to which candidates will be obtained/inserted
* @param ofS SpecificOFStuff struct where functional-specific variables reside
* @param ofD OpticalFlowData struct containing the updated optical flow fields
* @param iteration index for the local minimization's current iteration
* @param ene_val array that stores the energy values (will be updated through the execution of this function)
* @param out_flow array that stores the optical flow fields (will be updated through the execution of this function)
* @param out_occ array that stores the occlusions map (may be updated if occlusions are estimated)
* @param BiFilt struct that contains the indices and weights of the bilateral filter
* @param fwd_or_bwd boolean that defines if we are processing a forward or backward flow (if we store partial results)
* @param w width of the optical flow data being processed (to img_width or partition_width if parallelizing)
* @param h height of the optical flow data being processed (to img_height or partition_height if parallelizing)
*/
void local_growing(const float *i0, const float *i1, const float *i_1, pq_cand *queue, SpecificOFStuff *ofS,
OpticalFlowData *ofD, int iteration, float *ene_val, float *out_flow, float *out_occ,
BilateralFilterData *BiFilt, bool fwd_or_bwd, const int w, const int h, const int part_idx)
{
std::vector<int> percent_print = {30, 70, 80, 95, 100};
int fixed = 0;
const int size = w * h;
std::printf("queue size at start = %d\n", (int) queue->size());
while (!queue->empty()) {
//std::printf("Fixed elements = %d\n", val);
SparseOF element = queue->top();
int i = element.i;
int j = element.j;
queue->pop();
if (!ofD->fixed_points[j * w + i]) {
assert(std::isfinite(element.sim_node));
float u = element.u;
float v = element.v;
float energy = element.sim_node;
float occlusion;
if (ofD->params.val_method >= 8) {
occlusion = element.occluded;
} else {
occlusion = 0.0;
}
if (!std::isfinite(u)) {
std::printf("U1 = %f\n", u);
}
if (!std::isfinite(v)) {
std::printf("U2 = %f\n", v);
}
ofD->fixed_points[j * w + i] = 1;
fixed ++;
out_flow[j * w + i] = u;
out_flow[w * h + j * w + i] = v;
ene_val[j * w + i] = energy;
out_occ[j * w + i] = occlusion;
// TODO: copy the values so they are taken into account in the minimization
// ofD->u1[j*w + i] = u;
// ofD->u2[j*w + i] = v;
add_neighbors(i0, i1, i_1, ene_val, ofD, ofS, queue, i, j, iteration, out_flow, out_occ, BiFilt, w, h);
// From here to the end of the function:
// Code used to print partial growing results for debugging or further exploration
// Just set add the flag '-partial_res 1' when you call any of the Python scripts (or the binary)
float percent = 100 * fixed * 1.0 / size * 1.0;
if (ofD->params.part_res == 1) {
for (int k = 0; k < 4; k++) {
if (percent > percent_print[k] && percent < percent_print[k + 1]) {
string filename_flow = " ";
string filename_occ = " ";
if (fwd_or_bwd) {
if (ofD->params.split_img) {
filename_flow =
"../Results/Partial_results/partial_results_fwd_" +
std::to_string(percent_print[k]) +
"_iter_" + std::to_string(iteration) + "_part_idx" + to_string(part_idx) + ".flo";
iio_save_image_float_split(filename_flow.c_str(), out_flow, w, h, 2);
if (ofD->params.val_method >= 8) {
filename_occ =
"../Results/Partial_results/partial_results_fwd_" +
std::to_string(percent_print[k]) +
"_iter_" + std::to_string(iteration) + "_part_idx" + to_string(part_idx) +
"_occ.png";
auto *out_occ_int = new int[w * h];
for (int l = 0; l < w * h; l++) {
out_occ_int[l] = out_occ[l];
}
iio_save_image_int(filename_occ.c_str(), out_occ_int, w, h);
}
} else {
filename_flow =
"../Results/Partial_results/partial_results_fwd_" +
std::to_string(percent_print[k]) +
"_iter_" + std::to_string(iteration) + ".flo";
iio_save_image_float_split(filename_flow.c_str(), out_flow, w, h, 2);
if (ofD->params.val_method >= 8) {
filename_occ =
"../Results/Partial_results/partial_results_fwd_" +
std::to_string(percent_print[k]) +
"_iter_" + std::to_string(iteration) + "_occ.png";
auto *out_occ_int = new int[w * h];
for (int l = 0; l < w * h; l++) {
out_occ_int[l] = out_occ[l];
}
iio_save_image_int(filename_occ.c_str(), out_occ_int, w, h);
}
}
percent_print[k] = 200;
}
}
}
}
}
}
if (ofD->params.part_res == 1) {
if (fwd_or_bwd) {
string filename_flow = " ";