-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparse_flow.cpp
executable file
·73 lines (65 loc) · 1.97 KB
/
sparse_flow.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
#include <cstdlib>
#include <cassert>
#include <string>
#include <fstream>
#include <limits>
#include <cmath>
#include <iostream>
#include <cstdio>
extern "C" {
#include "iio.h"
}
static int sparse_optical_flow(char *input, int nx, int ny, float *out) {
float x1, x2, y1, y2;
std::string filename_sift_matches(input);
std::ifstream file(input);
std::string str;
//Initialize all the the optical flow to NAN
for (int j = 0; j < ny; j++){
for (int i = 0; i < nx; i++){
out[j*nx + i] = NAN;
out[nx*ny + j*nx + i] = NAN;
}
}
if (file){
//Insert the sparse flow obtained from matches
while (getline(file, str)){
//Colum I0, Row I0, Colum I1, Row I1
sscanf(str.c_str(), "%f %f %f %f\n",
&x1, &y1, &x2, &y2);
float u = x2 - x1;
float v = y2 - y1;
int i = std::floor(x1);
int j = std::floor(y1);
//fprintf(stderr, "Colum x row: %d x %d u x v: (%f,%f)\n", i,j,u,v);
out[j*nx + i] = u;
out[nx*ny + j*nx + i] = v;
}
return 1;
}else{
std::cout << "File does not exist\n";
std::cout << input << "\n";
return 0;
}
}
int main(int argc, char *argv[]) {
// process input arguments
if (argc != 5) {
fprintf(stderr, "usage:\n\t%s sift_matches.txt colum row out.flo\n", *argv);
// 0 1 2 3 4
fprintf(stderr, "usage:\n\t Nargs:%d\n", argc);
return 1;
}
//Read the input parameter
char *filename_in = argv[1];
char *filename_out = argv[4];
int nx = atoi(argv[2]);
int ny = atoi(argv[3]);
float *out = new float[2*nx*ny];
//Call the function that creates the sparse optical flow
sparse_optical_flow(filename_in, nx, ny, out);
//Save the optical flow
iio_save_image_float_split(filename_out, out, nx, ny, 2);
delete [] out;
return 0;
}