5 #include "opencv2/highgui/highgui.hpp"
6 #include "opencv2/ocl/ocl.hpp"
7 #include "opencv2/video/video.hpp"
11 using namespace cv::ocl;
13 typedef unsigned char uchar;
18 static void workBegin()
20 work_begin = getTickCount();
24 work_end += (getTickCount() - work_begin);
26 static double getTime()
28 return work_end * 1000. / getTickFrequency();
31 template <typename T> inline T clamp (T x, T a, T b)
33 return ((x) > (a) ? ((x) < (b) ? (x) : (b)) : (a));
36 template <typename T> inline T mapValue(T x, T a, T b, T c, T d)
39 return c + (d - c) * (x - a) / (b - a);
42 static void getFlowField(const Mat& u, const Mat& v, Mat& flowField)
44 float maxDisplacement = 1.0f;
46 for (int i = 0; i < u.rows; ++i)
48 const float* ptr_u = u.ptr<float>(i);
49 const float* ptr_v = v.ptr<float>(i);
51 for (int j = 0; j < u.cols; ++j)
53 float d = max(fabsf(ptr_u[j]), fabsf(ptr_v[j]));
55 if (d > maxDisplacement)
60 flowField.create(u.size(), CV_8UC4);
62 for (int i = 0; i < flowField.rows; ++i)
64 const float* ptr_u = u.ptr<float>(i);
65 const float* ptr_v = v.ptr<float>(i);
68 Vec4b* row = flowField.ptr<Vec4b>(i);
70 for (int j = 0; j < flowField.cols; ++j)
73 row[j][1] = static_cast<unsigned char> (mapValue (-ptr_v[j], -maxDisplacement, maxDisplacement, 0.0f, 255.0f));
74 row[j][2] = static_cast<unsigned char> (mapValue ( ptr_u[j], -maxDisplacement, maxDisplacement, 0.0f, 255.0f));
81 int main(int argc, const char* argv[])
83 static std::vector<Info> ocl_info;
84 ocl::getDevice(ocl_info);
85 //if you want to use undefault device, set it here
86 setDevice(ocl_info[0]);
88 //set this to save kernel compile time from second time you run
89 ocl::setBinpath("./");
91 "{ h | help | false | print help message }"
92 "{ l | left | | specify left image }"
93 "{ r | right | | specify right image }"
94 "{ o | output | tvl1_output.jpg | specify output save path }"
95 "{ c | camera | 0 | enable camera capturing }"
96 "{ s | use_cpu | false | use cpu or gpu to process the image }"
97 "{ v | video | | use video as input }";
99 CommandLineParser cmd(argc, argv, keys);
101 if (cmd.get<bool>("help"))
103 cout << "Usage: pyrlk_optical_flow [options]" << endl;
104 cout << "Avaible options:" << endl;
109 bool defaultPicturesFail = false;
110 string fname0 = cmd.get<string>("l");
111 string fname1 = cmd.get<string>("r");
112 string vdofile = cmd.get<string>("v");
113 string outpath = cmd.get<string>("o");
114 bool useCPU = cmd.get<bool>("s");
115 bool useCamera = cmd.get<bool>("c");
116 int inputName = cmd.get<int>("c");
118 Mat frame0 = imread(fname0, cv::IMREAD_GRAYSCALE);
119 Mat frame1 = imread(fname1, cv::IMREAD_GRAYSCALE);
120 cv::Ptr<cv::DenseOpticalFlow> alg = cv::createOptFlow_DualTVL1();
121 cv::ocl::OpticalFlowDual_TVL1_OCL d_alg;
126 if (frame0.empty() || frame1.empty())
129 defaultPicturesFail = true;
130 CvCapture* capture = 0;
131 capture = cvCaptureFromCAM( inputName );
134 cout << "Can't load input images" << endl;
142 CvCapture* capture = 0;
143 Mat frame, frameCopy;
144 Mat frame0Gray, frame1Gray;
148 capture = cvCaptureFromCAM( inputName );
150 capture = cvCreateFileCapture(vdofile.c_str());
156 cout << "Capture from CAM " << c << " didn't work" << endl;
158 cout << "Capture from file " << vdofile << " failed" <<endl;
159 if (defaultPicturesFail)
166 cout << "In capture ..." << endl;
169 frame = cvQueryFrame( capture );
175 frame.copyTo( frame0 );
176 cvtColor(frame0, frame0Gray, COLOR_BGR2GRAY);
182 frame.copyTo(frame1);
183 cvtColor(frame1, frame1Gray, COLOR_BGR2GRAY);
189 frame.copyTo(frame0);
190 cvtColor(frame0, frame0Gray, COLOR_BGR2GRAY);
197 alg->calc(ptr0, ptr1, flow);
198 split(flow, flow_vec);
202 oclMat d_flowx, d_flowy;
203 d_alg(oclMat(ptr0), oclMat(ptr1), d_flowx, d_flowy);
204 d_flowx.download(flow_vec[0]);
205 d_flowy.download(flow_vec[1]);
208 frame1.copyTo(frameCopy);
210 frame0.copyTo(frameCopy);
211 getFlowField(flow_vec[0], flow_vec[1], show_flow);
212 imshow("PyrLK [Sparse]", show_flow);
215 if( waitKey( 10 ) >= 0 )
222 cvReleaseCapture( &capture );
227 oclMat d_flowx, d_flowy;
228 for(int i = 0; i <= LOOP_NUM; i ++)
230 cout << "loop" << i << endl;
232 if (i > 0) workBegin();
235 alg->calc(frame0, frame1, flow);
236 split(flow, flow_vec);
240 d_alg(oclMat(frame0), oclMat(frame1), d_flowx, d_flowy);
241 d_flowx.download(flow_vec[0]);
242 d_flowy.download(flow_vec[1]);
244 if (i > 0 && i <= LOOP_NUM)
250 cout << "average CPU time (noCamera) : ";
252 cout << "average GPU time (noCamera) : ";
253 cout << getTime() / LOOP_NUM << " ms" << endl;
255 getFlowField(flow_vec[0], flow_vec[1], show_flow);
256 imshow("PyrLK [Sparse]", show_flow);
257 imwrite(outpath, show_flow);