-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeepflow.c
246 lines (231 loc) · 10.8 KB
/
deepflow.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "image.h"
#include "opticalflow.h"
#include "io.h"
int load_matches_from_file(image_t *match_x, image_t *match_y, image_t *match_z, int wm, int hm,
const char *path_match_file);
void usage(){
printf("usage:\n");
printf("./deepflow image1 image2 outputfile [options] \n");
printf("Compute the flow between two images and store it into a file\n");
printf("Images must be in PPM or JPG format");
printf("\n");
printf("options:\n");
printf(" -h, --help print this message\n");
printf(" -a, -alpha <float>(12.0) weight of smoothness terms\n");
printf(" -b, -beta <float>(300.0) weight of descriptor matching\n");
printf(" -g, -gamma <float>(3.0) weight of gradient constancy assumption\n");
printf(" -d, -delta <float>(2.0) weight of color constancy assumption\n");
printf(" -s, -sigma <float>(0.8) standard deviation of Gaussian presmoothing kernel\n");
printf(" -e, -eta <float>(0.95) ratio factor for coarse-to-fine scheme\n");
printf(" -minsize <interger>(25) size of the coarsest level\n");
printf(" -inner <integer>(5) number of inner fixed point iterations\n");
printf(" -iter <integer>(25) number of iterations for the solver\n");
printf(" -soromega <float>(1.6) omega parameter of the sor method\n");
printf(" -bk <float>(0.0) use decreasing beta i.e. beta(k) = beta*( k / kmax )^betak, if 0, a last iteration is done with beta=0\n");
printf("\n");
printf(" -match '-match filename' reads matches from a file and '-match' from stdin. Matches must be given with the format <int> <int> <int> <int> <float> x1 y1 x2 y2 score at a resolution of 512x256\n");
printf(" -matchf same as -match with images at original resolution\n");
printf("\n");
printf(" -sintel set the parameters to the one used in the ICCV paper for MPI-Sintel dataset\n");
printf(" -middlebury set the parameters to the one used in the ICCV paper for middlebury dataset\n");
printf(" -kitti set the parameters to the one used in the ICCV paper for KITTI dataset\n");
printf("\n");
}
void require_argument(const char *arg){
fprintf(stderr, "Require an argument after option %s\n", arg);
exit(1);
}
int main(int argc, char ** argv){
image_t *match_x = NULL, *match_y = NULL, *match_z = NULL;
// load images
if(argc < 4){
fprintf(stderr,"Wrong command, require at least 3 arguments.\n\n");
usage();
exit(1);
}
color_image_t *im1 = color_image_load(argv[1]), *im2 = color_image_load(argv[2]);
if(im1->width != im2->width || im1->height != im2->height){
fprintf(stderr,"Image dimensions does not match\n");
exit(1);
}
// set params to default
optical_flow_params_t* params = (optical_flow_params_t*) malloc(sizeof(optical_flow_params_t));
if(!params){
fprintf(stderr,"error deepflow(): not enough memory\n");
exit(1);
}
optical_flow_params_default(params);
// parse options
int current_arg = 4;
while(1){
if( current_arg >= argc) break;
if(!strcmp(argv[current_arg],"-h") || !strcmp(argv[current_arg],"--help") ){
usage();
exit(1);
}else if(!strcmp(argv[current_arg],"-a") || !strcmp(argv[current_arg],"-alpha") ){
current_arg++;
if(current_arg >= argc) require_argument("alpha");
float alpha = atof(argv[current_arg++]);
if(alpha<0){
fprintf(stderr,"Alpha argument cannot be negative\n");
exit(1);
}
params->alpha = alpha;
}else if(!strcmp(argv[current_arg],"-b") || !strcmp(argv[current_arg],"-beta") ){
current_arg++;
if(current_arg >= argc) require_argument("beta");
float beta = atof(argv[current_arg++]);
if(beta<0){
fprintf(stderr,"Beta argument cannot be negative\n");
exit(1);
}
params->beta = beta;
}else if(!strcmp(argv[current_arg],"-g") || !strcmp(argv[current_arg],"-gamma") ){
current_arg++;
if(current_arg >= argc) require_argument("gamma");
float gamma = atof(argv[current_arg++]);
if(gamma<0){
fprintf(stderr,"Gamma argument cannot be negative\n");
exit(1);
}
params->gamma = gamma;
}else if(!strcmp(argv[current_arg],"-d") || !strcmp(argv[current_arg],"-delta") ){
current_arg++;
if(current_arg >= argc) require_argument("delta");
float delta = atof(argv[current_arg++]);
if(delta<0) {
fprintf(stderr,"Delta argument cannot be negative\n");
exit(1);
}
params->delta = delta;
}else if(!strcmp(argv[current_arg],"-s") || !strcmp(argv[current_arg],"-sigma") ){
current_arg++;
if(current_arg >= argc) require_argument("sigma");
float sigma = atof(argv[current_arg++]);
if(sigma<0){
fprintf(stderr,"Sigma argument is negative\n");
exit(1);
}
params->sigma = sigma;
}else if(!strcmp(argv[current_arg],"-bk")) {
current_arg++;
if(current_arg >= argc) require_argument("bk");
float betak = atof(argv[current_arg++]);
if(betak<0.0f){
fprintf(stderr,"Bk argument must be positive\n");
exit(1);
}
params->bk = betak;
}else if(!strcmp(argv[current_arg],"-e") || !strcmp(argv[current_arg],"-eta") ){
current_arg++;
if(current_arg >= argc) require_argument("eta");
float eta = atof(argv[current_arg++]);
if(eta<0.25 || eta>0.98){
fprintf(stderr,"Eta argument has to be between 0.25 and 0.98\n");
exit(1);
}
params->eta = eta;
}else if( !strcmp(argv[current_arg],"-minsize") ){
current_arg++;
if(current_arg >= argc) require_argument("minsize");
int minsize = atoi(argv[current_arg++]);
if(minsize < 10){
fprintf(stderr,"Minsize argument has to be higher than 10\n");
exit(1);
}
params->min_size = minsize;
}else if(!strcmp(argv[current_arg],"-inner") ){
current_arg++;
if(current_arg >= argc) require_argument("inner");
int inner = atoi(argv[current_arg++]);
if(inner<=0){
fprintf(stderr,"Inner argument must be strictly positive\n");
exit(1);
}
params->n_inner_iteration = inner;
}else if(!strcmp(argv[current_arg],"-iter") ){
current_arg++;
if(current_arg >= argc) require_argument("iter");
int iter = atoi(argv[current_arg++]);
if(iter<=0){
fprintf(stderr,"Iter argument must be strictly positive\n");
exit(1);
}
params->n_solver_iteration = iter;
}else if( !strcmp(argv[current_arg],"-match") || !strcmp(argv[current_arg],"-matchf")){
int wm = im1->width, hm = im1->height;
if( !strcmp(argv[current_arg++],"-match") ){
wm = 512;
hm = 256;
}
image_delete(match_x); image_delete(match_y); image_delete(match_z);
match_x = image_new(wm, hm); match_y = image_new(wm, hm); match_z = image_new(wm, hm);
char *path_match_file = 0;
if( current_arg<argc && argv[current_arg][0] != '-'){
path_match_file = argv[current_arg++];
}
int load_result = load_matches_from_file(match_x, match_y, match_z, wm, hm, path_match_file);
if( load_result != 0 ) {
exit(load_result);
}
}else if ( !strcmp(argv[current_arg],"-sintel") ){
current_arg++;
optical_flow_params_sintel(params);
}else if ( !strcmp(argv[current_arg],"-middlebury") ){
current_arg++;
optical_flow_params_middlebury(params);
}else if ( !strcmp(argv[current_arg],"-kitti") ){
current_arg++;
optical_flow_params_kitti(params);
}else{
if(argv[current_arg][0] == '-'){
fprintf(stderr,"Unknow options %s\n",argv[current_arg]);
}else{
fprintf(stderr,"Error while reading options, %s\n",argv[current_arg]);
}
exit(1);
}
}
image_t *wx = image_new(im1->width,im1->height), *wy = image_new(im1->width,im1->height);
optical_flow(wx, wy, im1, im2, params, match_x, match_y, match_z);
const char *dst_filename = argv[3];
if (writeFlowFile(dst_filename, wx, wy) != 0) {
fprintf(stderr, "Error while opening %s\n", dst_filename);
exit(1);
}
image_delete(wx);
image_delete(wy);
image_delete(match_x); image_delete(match_y); image_delete(match_z);
color_image_delete(im1); color_image_delete(im2);
free(params);
return 0;
}
int load_matches_from_file(image_t *match_x, image_t *match_y, image_t *match_z, int wm, int hm,
const char *path_match_file) {
FILE *fid = stdin;
if( path_match_file != 0 ){
fid = fopen(path_match_file, "r");
if(fid==NULL){
fprintf(stderr, "Cannot read matches from file %s", path_match_file);
return 1;
}
}
image_erase(match_x);
image_erase(match_y);
image_erase(match_z);
int x1, x2, y1, y2;
float score;
while(!feof(fid) && fscanf(fid, "%d %d %d %d %f\n", &x1, &y1, &x2, &y2, &score)==5){
if( x1<0 || y1<0 || x2<0 || y2<0 || x1>=wm || y1>=hm || x2>=wm || y2>=hm){
fprintf(stderr, "Error while reading matches %d %d -> %d %d, out of bounds\n", x1, y1, x2, y2);
return 1;
}
match_x->data[ y1*match_x->stride+x1 ] = (float) (x2-x1);
match_y->data[ y1*match_x->stride+x1 ] = (float) (y2-y1);
match_z->data[ y1*match_x->stride+x1 ] = score;
}
return 0;
}