/
gbh_stream.cpp
741 lines (658 loc) · 22.3 KB
/
gbh_stream.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
/*
Original Code From:
Copyright (C) 2006 Pedro Felzenszwalb
Modifications (may have been made) Copyright (C) 2011,2012
Chenliang Xu, Jason Corso.
Modifications (may have been made) Copyright (C) 2013
Subarna Tripathi, UCSD.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <cstdio>
#include <cstdlib>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
# ifdef WIN32
#include <time.h>
//#include "msdirent.h"
#include <direct.h>
# else
#include <dirent.h>
# endif
#include "image.h"
#include "pnmfile.h"
#include "segment-image.h"
#include "disjoint-set.h"
# include <math.h>
//subarna
#include "optical_flow\OpticalFlow.h"
#include "optical_flow\Image.h"
int main(int argc, char** argv) {
if (argc != 11) {
printf("%s c c_reg min sigma hie_num start_fr end_fr input output\n", argv[0]);
printf(" c --> value for the threshold function in over-segmentation\n");
printf(" c_reg --> value for the threshold function in hierarchical region segmentation\n");
printf(" min --> enforced minimum supervoxel size\n");
printf(" sigma --> variance of the Gaussian smoothing.\n");
printf(" range --> number of frames as one subsequence (k in the paper)\n");
printf(" hie_num --> desired number of hierarchy levels\n");
printf(" start_fr --> starting frame index\n");
printf(" end_fr --> end frame index\n");
printf(" input --> input path of ppm video frames\n");
printf(" output --> output path of segmentation results\n");
return 1;
}
// Read Parameters
float c = (float)atof(argv[1]);
float c_reg = (float)atof(argv[2]);
int min_size = atoi(argv[3]);
float sigma = (float)atof(argv[4]);
int range = atoi(argv[5]);
int hie_num = atoi(argv[6]);
int start_frame_num = atoi(argv[7]);
int end_frame_num = atoi(argv[8]);
char* input_path = argv[9];
char* output_path = argv[10];
if (c <= 0 || c_reg < 0 || min_size < 0 || sigma < 0 || hie_num < 0) {
fprintf(stderr, "Unable to use the input parameters.");
return 1;
}
//subarna: optical flow
double alpha = 0.012;
double ratio = 0.75;
int minWidth = 20;
int nOuterFPIterations = 7;
int nInnerFPIterations = 1;
int nSORIterations = 30;
// count files in the input directory
/*int frame_num = 0;
struct dirent* pDirent;
DIR* pDir;
pDir = opendir(input_path);
if (pDir != NULL) {
while ((pDirent = readdir(pDir)) != NULL) {
int len = strlen(pDirent->d_name);
if (len >= 4) {
if (strcmp(".ppm", &(pDirent->d_name[len - 4])) == 0)
frame_num++;
}
}
}
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200%28v=vs.85%29.aspx
if (frame_num == 0) {
fprintf(stderr, "Unable to find video frames at %s", input_path);
return 1;
}
printf("Total number of frames in fold is %d\n", frame_num);
// check if the range is right
if (range > frame_num)
range = frame_num;
if (range < 1)
range = 1;
*/
// check if the range is right
if (range > (end_frame_num - start_frame_num))
range = (end_frame_num - start_frame_num);
if (range < 1)
range = 1;
# ifdef USE_OPTICAL_FLOW
if (range < 2)
{
range = 2;
printf("range value should at least be 2 if motion values are to be used\n");
printf("making the range value 2");
if ( (end_frame_num - start_frame_num) < 2 )
{
printf("\n Not enough frames ... exiting\n\n\n\n");
exit(1);
}
}
# endif
// make the output directory
struct stat st;
int status = 0;
char savepath[1024];
sprintf(savepath, "%s",output_path);
if (stat(savepath, &st) != 0) {
/* Directory does not exist */
if (mkdir(savepath/*, S_IRWXU*/) != 0) {
status = -1;
}
}
for (int i = 0; i <= hie_num; i++) {
sprintf(savepath, "%s/%02d",output_path,i);
if (stat(savepath, &st) != 0) {
/* Directory does not exist */
if (_mkdir(savepath/*, S_IRWXU*/) != 0) { //subarna: _mkdir
status = -1;
}
}
}
if (status == -1) {
fprintf(stderr,"Unable to create the output directories at %s",output_path);
return 1;
}
// Initialize Parameters
int last_clip = (end_frame_num - start_frame_num + 1) % range;
int num_clip = (end_frame_num - start_frame_num + 1) / range;
char filepath[1024];
universe** u = new universe*[num_clip + 1];
image<rgb>** input_first = new image<rgb>*[range];
image<rgb>** input_middle = new image<rgb>*[range + 1];
image<rgb>** input_last = new image<rgb>*[last_clip + 1];
//subarna
# ifdef LUV
DImage** input_first_LUV = new DImage*[range];
DImage** input_middle_LUV = new DImage*[range+1];
DImage** input_last_LUV = new DImage*[last_clip+1];
# endif
// Time Recorder
time_t Start_t, End_t;
int time_task;
Start_t = time(NULL);
int height, width;
// clip 1
//calculate pair-wise optical flow
//initialize memory and first frame
printf("processing subsequence -- 0\n");
for (int j = 0; j < range; j++) {
sprintf(filepath, "%s/%05d.ppm", input_path, j + 1+ start_frame_num);
input_first[j] = loadPPM(filepath);
printf("load --> %s\n", filepath);
if (j == 0 )
{
height = input_first[0]->height();
width = input_first[0]->width();
}
# ifdef LUV
input_first_LUV[j] = new DImage (width, height,3);
int m = 0;
//convert to LUV and then apply bilateral filter
for (int s = 0; s < height*width; s++)
{
double x1,y1,z1;
double x2,y2,z2;
ccRGBtoXYZ(input_first[j]->data[s].r, input_first[j]->data[s].g, input_first[j]->data[s].b, &x1, &y1, &z1);
ccXYZtoCIE_Luv(x1, y1, z1, &x2, &y2, &z2);
input_first_LUV[j]->pData[m] = x2,
input_first_LUV[j]->pData[m+1] = y2,
input_first_LUV[j]->pData[m+2] = z2;
m = m+3;
}
// pass input_first_LUV
# endif
}
#ifdef USE_OPTICAL_FLOW
DImage** D_vx_motion_first = new DImage*[range];
DImage** D_vx_motion_middle = new DImage*[range + 1];
DImage** D_vx_motion_last = new DImage*[last_clip + 1];
DImage** D_vy_motion_first = new DImage*[range];
DImage** D_vy_motion_middle = new DImage*[range + 1];
DImage** D_vy_motion_last = new DImage*[last_clip + 1];
# endif
# ifdef USE_OPTICAL_FLOW
DImage** D_input_first = new DImage*[range];
DImage** D_input_middle = new DImage*[range + 1];
DImage** D_input_last = new DImage*[last_clip + 1];
for (int i = 0; i < range; i++ )
{
D_input_first[i] = new DImage(width,height,3);
D_vx_motion_first[i] = new DImage (width,height);
D_vy_motion_first[i] = new DImage (width,height);
}
image<rgb>* motion_buffer = NULL;
DImage* D_motion_buffer = new DImage(width,height,3);
//initialize
int m = 0;
int m1 = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
D_input_first[0]->pData[m1] = input_first[0]->data[m].r;
D_input_first[0]->pData[m1+1] = input_first[0]->data[m].g;
D_input_first[0]->pData[m1+2] = input_first[0]->data[m].b;
m++;
m1=m1+3;
}
}
D_input_first[0]->im2double();
for (int j = 0; j < range-1; j++) {
// initialization for consecutive frames
m = 0;
m1 = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
D_input_first[j+1]->pData[m1] = input_first[j+1]->data[m].r;
D_input_first[j+1]->pData[m1+1] = input_first[j+1]->data[m].g;
D_input_first[j+1]->pData[m1+2] = input_first[j+1]->data[m].b;
m++;
m1=m1+3;
}
}
D_input_first[j+1]->im2double();
OpticalFlow::Coarse2FineTwoFrames(*D_input_first[j], *D_input_first[j+1], &D_vx_motion_first[j], &D_vy_motion_first[j], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
# ifdef DUMP_FLOW
//debug: to be deleted
image<rgb>* test_out = new image<rgb>(width, height);
int m = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
//test_out->data[m].r = ((int)(fabs(D_vx_motion_first[j]->pData[m])*30)>>4)<<4;
//test_out->data[m].g = ((int)(fabs(D_vy_motion_first[j]->pData[m])*30)>>4)<<4;
//test_out->data[m].b = 0 ; //test_out->data[m].r;
test_out->data[m].r = 50 + 30*((int)(fabs(D_vx_motion_first[j]->pData[m])));
test_out->data[m].g = 50 + 30*((int)(fabs(D_vy_motion_first[j]->pData[m])));
test_out->data[m].b = 0 ; //test_out->data[m].r;
m++;
}
}
sprintf(filepath,"%s/motion/%05d.ppm",output_path, j + 1+ start_frame_num);
savePPM(test_out, filepath); //"out_test/test1.ppm"
delete test_out;
# endif
}
delete input_first[0];
sprintf(filepath, "%s/%05d.ppm", input_path, range +1 + start_frame_num);
input_first[0] = loadPPM(filepath);
m = 0;
m1 = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
D_motion_buffer->pData[m1] = input_first[0]->data[m].r;
D_motion_buffer->pData[m1+1] = input_first[0]->data[m].g;
D_motion_buffer->pData[m1+2] = input_first[0]->data[m].b;
m++;
m1=m1+3;
}
}
// dummy place holder
D_motion_buffer->im2double();
OpticalFlow::Coarse2FineTwoFrames(*D_input_first[range-2], *D_motion_buffer, &D_vx_motion_first[range-1], &D_vy_motion_first[range-1], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
# if 0
//copy content from D_motion_first[j-1] to D_motion_first[range-1]
D_vx_motion_first[range-1]->copyData(*D_vx_motion_first[range-2]);
D_vy_motion_first[range-1]->copyData(*D_vy_motion_first[range-2]);
# endif
///////////////////////////////////////
# endif
# ifdef USE_OPTICAL_FLOW
// frame index starts from 0
# ifndef LUV
u[0] = segment_image(output_path, input_first, D_vx_motion_first, D_vy_motion_first, 0, range - 1, c, c_reg, min_size,
sigma, hie_num, NULL,start_frame_num);
# else
u[0] = segment_image_LUV(output_path, input_first_LUV, D_vx_motion_first, D_vy_motion_first, 0, range - 1, c, c_reg, min_size,
sigma, hie_num, NULL,start_frame_num);
# endif
# else
// frame index starts from 0
# ifndef LUV
u[0] = segment_image(output_path, input_first,0, range - 1, c, c_reg, min_size,
sigma, hie_num, NULL,start_frame_num);
# else
u[0] = segment_image_LUV(output_path, input_first_LUV, 0, range - 1, c, c_reg, min_size,
sigma, hie_num, NULL,start_frame_num);
# endif
# endif
for (int j = 0; j < range; j++) {
delete input_first[j];
# ifdef LUV
delete input_first_LUV[j];
# endif
}
# ifdef USE_OPTICAL_FLOW //subarna
for (int j = 0; j < range; j++) {
delete D_vx_motion_first[j];
delete D_vy_motion_first[j];
delete D_input_first[j];
}
# endif
// clip 2 -- last
int ii;
for (ii = 1; ii < num_clip; ii++)
{
printf("processing subsequence -- %d\n", ii);
for (int j = 0; j < range + 1; j++)
{
sprintf(filepath, "%s/%05d.ppm", input_path, ii * range + j + start_frame_num);
input_middle[j] = loadPPM(filepath);
printf("load --> %s\n", filepath);
# ifdef LUV
input_middle_LUV[j] = new DImage (width, height,3);
int m = 0;
//convert to LUV and then apply bilateral filter
for (int s = 0; s < height*width; s++)
{
double x1,y1,z1;
double x2,y2,z2;
ccRGBtoXYZ(input_middle[j]->data[s].r, input_middle[j]->data[s].g, input_middle[j]->data[s].b, &x1, &y1, &z1);
ccXYZtoCIE_Luv(x1, y1, z1, &x2, &y2, &z2);
input_middle_LUV[j]->pData[m] = x2,
input_middle_LUV[j]->pData[m+1] = y2,
input_middle_LUV[j]->pData[m+2] = z2;
m = m+3;
}
//pass input_middle_LUV
# endif
}
# ifdef USE_OPTICAL_FLOW
for (int i = 0; i < range+1; i++ )
{
D_input_middle[i] = new DImage(width,height,3);
D_vx_motion_middle[i] = new DImage(width,height);
D_vy_motion_middle[i] = new DImage(width,height);
}
//initialize
m = 0;
m1 = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
D_input_middle[0]->pData[m1] = input_middle[0]->data[m].r;
D_input_middle[0]->pData[m1+1] = input_middle[0]->data[m].g;
D_input_middle[0]->pData[m1+2] = input_middle[0]->data[m].b;
m++;
m1=m1+3;
}
}
D_input_middle[0]->im2double();
//calculate pair-wise optical flow
for (int j = 0; j < range; j++) {
// initialization for consecutive frames
m = 0;
m1 = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
D_input_middle[j+1]->pData[m1] = input_middle[j+1]->data[m].r;
D_input_middle[j+1]->pData[m1+1] = input_middle[j+1]->data[m].g;
D_input_middle[j+1]->pData[m1+2] = input_middle[j+1]->data[m].b;
m++;
m1 =m1+3;
}
}
D_input_middle[j+1]->im2double();
OpticalFlow::Coarse2FineTwoFrames(*D_input_middle[j], *D_input_middle[j+1], &D_vx_motion_middle[j], &D_vy_motion_middle[j], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
# ifdef DUMP_FLOW
//debug: to be deleted
image<rgb>* test_out = new image<rgb>(width, height);
int m = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
//test_out->data[m].r = ((int)(fabs(D_vx_motion_first[j]->pData[m])*30)>>4)<<4;
//test_out->data[m].g = ((int)(fabs(D_vy_motion_first[j]->pData[m])*30)>>4)<<4;
//test_out->data[m].b = 0 ; //test_out->data[m].r;
test_out->data[m].r = 50 + 40*((int)(fabs(D_vx_motion_middle[j]->pData[m])));
test_out->data[m].g = 50 + 40*((int)(fabs(D_vx_motion_middle[j]->pData[m])));
test_out->data[m].b = 0 ; //test_out->data[m].r;
m++;
}
}
sprintf(filepath,"%s/motion/%05d.ppm",output_path, i * range + j + start_frame_num);
savePPM(test_out, filepath); //"out_test/test1.ppm"
delete test_out;
# endif
}
// subarna: fix crash for specific frame number case
if ( (ii == num_clip - 1 ) && (last_clip == 0) )
{
// for the last frame motion feature -- copy feature value from last frame, but with opposite sign
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
D_vx_motion_middle[range]->pData[i*width + j] = -D_vx_motion_middle[range-1]->pData[i*width + j];
D_vy_motion_middle[range]->pData[i*width + j] = -D_vy_motion_middle[range-1]->pData[i*width + j];
}
}
}
else
{
delete input_middle[0];
sprintf(filepath, "%s/%05d.ppm", input_path, (ii+1) * range +1 + start_frame_num);
input_middle[0] = loadPPM(filepath);
m = 0;
m1 = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
D_motion_buffer->pData[m1] = input_middle[0]->data[m].r;
D_motion_buffer->pData[m1+1] = input_middle[0]->data[m].g;
D_motion_buffer->pData[m1+2] = input_middle[0]->data[m].b;
m++;
m1=m1+3;
}
}
// dummy place holder
D_motion_buffer->im2double();
OpticalFlow::Coarse2FineTwoFrames(*D_input_middle[range], *D_motion_buffer, &D_vx_motion_middle[range], &D_vy_motion_middle[range], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
/////////////////////////////////////
}
# endif
# ifdef USE_OPTICAL_FLOW
# ifndef LUV
u[ii] = segment_image(output_path, input_middle, D_vx_motion_middle, D_vy_motion_middle, ii * range - 1,
ii * range + range - 1, c, c_reg, min_size, sigma, hie_num,
u[ii - 1], start_frame_num);
# else
u[ii] = segment_image_LUV(output_path, input_middle_LUV, D_vx_motion_middle, D_vy_motion_middle, ii * range - 1,
ii * range + range - 1, c, c_reg, min_size, sigma, hie_num,
u[ii - 1],start_frame_num);
# endif
# else
# ifndef LUV
u[ii] = segment_image(output_path, input_middle, ii * range - 1,
ii * range + range - 1, c, c_reg, min_size, sigma, hie_num,
u[ii - 1], start_frame_num);
# else
u[ii] = segment_image_LUV(output_path, input_middle_LUV, ii * range - 1,
ii* range + range - 1, c, c_reg, min_size, sigma, hie_num,
u[ii - 1],start_frame_num);
# endif
# endif
if ( u[ii-1] ) delete u[ii - 1];
////////////////////
for (int j = 0; j < range + 1; j++)
{
delete input_middle[j];
# ifdef LUV
delete input_middle_LUV[j];
# endif
}
# ifdef USE_OPTICAL_FLOW
for (int j = 0; j < range + 1; j++)
{
delete D_vx_motion_middle[j];
delete D_vy_motion_middle[j];
delete D_input_middle[j];
}
# endif
}
// clip last
if (last_clip > 0) {
printf("processing subsequence -- %d\n", num_clip);
for (int j = 0; j < last_clip + 1; j++)
{
sprintf(filepath, "%s/%05d.ppm", input_path, num_clip * range + j + start_frame_num);
input_last[j] = loadPPM(filepath);
printf("load --> %s\n", filepath);
# ifdef LUV
input_last_LUV[j] = new DImage (width, height,3);
int m=0;
for (int s = 0; s < width*height; s++)
{
double x1,y1,z1;
double x2,y2,z2;
ccRGBtoXYZ(input_last[j]->data[s].r, input_last[j]->data[s].g, input_last[j]->data[s].b, &x1, &y1, &z1);
ccXYZtoCIE_Luv(x1, y1, z1, &x2, &y2, &z2);
input_last_LUV[j]->pData[m] = x2,
input_last_LUV[j]->pData[m+1] = y2,
input_last_LUV[j]->pData[m+2] = z2;
m = m+3;
}
# endif
}
# ifdef USE_OPTICAL_FLOW
//subarna
for (int i = 0; i < last_clip+1; i++ )
{
D_input_last[i] = new DImage(width,height,3);
D_vx_motion_last[i] = new DImage(width,height);
D_vy_motion_last[i] = new DImage(width,height);
}
//subarna
//initialize
m1 = 0;
m = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
D_input_last[0]->pData[m1] = input_last[0]->data[m].r;
D_input_last[0]->pData[m1+1] = input_last[0]->data[m].g;
D_input_last[0]->pData[m1+2] = input_last[0]->data[m].b;//0
m++;
m1=m1+3;
}
}
D_input_last[0]->im2double();
//calculate pair-wise optical flow
for (int j = 0; j < last_clip; j++) {
// initialization for consecutive frames
m = 0;
m1 = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
D_input_last[j+1]->pData[m1] = input_last[j+1]->data[m].r;
D_input_last[j+1]->pData[m1+1] = input_last[j+1]->data[m].g;
D_input_last[j+1]->pData[m1+2] = input_last[j+1]->data[m].b;
m++;
m1=m1+3;
}
}
D_input_last[j+1]->im2double();
OpticalFlow::Coarse2FineTwoFrames(*D_input_last[j], *D_input_last[j+1], &D_vx_motion_last[j], &D_vy_motion_last[j], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
# ifdef DUMP_FLOW
//debug: to be deleted
image<rgb>* test_out = new image<rgb>(width, height);
int m = 0;
for(int k1= 0; k1 < width; k1++)
{
for (int k2 = 0; k2 < height; k2++)
{
//test_out->data[m].r = ((int)(fabs(D_vx_motion_first[j]->pData[m])*30)>>4)<<4;
//test_out->data[m].g = ((int)(fabs(D_vy_motion_first[j]->pData[m])*30)>>4)<<4;
//test_out->data[m].b = 0 ; //test_out->data[m].r;
test_out->data[m].r = 50 + 30*((int)(fabs(D_vx_motion_last[j]->pData[m])));
test_out->data[m].g = 50 + 30*((int)(fabs(D_vx_motion_last[j]->pData[m])));
test_out->data[m].b = 0 ; //test_out->data[m].r;
m++;
}
}
sprintf(filepath,"%s/motion/%05d.ppm",output_path, num_clip * range + j + start_frame_num);
savePPM(test_out, filepath); //"out_test/test1.ppm"
delete test_out;
# endif
}
// for the last frame motion feature -- copy feature value from last frame, but with opposite sign
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
D_vx_motion_last[last_clip]->pData[i*width + j] = -D_vx_motion_last[last_clip-1]->pData[i*width + j];
D_vy_motion_last[last_clip]->pData[i*width + j] = -D_vy_motion_last[last_clip-1]->pData[i*width + j];
}
}
//////////////////////////////////////
# endif
# ifdef USE_OPTICAL_FLOW
# ifndef LUV
u[num_clip] = segment_image(output_path, input_last, D_vx_motion_last,D_vy_motion_last, num_clip * range - 1,
num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,
hie_num, u[num_clip - 1],start_frame_num);
# else
u[num_clip] = segment_image_LUV(output_path, input_last_LUV, D_vx_motion_last,D_vy_motion_last, num_clip * range - 1,
num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,hie_num, u[num_clip - 1],start_frame_num);
# endif
# else
# ifndef LUV
u[num_clip] = segment_image(output_path, input_last,num_clip * range - 1,
num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,
hie_num, u[num_clip - 1],start_frame_num);
# else
u[num_clip] = segment_image_LUV(output_path, input_last_LUV, num_clip * range - 1,
num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,hie_num, u[num_clip - 1],start_frame_num);
# endif
# endif
if (u[num_clip - 1]) delete u[num_clip - 1];
delete u[num_clip];
for (int j = 0; j < last_clip + 1; j++)
{
delete input_last[j];
# ifdef LUV
delete input_last_LUV[j];
# endif
}
//subarna
# ifdef USE_OPTICAL_FLOW
for (int j = 0; j < last_clip + 1; j++) {
delete D_vx_motion_last[j];
delete D_vy_motion_last[j];
delete D_input_last[j];
}
delete(D_motion_buffer);
# endif
}
//////////////////////////////////////
# ifdef LUV
delete input_first_LUV;
delete input_middle_LUV;
delete input_last_LUV;
# endif
# ifdef USE_OPTICAL_FLOW
delete D_vx_motion_first;
delete D_vy_motion_first;
delete D_vx_motion_middle;
delete D_vy_motion_middle;
delete D_vx_motion_last;
delete D_vy_motion_last;
# endif
/////////////////////////////////////
delete[] u;
// Time Recorder
End_t = time(NULL);
time_task = difftime(End_t, Start_t);
std::ofstream myfile;
char timefile[1024];
sprintf(timefile, "%s/%s", output_path, "time.txt");
myfile.open(timefile);
myfile << time_task << endl;
myfile.close();
printf("Congratulations! It's done!\n");
printf("Time_total = %d seconds\n", time_task);
return 0;
}