forked from tikroeger/OF_DIS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_dense.cpp
447 lines (376 loc) · 15.5 KB
/
run_dense.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
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <sys/time.h>
#include <fstream>
#include "oflow.h"
#ifndef HAS_OPENCV
#define HAS_OPENCV
#endif
#include "mexopencv.hpp"
using namespace std;
// Save a Depth/OF/SF as .flo file
void SaveFlowFile(cv::Mat& img, const char* filename)
{
cv::Size szt = img.size();
int width = szt.width, height = szt.height;
int nc = img.channels();
float tmp[nc];
FILE *stream = fopen(filename, "wb");
if (stream == 0)
cout << "WriteFile: could not open file" << endl;
// write the header
fprintf(stream, "PIEH");
if ((int)fwrite(&width, sizeof(int), 1, stream) != 1 ||
(int)fwrite(&height, sizeof(int), 1, stream) != 1)
cout << "WriteFile: problem writing header" << endl;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (nc==1) // depth
tmp[0] = img.at<float>(y,x);
else if (nc==2) // Optical Flow
{
tmp[0] = img.at<cv::Vec2f>(y,x)[0];
tmp[1] = img.at<cv::Vec2f>(y,x)[1];
}
else if (nc==4) // Scene Flow
{
tmp[0] = img.at<cv::Vec4f>(y,x)[0];
tmp[1] = img.at<cv::Vec4f>(y,x)[1];
tmp[2] = img.at<cv::Vec4f>(y,x)[2];
tmp[3] = img.at<cv::Vec4f>(y,x)[3];
}
if ((int)fwrite(tmp, sizeof(float), nc, stream) != nc)
cout << "WriteFile: problem writing data" << endl;
}
}
fclose(stream);
}
// Save a depth as .pfm file
void SavePFMFile(cv::Mat& img, const char* filename)
{
cv::Size szt = img.size();
FILE *stream = fopen(filename, "wb");
if (stream == 0)
cout << "WriteFile: could not open file" << endl;
// write the header
fprintf(stream, "Pf\n%d %d\n%f\n", szt.width, szt.height, (float)-1.0f);
for (int y = szt.height-1; y >= 0 ; --y)
{
for (int x = 0; x < szt.width; ++x)
{
float tmp = -img.at<float>(y,x);
if ((int)fwrite(&tmp, sizeof(float), 1, stream) != 1)
cout << "WriteFile: problem writing data" << endl;
}
}
fclose(stream);
}
// Read a depth/OF/SF as file
void ReadFlowFile(cv::Mat& img, const char* filename)
{
FILE *stream = fopen(filename, "rb");
if (stream == 0)
cout << "ReadFile: could not open %s" << endl;
int width, height;
float tag;
int nc = img.channels();
float tmp[nc];
if ((int)fread(&tag, sizeof(float), 1, stream) != 1 ||
(int)fread(&width, sizeof(int), 1, stream) != 1 ||
(int)fread(&height, sizeof(int), 1, stream) != 1)
cout << "ReadFile: problem reading file %s" << endl;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if ((int)fread(tmp, sizeof(float), nc, stream) != nc)
cout << "ReadFile(%s): file is too short" << endl;
if (nc==1) // depth
img.at<float>(y,x) = tmp[0];
else if (nc==2) // Optical Flow
{
img.at<cv::Vec2f>(y,x)[0] = tmp[0];
img.at<cv::Vec2f>(y,x)[1] = tmp[1];
}
else if (nc==4) // Scene Flow
{
img.at<cv::Vec4f>(y,x)[0] = tmp[0];
img.at<cv::Vec4f>(y,x)[1] = tmp[1];
img.at<cv::Vec4f>(y,x)[2] = tmp[2];
img.at<cv::Vec4f>(y,x)[3] = tmp[3];
}
}
}
if (fgetc(stream) != EOF)
cout << "ReadFile(%s): file is too long" << endl;
fclose(stream);
}
void ConstructImgPyramide(const cv::Mat & img_ao_fmat, cv::Mat * img_ao_fmat_pyr, cv::Mat * img_ao_dx_fmat_pyr, cv::Mat * img_ao_dy_fmat_pyr, const float ** img_ao_pyr, const float ** img_ao_dx_pyr, const float ** img_ao_dy_pyr, const int lv_f, const int lv_l, const int rpyrtype, const bool getgrad, const int imgpadding, const int padw, const int padh)
{
for (int i=0; i<=lv_f; ++i) // Construct image and gradient pyramides
{
if (i==0) // At finest scale: copy directly, for all other: downscale previous scale by .5
{
#if (SELECTCHANNEL==1 | SELECTCHANNEL==3) // use RGB or intensity image directly
img_ao_fmat_pyr[i] = img_ao_fmat.clone();
#elif (SELECTCHANNEL==2) // use gradient magnitude image as input
cv::Mat dx,dy,dx2,dy2,dmag;
cv::Sobel( img_ao_fmat, dx, CV_32F, 1, 0, 1, 1, 0, cv::BORDER_DEFAULT );
cv::Sobel( img_ao_fmat, dy, CV_32F, 0, 1, 1, 1, 0, cv::BORDER_DEFAULT );
dx2 = dx.mul(dx);
dy2 = dy.mul(dy);
dmag = dx2+dy2;
cv::sqrt(dmag,dmag);
img_ao_fmat_pyr[i] = dmag.clone();
#endif
}
else
cv::resize(img_ao_fmat_pyr[i-1], img_ao_fmat_pyr[i], cv::Size(), .5, .5, cv::INTER_LINEAR);
img_ao_fmat_pyr[i].convertTo(img_ao_fmat_pyr[i], rpyrtype);
if ( getgrad )
{
cv::Sobel( img_ao_fmat_pyr[i], img_ao_dx_fmat_pyr[i], CV_32F, 1, 0, 1, 1, 0, cv::BORDER_DEFAULT );
cv::Sobel( img_ao_fmat_pyr[i], img_ao_dy_fmat_pyr[i], CV_32F, 0, 1, 1, 1, 0, cv::BORDER_DEFAULT );
img_ao_dx_fmat_pyr[i].convertTo(img_ao_dx_fmat_pyr[i], CV_32F);
img_ao_dy_fmat_pyr[i].convertTo(img_ao_dy_fmat_pyr[i], CV_32F);
}
}
// pad images
for (int i=0; i<=lv_f; ++i) // Construct image and gradient pyramides
{
copyMakeBorder(img_ao_fmat_pyr[i],img_ao_fmat_pyr[i],imgpadding,imgpadding,imgpadding,imgpadding,cv::BORDER_REPLICATE); // Replicate border for image padding
img_ao_pyr[i] = (float*)img_ao_fmat_pyr[i].data;
if ( getgrad )
{
copyMakeBorder(img_ao_dx_fmat_pyr[i],img_ao_dx_fmat_pyr[i],imgpadding,imgpadding,imgpadding,imgpadding,cv::BORDER_CONSTANT , 0); // Zero padding for gradients
copyMakeBorder(img_ao_dy_fmat_pyr[i],img_ao_dy_fmat_pyr[i],imgpadding,imgpadding,imgpadding,imgpadding,cv::BORDER_CONSTANT , 0);
img_ao_dx_pyr[i] = (float*)img_ao_dx_fmat_pyr[i].data;
img_ao_dy_pyr[i] = (float*)img_ao_dy_fmat_pyr[i].data;
}
}
}
int AutoFirstScaleSelect(int imgwidth, int fratio, int patchsize)
{
return std::max(0,(int)std::floor(log2((2.0f*(float)imgwidth) / ((float)fratio * (float)patchsize))));
}
cv::Mat returnFlow(cv::Mat img_ao_mat, cv::Mat img_bo_mat, int rpyrtype, int nochannels, int incoltype, int sel_oppoint)
{
struct timeval tv_start_all, tv_end_all;
gettimeofday(&tv_start_all, NULL);
cv::Mat img_tmp;
cv::Mat img_ao_fmat, img_bo_fmat;
cv::Size sz = img_ao_mat.size();
int width_org = sz.width; // unpadded original image size
int height_org = sz.height; // unpadded original image size
// *** Parse rest of parameters, See oflow.h for definitions.
int lv_f, lv_l, maxiter, miniter, patchsz, patnorm, costfct, tv_innerit, tv_solverit, verbosity;
float mindprate, mindrrate, minimgerr, poverl, tv_alpha, tv_gamma, tv_delta, tv_sor;
bool usefbcon, usetvref;
//bool hasinfile; // initialization flow file
//char *infile = nullptr;
mindprate = 0.05; mindrrate = 0.95; minimgerr = 0.0;
usefbcon = 0; patnorm = 1; costfct = 0;
tv_alpha = 10.0; tv_gamma = 10.0; tv_delta = 5.0;
tv_innerit = 1; tv_solverit = 3; tv_sor = 1.6;
verbosity = 0; // Default: Plot detailed timings
int fratio = 5; // For automatic selection of coarsest scale: 1/fratio * width = maximum expected motion magnitude in image. Set lower to restrict search space.
switch (sel_oppoint)
{
case 1:
patchsz = 8; poverl = 0.3;
lv_f = AutoFirstScaleSelect(width_org, fratio, patchsz);
lv_l = std::max(lv_f-2,0); maxiter = 16; miniter = 16;
usetvref = 0;
break;
case 3:
patchsz = 12; poverl = 0.75;
lv_f = AutoFirstScaleSelect(width_org, fratio, patchsz);
lv_l = std::max(lv_f-4,0); maxiter = 16; miniter = 16;
usetvref = 1;
break;
case 4:
patchsz = 12; poverl = 0.75;
lv_f = AutoFirstScaleSelect(width_org, fratio, patchsz);
lv_l = std::max(lv_f-5,0); maxiter = 128; miniter = 128;
usetvref = 1;
break;
case 2:
default:
patchsz = 8; poverl = 0.4;
lv_f = AutoFirstScaleSelect(width_org, fratio, patchsz);
lv_l = std::max(lv_f-2,0); maxiter = 12; miniter = 12;
usetvref = 1;
break;
}
// else // Parse explicitly provided parameters
// {
// int acnt = 4; // Argument counter
// lv_f = atoi(argv[acnt++]);
// lv_l = atoi(argv[acnt++]);
// maxiter = atoi(argv[acnt++]);
// miniter = atoi(argv[acnt++]);
// mindprate = atof(argv[acnt++]);
// mindrrate = atof(argv[acnt++]);
// minimgerr = atof(argv[acnt++]);
// patchsz = atoi(argv[acnt++]);
// poverl = atof(argv[acnt++]);
// usefbcon = atoi(argv[acnt++]);
// patnorm = atoi(argv[acnt++]);
// costfct = atoi(argv[acnt++]);
// usetvref = atoi(argv[acnt++]);
// tv_alpha = atof(argv[acnt++]);
// tv_gamma = atof(argv[acnt++]);
// tv_delta = atof(argv[acnt++]);
// tv_innerit = atoi(argv[acnt++]);
// tv_solverit = atoi(argv[acnt++]);
// tv_sor = atof(argv[acnt++]);
// verbosity = atoi(argv[acnt++]);
// //hasinfile = (bool)atoi(argv[acnt++]); // initialization flow file
// //if (hasinfile) infile = argv[acnt++];
// }
// *** Pad image such that width and height are restless divisible on all scales (except last)
int padw=0, padh=0;
int scfct = pow(2,lv_f); // enforce restless division by this number on coarsest scale
//if (hasinfile) scfct = pow(2,lv_f+1); // if initialization file is given, make sure that size is restless divisible by 2^(lv_f+1) !
int div = sz.width % scfct;
if (div>0) padw = scfct - div;
div = sz.height % scfct;
if (div>0) padh = scfct - div;
if (padh>0 || padw>0)
{
copyMakeBorder(img_ao_mat,img_ao_mat,floor((float)padh/2.0f),ceil((float)padh/2.0f),floor((float)padw/2.0f),ceil((float)padw/2.0f),cv::BORDER_REPLICATE);
copyMakeBorder(img_bo_mat,img_bo_mat,floor((float)padh/2.0f),ceil((float)padh/2.0f),floor((float)padw/2.0f),ceil((float)padw/2.0f),cv::BORDER_REPLICATE);
}
sz = img_ao_mat.size(); // padded image size, ensures divisibility by 2 on all scales (except last)
// Timing, image loading
if (verbosity > 1)
{
gettimeofday(&tv_end_all, NULL);
double tt = (tv_end_all.tv_sec-tv_start_all.tv_sec)*1000.0f + (tv_end_all.tv_usec-tv_start_all.tv_usec)/1000.0f;
printf("TIME (Image loading ) (ms): %3g\n", tt);
gettimeofday(&tv_start_all, NULL);
}
// *** Generate scale pyramides
img_ao_mat.convertTo(img_ao_fmat, CV_32F); // convert to float
img_bo_mat.convertTo(img_bo_fmat, CV_32F);
const float* img_ao_pyr[lv_f+1];
const float* img_bo_pyr[lv_f+1];
const float* img_ao_dx_pyr[lv_f+1];
const float* img_ao_dy_pyr[lv_f+1];
const float* img_bo_dx_pyr[lv_f+1];
const float* img_bo_dy_pyr[lv_f+1];
cv::Mat img_ao_fmat_pyr[lv_f+1];
cv::Mat img_bo_fmat_pyr[lv_f+1];
cv::Mat img_ao_dx_fmat_pyr[lv_f+1];
cv::Mat img_ao_dy_fmat_pyr[lv_f+1];
cv::Mat img_bo_dx_fmat_pyr[lv_f+1];
cv::Mat img_bo_dy_fmat_pyr[lv_f+1];
ConstructImgPyramide(img_ao_fmat, img_ao_fmat_pyr, img_ao_dx_fmat_pyr, img_ao_dy_fmat_pyr, img_ao_pyr, img_ao_dx_pyr, img_ao_dy_pyr, lv_f, lv_l, rpyrtype, 1, patchsz, padw, padh);
ConstructImgPyramide(img_bo_fmat, img_bo_fmat_pyr, img_bo_dx_fmat_pyr, img_bo_dy_fmat_pyr, img_bo_pyr, img_bo_dx_pyr, img_bo_dy_pyr, lv_f, lv_l, rpyrtype, 1, patchsz, padw, padh);
// Timing, image gradients and pyramid
if (verbosity > 1)
{
gettimeofday(&tv_end_all, NULL);
double tt = (tv_end_all.tv_sec-tv_start_all.tv_sec)*1000.0f + (tv_end_all.tv_usec-tv_start_all.tv_usec)/1000.0f;
printf("TIME (Pyramide+Gradients) (ms): %3g\n", tt);
}
// // Read Initial Truth flow (if available)
// float * initptr = nullptr;
// cv::Mat flowinit;
// if (hasinfile)
// {
// #if (SELECTMODE==1)
// flowinit.create(height_org, width_org, CV_32FC2);
// #else
// flowinit.create(height_org, width_org, CV_32FC1);
// #endif
//
// ReadFlowFile(flowinit, infile);
//
// // padding to ensure divisibility by 2
// if (padh>0 || padw>0)
// copyMakeBorder(flowinit,flowinit,floor((float)padh/2.0f),ceil((float)padh/2.0f),floor((float)padw/2.0f),ceil((float)padw/2.0f),cv::BORDER_REPLICATE);
//
// // resizing to coarsest scale - 1, since the array is upsampled at .5 in the code
// float sc_fct = pow(2,-lv_f-1);
// flowinit *= sc_fct;
// cv::resize(flowinit, flowinit, cv::Size(), sc_fct, sc_fct , cv::INTER_AREA);
//
// initptr = (float*)flowinit.data;
// }
// *** Run main optical flow / depth algorithm
float sc_fct = pow(2,lv_l);
#if (SELECTMODE==1)
cv::Mat flowout(sz.height / sc_fct , sz.width / sc_fct, CV_32FC2); // Optical Flow
#else
cv::Mat flowout(sz.height / sc_fct , sz.width / sc_fct, CV_32FC1); // Depth
#endif
OFC::OFClass ofc(img_ao_pyr, img_ao_dx_pyr, img_ao_dy_pyr,
img_bo_pyr, img_bo_dx_pyr, img_bo_dy_pyr,
patchsz, // extra image padding to avoid border violation check
(float*)flowout.data, // pointer to n-band output float array
nullptr, // pointer to n-band input float array of size of first (coarsest) scale, pass as nullptr to disable
sz.width, sz.height,
lv_f, lv_l, maxiter, miniter, mindprate, mindrrate, minimgerr, patchsz, poverl,
usefbcon, costfct, nochannels, patnorm,
usetvref, tv_alpha, tv_gamma, tv_delta, tv_innerit, tv_solverit, tv_sor,
verbosity);
if (verbosity > 1) gettimeofday(&tv_start_all, NULL);
// *** Resize to original scale, if not run to finest level
if (lv_l != 0)
{
flowout *= sc_fct;
cv::resize(flowout, flowout, cv::Size(), sc_fct, sc_fct , cv::INTER_LINEAR);
}
// If image was padded, remove padding before saving to file
flowout = flowout(cv::Rect((int)floor((float)padw/2.0f),(int)floor((float)padh/2.0f),width_org,height_org));
return flowout;
// // Save Result Image
// #if (SELECTMODE==1)
// SaveFlowFile(flowout, outfile);
// #else
// SavePFMFile(flowout, outfile);
// #endif
if (verbosity > 1)
{
gettimeofday(&tv_end_all, NULL);
double tt = (tv_end_all.tv_sec-tv_start_all.tv_sec)*1000.0f + (tv_end_all.tv_usec-tv_start_all.tv_usec)/1000.0f;
printf("TIME (Saving flow file ) (ms): %3g\n", tt);
}
// return 0;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
// // *** Parse and load input images
// char *imgfile_ao = argv[1];
// char *imgfile_bo = argv[2];
cv::Mat img_ao_mat, img_bo_mat, img_tmp;
int rpyrtype, nochannels, incoltype;
int sel_oppoint = 2; // Default operating point
if (nrhs == 3) // Use provided operating point
sel_oppoint= MxArray(prhs[2]).toInt();
#if (SELECTCHANNEL==1 | SELECTCHANNEL==2) // use Intensity or Gradient image
incoltype = CV_LOAD_IMAGE_GRAYSCALE;
rpyrtype = CV_32FC1;
nochannels = 1;
#elif (SELECTCHANNEL==3) // use RGB image
incoltype = CV_LOAD_IMAGE_COLOR;
rpyrtype = CV_32FC3;
nochannels = 3;
#endif
img_ao_mat = MxArray(prhs[0]).toMat();
img_bo_mat = MxArray(prhs[1]).toMat();
// char *outfile = "flow_file_from_mex.flo";
cv::Mat opt_flowout = returnFlow(img_ao_mat, img_bo_mat, rpyrtype, nochannels, incoltype, sel_oppoint);
plhs[0] = MxArray(opt_flowout);
// Save Result Image
// #if (SELECTMODE==1)
// SaveFlowFile(opt_flowout, outfile);
// #else
// SavePFMFile(opt_flowout, outfile);
// #endif
// return 0;
}