1 #include <opencv2/core/utility.hpp>
2 #include "opencv2/video/tracking.hpp"
3 #include "opencv2/imgproc.hpp"
4 #include "opencv2/imgcodecs.hpp"
5 #include "opencv2/highgui.hpp"
13 #define APP_NAME "simpleflow_demo : "
17 // print a welcome message, and the OpenCV version
18 printf("This is a demo of SimpleFlow optical flow algorithm,\n"
19 "Using OpenCV version %s\n\n", CV_VERSION);
21 printf("Usage: simpleflow_demo frame1 frame2 output_flow"
22 "\nApplication will write estimated flow "
23 "\nbetween 'frame1' and 'frame2' in binary format"
24 "\ninto file 'output_flow'"
25 "\nThen one can use code from http://vision.middlebury.edu/flow/data/"
26 "\nto convert flow in binary file to image\n");
29 // binary file format for flow data specified here:
30 // http://vision.middlebury.edu/flow/data/
31 static void writeOpticalFlowToFile(const Mat& flow, FILE* file) {
35 fprintf(file, "PIEH");
37 if (fwrite(&cols, sizeof(int), 1, file) != 1 ||
38 fwrite(&rows, sizeof(int), 1, file) != 1) {
39 printf(APP_NAME "writeOpticalFlowToFile : problem writing header\n");
43 for (int i= 0; i < rows; ++i) {
44 for (int j = 0; j < cols; ++j) {
45 Vec2f flow_at_point = flow.at<Vec2f>(i, j);
47 if (fwrite(&(flow_at_point[0]), sizeof(float), 1, file) != 1 ||
48 fwrite(&(flow_at_point[1]), sizeof(float), 1, file) != 1) {
49 printf(APP_NAME "writeOpticalFlowToFile : problem writing data\n");
56 static void run(int argc, char** argv) {
58 printf(APP_NAME "Wrong number of command line arguments for mode `run`: %d (expected %d)\n",
63 Mat frame1 = imread(argv[0]);
64 Mat frame2 = imread(argv[1]);
67 printf(APP_NAME "Image #1 : %s cannot be read\n", argv[0]);
72 printf(APP_NAME "Image #2 : %s cannot be read\n", argv[1]);
76 if (frame1.rows != frame2.rows && frame1.cols != frame2.cols) {
77 printf(APP_NAME "Images should be of equal sizes\n");
81 if (frame1.type() != 16 || frame2.type() != 16) {
82 printf(APP_NAME "Images should be of equal type CV_8UC3\n");
86 printf(APP_NAME "Read two images of size [rows = %d, cols = %d]\n",
87 frame1.rows, frame1.cols);
91 float start = (float)getTickCount();
92 calcOpticalFlowSF(frame1, frame2,
94 3, 2, 4, 4.1, 25.5, 18, 55.0, 25.5, 0.35, 18, 55.0, 25.5, 10);
95 printf(APP_NAME "calcOpticalFlowSF : %lf sec\n", (getTickCount() - start) / getTickFrequency());
97 FILE* file = fopen(argv[2], "wb");
99 printf(APP_NAME "Unable to open file '%s' for writing\n", argv[2]);
102 printf(APP_NAME "Writing to file\n");
103 writeOpticalFlowToFile(flow, file);
107 static bool readOpticalFlowFromFile(FILE* file, Mat& flow) {
109 if (fread(header, 1, 4, file) < 4 && (string)header != "PIEH") {
114 if (fread(&cols, sizeof(int), 1, file) != 1||
115 fread(&rows, sizeof(int), 1, file) != 1) {
119 flow = Mat::zeros(rows, cols, CV_32FC2);
121 for (int i = 0; i < rows; ++i) {
122 for (int j = 0; j < cols; ++j) {
124 if (fread(&(flow_at_point[0]), sizeof(float), 1, file) != 1 ||
125 fread(&(flow_at_point[1]), sizeof(float), 1, file) != 1) {
128 flow.at<Vec2f>(i, j) = flow_at_point;
135 static bool isFlowCorrect(float u) {
136 return !cvIsNaN(u) && (fabs(u) < 1e9);
139 static float calc_rmse(Mat flow1, Mat flow2) {
142 const int rows = flow1.rows;
143 const int cols = flow1.cols;
145 for (int y = 0; y < rows; ++y) {
146 for (int x = 0; x < cols; ++x) {
147 Vec2f flow1_at_point = flow1.at<Vec2f>(y, x);
148 Vec2f flow2_at_point = flow2.at<Vec2f>(y, x);
150 float u1 = flow1_at_point[0];
151 float v1 = flow1_at_point[1];
152 float u2 = flow2_at_point[0];
153 float v2 = flow2_at_point[1];
155 if (isFlowCorrect(u1) && isFlowCorrect(u2) && isFlowCorrect(v1) && isFlowCorrect(v2)) {
156 sum += (u1-u2)*(u1-u2) + (v1-v2)*(v1-v2);
161 return (float)sqrt(sum / (1e-9 + counter));
164 static void eval(int argc, char** argv) {
166 printf(APP_NAME "Wrong number of command line arguments for mode `eval` : %d (expected %d)\n",
173 FILE* flow_file_1 = fopen(argv[0], "rb");
174 if (flow_file_1 == NULL) {
175 printf(APP_NAME "Cannot open file with first flow : %s\n", argv[0]);
178 if (!readOpticalFlowFromFile(flow_file_1, flow1)) {
179 printf(APP_NAME "Cannot read flow data from file %s\n", argv[0]);
184 FILE* flow_file_2 = fopen(argv[1], "rb");
185 if (flow_file_2 == NULL) {
186 printf(APP_NAME "Cannot open file with first flow : %s\n", argv[1]);
189 if (!readOpticalFlowFromFile(flow_file_2, flow2)) {
190 printf(APP_NAME "Cannot read flow data from file %s\n", argv[1]);
195 float rmse = calc_rmse(flow1, flow2);
196 printf("%lf\n", rmse);
199 int main(int argc, char** argv) {
201 printf(APP_NAME "Mode is not specified\n");
205 string mode = (string)argv[1];
206 int new_argc = argc - 2;
207 char** new_argv = &argv[2];
210 run(new_argc, new_argv);
211 } else if ("eval" == mode) {
212 eval(new_argc, new_argv);
213 } else if ("help" == mode)
216 printf(APP_NAME "Unknown mode : %s\n", argv[1]);