5
#include "opencv2/core/ocl.hpp"
6
#include "opencv2/core/utility.hpp"
7
#include "opencv2/imgcodecs.hpp"
8
#include "opencv2/videoio.hpp"
9
#include "opencv2/highgui/highgui.hpp"
10
#include "opencv2/video/video.hpp"
15
typedef unsigned char uchar;
20
static void workBegin()
22
work_begin = getTickCount();
26
work_end += (getTickCount() - work_begin);
28
static double getTime()
30
return work_end * 1000. / getTickFrequency();
33
template <typename T> inline T clamp (T x, T a, T b)
35
return ((x) > (a) ? ((x) < (b) ? (x) : (b)) : (a));
38
template <typename T> inline T mapValue(T x, T a, T b, T c, T d)
41
return c + (d - c) * (x - a) / (b - a);
44
static void getFlowField(const Mat& u, const Mat& v, Mat& flowField)
46
float maxDisplacement = 1.0f;
48
for (int i = 0; i < u.rows; ++i)
50
const float* ptr_u = u.ptr<float>(i);
51
const float* ptr_v = v.ptr<float>(i);
53
for (int j = 0; j < u.cols; ++j)
55
float d = max(fabsf(ptr_u[j]), fabsf(ptr_v[j]));
57
if (d > maxDisplacement)
62
flowField.create(u.size(), CV_8UC4);
64
for (int i = 0; i < flowField.rows; ++i)
66
const float* ptr_u = u.ptr<float>(i);
67
const float* ptr_v = v.ptr<float>(i);
70
Vec4b* row = flowField.ptr<Vec4b>(i);
72
for (int j = 0; j < flowField.cols; ++j)
75
row[j][1] = static_cast<unsigned char> (mapValue (-ptr_v[j], -maxDisplacement, maxDisplacement, 0.0f, 255.0f));
76
row[j][2] = static_cast<unsigned char> (mapValue ( ptr_u[j], -maxDisplacement, maxDisplacement, 0.0f, 255.0f));
83
int main(int argc, const char* argv[])
86
"{ h help | false | print help message }"
87
"{ l left | | specify left image }"
88
"{ r right | | specify right image }"
89
"{ o output | tvl1_output.jpg | specify output save path }"
90
"{ c camera | 0 | enable camera capturing }"
91
"{ m cpu_mode | false | run without OpenCL }"
92
"{ v video | | use video as input }";
94
CommandLineParser cmd(argc, argv, keys);
98
cout << "Usage: pyrlk_optical_flow [options]" << endl;
99
cout << "Available options:" << endl;
104
string fname0 = cmd.get<string>("l");
105
string fname1 = cmd.get<string>("r");
106
string vdofile = cmd.get<string>("v");
107
string outpath = cmd.get<string>("o");
108
bool useCPU = cmd.get<bool>("s");
109
bool useCamera = cmd.get<bool>("c");
110
int inputName = cmd.get<int>("c");
113
imread(fname0, cv::IMREAD_GRAYSCALE).copyTo(frame0);
114
imread(fname1, cv::IMREAD_GRAYSCALE).copyTo(frame1);
115
cv::Ptr<cv::DenseOpticalFlow> alg = cv::createOptFlow_DualTVL1();
119
vector<UMat> flow_vec;
120
if (frame0.empty() || frame1.empty())
125
VideoCapture capture;
126
UMat frame, frameCopy;
127
UMat frame0Gray, frame1Gray;
131
capture.open( inputName );
133
capture.open(vdofile.c_str());
135
if(!capture.isOpened())
138
cout << "Capture from CAM " << inputName << " didn't work" << endl;
140
cout << "Capture from file " << vdofile << " failed" <<endl;
144
cout << "In capture ..." << endl;
147
if( !capture.read(frame) )
152
frame.copyTo( frame0 );
153
cvtColor(frame0, frame0Gray, COLOR_BGR2GRAY);
159
frame.copyTo(frame1);
160
cvtColor(frame1, frame1Gray, COLOR_BGR2GRAY);
166
frame.copyTo(frame0);
167
cvtColor(frame0, frame0Gray, COLOR_BGR2GRAY);
172
alg->calc(ptr0, ptr1, flow);
173
split(flow, flow_vec);
176
frame1.copyTo(frameCopy);
178
frame0.copyTo(frameCopy);
179
getFlowField(flow_vec[0].getMat(ACCESS_READ), flow_vec[1].getMat(ACCESS_READ), show_flow);
180
imshow("tvl1 optical flow field", show_flow);
183
char key = (char)waitKey(10);
186
else if (key == 'm' || key == 'M')
188
ocl::setUseOpenCL(!cv::ocl::useOpenCL());
189
cout << "Switched to " << (ocl::useOpenCL() ? "OpenCL" : "CPU") << " mode\n";
198
if (cmd.has("cpu_mode"))
200
ocl::setUseOpenCL(false);
201
std::cout << "OpenCL was disabled" << std::endl;
203
for(int i = 0; i <= LOOP_NUM; i ++)
205
cout << "loop" << i << endl;
207
if (i > 0) workBegin();
209
alg->calc(frame0, frame1, flow);
210
split(flow, flow_vec);
212
if (i > 0 && i <= LOOP_NUM)
218
cout << "average CPU time (noCamera) : ";
220
cout << "average GPU time (noCamera) : ";
221
cout << getTime() / LOOP_NUM << " ms" << endl;
223
getFlowField(flow_vec[0].getMat(ACCESS_READ), flow_vec[1].getMat(ACCESS_READ), show_flow);
224
imshow("PyrLK [Sparse]", show_flow);
225
imwrite(outpath, show_flow);