-
Notifications
You must be signed in to change notification settings - Fork 202
/
denseFlow.cpp
107 lines (89 loc) · 3.12 KB
/
denseFlow.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
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
#include <iostream>
using namespace cv;
static void convertFlowToImage(const Mat &flow_x, const Mat &flow_y, Mat &img_x, Mat &img_y, double lowerBound, double higherBound) {
#define CAST(v, L, H) ((v) > (H) ? 255 : (v) < (L) ? 0 : cvRound(255*((v) - (L))/((H)-(L))))
for (int i = 0; i < flow_x.rows; ++i) {
for (int j = 0; j < flow_y.cols; ++j) {
float x = flow_x.at<float>(i,j);
float y = flow_y.at<float>(i,j);
img_x.at<uchar>(i,j) = CAST(x, lowerBound, higherBound);
img_y.at<uchar>(i,j) = CAST(y, lowerBound, higherBound);
}
}
#undef CAST
}
static void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,double, const Scalar& color)
{
for(int y = 0; y < cflowmap.rows; y += step)
for(int x = 0; x < cflowmap.cols; x += step)
{
const Point2f& fxy = flow.at<Point2f>(y, x);
line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),
color);
circle(cflowmap, Point(x,y), 2, color, -1);
}
}
int main(int argc, char** argv)
{
// IO operation
const char* keys =
{
"{ f | vidFile | ex2.avi | filename of video }"
"{ x | xFlowFile | flow_x | filename of flow x component }"
"{ y | yFlowFile | flow_y | filename of flow x component }"
"{ i | imgFile | flow_i | filename of flow image}"
"{ b | bound | 15 | specify the maximum of optical flow}"
};
CommandLineParser cmd(argc, argv, keys);
string vidFile = cmd.get<string>("vidFile");
string xFlowFile = cmd.get<string>("xFlowFile");
string yFlowFile = cmd.get<string>("yFlowFile");
string imgFile = cmd.get<string>("imgFile");
int bound = cmd.get<int>("bound");
VideoCapture capture(vidFile);
if(!capture.isOpened()) {
printf("Could not initialize capturing..\n");
return -1;
}
int frame_num = 0;
Mat image, prev_image, prev_grey, grey, frame, flow, cflow;
while(true) {
capture >> frame;
if(frame.empty())
break;
if(frame_num == 0) {
image.create(frame.size(), CV_8UC3);
grey.create(frame.size(), CV_8UC1);
prev_image.create(frame.size(), CV_8UC3);
prev_grey.create(frame.size(), CV_8UC1);
frame.copyTo(prev_image);
cvtColor(prev_image, prev_grey, CV_BGR2GRAY);
frame_num++;
continue;
}
frame.copyTo(image);
cvtColor(image, grey, CV_BGR2GRAY);
// calcOpticalFlowFarneback(prev_grey,grey,flow,0.5, 3, 15, 3, 5, 1.2, 0 );
calcOpticalFlowFarneback(prev_grey, grey, flow, 0.702, 5, 10, 2, 7, 1.5, cv::OPTFLOW_FARNEBACK_GAUSSIAN );
// prev_image.copyTo(cflow);
// drawOptFlowMap(flow, cflow, 12, 1.5, Scalar(0, 255, 0));
Mat flows[2];
split(flow,flows);
Mat imgX(flows[0].size(),CV_8UC1);
Mat imgY(flows[0].size(),CV_8UC1);
convertFlowToImage(flows[0],flows[1], imgX, imgY, -bound, bound);
char tmp[20];
sprintf(tmp,"_%04d.jpg",int(frame_num));
imwrite(xFlowFile + tmp,imgX);
imwrite(yFlowFile + tmp,imgY);
imwrite(imgFile + tmp, image);
std::swap(prev_grey, grey);
std::swap(prev_image, image);
frame_num = frame_num + 1;
}
return 0;
}