1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
18 // Sen Liu, swjtuls1987@126.com
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
23 // * Redistribution's of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
26 // * Redistribution's in binary form must reproduce the above copyright notice,
27 // this list of conditions and the following disclaimer in the documentation
28 // and/or other materials provided with the distribution.
30 // * The name of the copyright holders may not be used to endorse or promote products
31 // derived from this software without specific prior written permission.
33 // This software is provided by the copyright holders and contributors "as is" and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
47 #include "precomp.hpp"
48 #include "opencl_kernels.hpp"
49 #include "opencv2/video/tracking.hpp"
52 using namespace cv::ocl;
58 namespace optflow_farneback
67 inline void setGaussianBlurKernel(const float *c_gKer, int ksizeHalf)
69 cv::Mat t_gKer(1, ksizeHalf + 1, CV_32FC1, const_cast<float *>(c_gKer));
73 static void gaussianBlurOcl(const oclMat &src, int ksizeHalf, oclMat &dst)
75 string kernelName("gaussianBlur");
76 size_t localThreads[3] = { 256, 1, 1 };
77 size_t globalThreads[3] = { src.cols, src.rows, 1 };
78 int smem_size = (localThreads[0] + 2*ksizeHalf) * sizeof(float);
80 CV_Assert(dst.size() == src.size());
81 std::vector< std::pair<size_t, const void *> > args;
82 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
83 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
84 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&gKer.data));
85 args.push_back(std::make_pair(smem_size, (void *)NULL));
86 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
87 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
88 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.step));
89 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.step));
90 args.push_back(std::make_pair(sizeof(cl_int), (void *)&ksizeHalf));
92 openCLExecuteKernel(Context::getContext(), &optical_flow_farneback, kernelName,
93 globalThreads, localThreads, args, -1, -1);
96 static void polynomialExpansionOcl(const oclMat &src, int polyN, oclMat &dst)
98 string kernelName("polynomialExpansion");
99 size_t localThreads[3] = { 256, 1, 1 };
100 size_t globalThreads[3] = { divUp(src.cols, localThreads[0] - 2*polyN) * localThreads[0], src.rows, 1 };
101 int smem_size = 3 * localThreads[0] * sizeof(float);
103 std::vector< std::pair<size_t, const void *> > args;
104 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
105 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
106 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&g.data));
107 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&xg.data));
108 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&xxg.data));
109 args.push_back(std::make_pair(smem_size, (void *)NULL));
110 args.push_back(std::make_pair(sizeof(cl_float4), (void *)&ig));
111 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.rows));
112 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
113 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.step));
114 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.step));
117 sprintf(opt, "-D polyN=%d", polyN);
119 openCLExecuteKernel(Context::getContext(), &optical_flow_farneback, kernelName,
120 globalThreads, localThreads, args, -1, -1, opt);
123 static void updateMatricesOcl(const oclMat &flowx, const oclMat &flowy, const oclMat &R0, const oclMat &R1, oclMat &M)
125 string kernelName("updateMatrices");
126 size_t localThreads[3] = { 32, 8, 1 };
127 size_t globalThreads[3] = { flowx.cols, flowx.rows, 1 };
129 std::vector< std::pair<size_t, const void *> > args;
130 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&M.data));
131 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&flowx.data));
132 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&flowy.data));
133 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&R0.data));
134 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&R1.data));
135 args.push_back(std::make_pair(sizeof(cl_int), (void *)&flowx.rows));
136 args.push_back(std::make_pair(sizeof(cl_int), (void *)&flowx.cols));
137 args.push_back(std::make_pair(sizeof(cl_int), (void *)&M.step));
138 args.push_back(std::make_pair(sizeof(cl_int), (void *)&flowx.step));
139 args.push_back(std::make_pair(sizeof(cl_int), (void *)&flowy.step));
140 args.push_back(std::make_pair(sizeof(cl_int), (void *)&R0.step));
141 args.push_back(std::make_pair(sizeof(cl_int), (void *)&R1.step));
143 openCLExecuteKernel(Context::getContext(), &optical_flow_farneback, kernelName,
144 globalThreads, localThreads, args, -1, -1);
147 static void boxFilter5Ocl(const oclMat &src, int ksizeHalf, oclMat &dst)
149 string kernelName("boxFilter5");
150 int height = src.rows / 5;
151 size_t localThreads[3] = { 256, 1, 1 };
152 size_t globalThreads[3] = { src.cols, height, 1 };
153 int smem_size = (localThreads[0] + 2*ksizeHalf) * 5 * sizeof(float);
155 std::vector< std::pair<size_t, const void *> > args;
156 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
157 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
158 args.push_back(std::make_pair(smem_size, (void *)NULL));
159 args.push_back(std::make_pair(sizeof(cl_int), (void *)&height));
160 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
161 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.step));
162 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.step));
163 args.push_back(std::make_pair(sizeof(cl_int), (void *)&ksizeHalf));
165 openCLExecuteKernel(Context::getContext(), &optical_flow_farneback, kernelName,
166 globalThreads, localThreads, args, -1, -1);
169 static void updateFlowOcl(const oclMat &M, oclMat &flowx, oclMat &flowy)
171 string kernelName("updateFlow");
172 int cols = divUp(flowx.cols, 4);
173 size_t localThreads[3] = { 32, 8, 1 };
174 size_t globalThreads[3] = { cols, flowx.rows, 1 };
176 std::vector< std::pair<size_t, const void *> > args;
177 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&flowx.data));
178 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&flowy.data));
179 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&M.data));
180 args.push_back(std::make_pair(sizeof(cl_int), (void *)&flowx.rows));
181 args.push_back(std::make_pair(sizeof(cl_int), (void *)&cols));
182 args.push_back(std::make_pair(sizeof(cl_int), (void *)&flowx.step));
183 args.push_back(std::make_pair(sizeof(cl_int), (void *)&flowy.step));
184 args.push_back(std::make_pair(sizeof(cl_int), (void *)&M.step));
186 openCLExecuteKernel(Context::getContext(), &optical_flow_farneback, kernelName,
187 globalThreads, localThreads, args, -1, -1);
190 static void gaussianBlur5Ocl(const oclMat &src, int ksizeHalf, oclMat &dst)
192 string kernelName("gaussianBlur5");
193 int height = src.rows / 5;
194 size_t localThreads[3] = { 256, 1, 1 };
195 size_t globalThreads[3] = { src.cols, height, 1 };
196 int smem_size = (localThreads[0] + 2*ksizeHalf) * 5 * sizeof(float);
198 std::vector< std::pair<size_t, const void *> > args;
199 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
200 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
201 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&gKer.data));
202 args.push_back(std::make_pair(smem_size, (void *)NULL));
203 args.push_back(std::make_pair(sizeof(cl_int), (void *)&height));
204 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
205 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.step));
206 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.step));
207 args.push_back(std::make_pair(sizeof(cl_int), (void *)&ksizeHalf));
209 openCLExecuteKernel(Context::getContext(), &optical_flow_farneback, kernelName,
210 globalThreads, localThreads, args, -1, -1);
214 } // namespace cv { namespace ocl { namespace optflow_farneback
216 static oclMat allocMatFromBuf(int rows, int cols, int type, oclMat &mat)
218 if (!mat.empty() && mat.type() == type && mat.rows >= rows && mat.cols >= cols)
219 return mat(Rect(0, 0, cols, rows));
220 return mat = oclMat(rows, cols, type);
223 cv::ocl::FarnebackOpticalFlow::FarnebackOpticalFlow()
227 fastPyramids = false;
235 void cv::ocl::FarnebackOpticalFlow::releaseMemory()
237 frames_[0].release();
238 frames_[1].release();
239 pyrLevel_[0].release();
240 pyrLevel_[1].release();
245 blurredFrame_[0].release();
246 blurredFrame_[1].release();
251 void cv::ocl::FarnebackOpticalFlow::prepareGaussian(
252 int n, double sigma, float *g, float *xg, float *xxg,
253 double &ig11, double &ig03, double &ig33, double &ig55)
256 for (int x = -n; x <= n; x++)
258 g[x] = (float)std::exp(-x*x/(2*sigma*sigma));
263 for (int x = -n; x <= n; x++)
265 g[x] = (float)(g[x]*s);
266 xg[x] = (float)(x*g[x]);
267 xxg[x] = (float)(x*x*g[x]);
270 Mat_<double> G(6, 6);
273 for (int y = -n; y <= n; y++)
275 for (int x = -n; x <= n; x++)
278 G(1,1) += g[y]*g[x]*x*x;
279 G(3,3) += g[y]*g[x]*x*x*x*x;
280 G(5,5) += g[y]*g[x]*x*x*y*y;
285 G(2,2) = G(0,3) = G(0,4) = G(3,0) = G(4,0) = G(1,1);
287 G(3,4) = G(4,3) = G(5,5);
296 Mat_<double> invG = G.inv(DECOMP_CHOLESKY);
304 void cv::ocl::FarnebackOpticalFlow::setPolynomialExpansionConsts(int n, double sigma)
306 vector<float> buf(n*6 + 3);
307 float* g = &buf[0] + n;
308 float* xg = g + n*2 + 1;
309 float* xxg = xg + n*2 + 1;
311 if (sigma < FLT_EPSILON)
314 double ig11, ig03, ig33, ig55;
315 prepareGaussian(n, sigma, g, xg, xxg, ig11, ig03, ig33, ig55);
317 cv::Mat t_g(1, n + 1, CV_32FC1, g);
318 cv::Mat t_xg(1, n + 1, CV_32FC1, xg);
319 cv::Mat t_xxg(1, n + 1, CV_32FC1, xxg);
321 optflow_farneback::g.upload(t_g);
322 optflow_farneback::xg.upload(t_xg);
323 optflow_farneback::xxg.upload(t_xxg);
325 optflow_farneback::ig[0] = static_cast<float>(ig11);
326 optflow_farneback::ig[1] = static_cast<float>(ig03);
327 optflow_farneback::ig[2] = static_cast<float>(ig33);
328 optflow_farneback::ig[3] = static_cast<float>(ig55);
331 void cv::ocl::FarnebackOpticalFlow::updateFlow_boxFilter(
332 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat &flowy,
333 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices)
335 optflow_farneback::boxFilter5Ocl(M, blockSize/2, bufM);
341 optflow_farneback::updateFlowOcl(M, flowx, flowy);
344 optflow_farneback::updateMatricesOcl(flowx, flowy, R0, R1, M);
348 void cv::ocl::FarnebackOpticalFlow::updateFlow_gaussianBlur(
349 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat& flowy,
350 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices)
352 optflow_farneback::gaussianBlur5Ocl(M, blockSize/2, bufM);
356 optflow_farneback::updateFlowOcl(M, flowx, flowy);
359 optflow_farneback::updateMatricesOcl(flowx, flowy, R0, R1, M);
363 void cv::ocl::FarnebackOpticalFlow::operator ()(
364 const oclMat &frame0, const oclMat &frame1, oclMat &flowx, oclMat &flowy)
366 CV_Assert(frame0.channels() == 1 && frame1.channels() == 1);
367 CV_Assert(frame0.size() == frame1.size());
368 CV_Assert(polyN == 5 || polyN == 7);
369 CV_Assert(!fastPyramids || std::abs(pyrScale - 0.5) < 1e-6);
371 Size size = frame0.size();
372 oclMat prevFlowX, prevFlowY, curFlowX, curFlowY;
374 flowx.create(size, CV_32F);
375 flowy.create(size, CV_32F);
376 oclMat flowx0 = flowx;
377 oclMat flowy0 = flowy;
379 // Crop unnecessary levels
381 int numLevelsCropped = 0;
382 for (; numLevelsCropped < numLevels; numLevelsCropped++)
385 if (size.width*scale < MIN_SIZE || size.height*scale < MIN_SIZE)
389 frame0.convertTo(frames_[0], CV_32F);
390 frame1.convertTo(frames_[1], CV_32F);
394 // Build Gaussian pyramids using pyrDown()
395 pyramid0_.resize(numLevelsCropped + 1);
396 pyramid1_.resize(numLevelsCropped + 1);
397 pyramid0_[0] = frames_[0];
398 pyramid1_[0] = frames_[1];
399 for (int i = 1; i <= numLevelsCropped; ++i)
401 pyrDown(pyramid0_[i - 1], pyramid0_[i]);
402 pyrDown(pyramid1_[i - 1], pyramid1_[i]);
406 setPolynomialExpansionConsts(polyN, polySigma);
408 for (int k = numLevelsCropped; k >= 0; k--)
411 for (int i = 0; i < k; i++)
414 double sigma = (1./scale - 1) * 0.5;
415 int smoothSize = cvRound(sigma*5) | 1;
416 smoothSize = std::max(smoothSize, 3);
418 int width = cvRound(size.width*scale);
419 int height = cvRound(size.height*scale);
423 width = pyramid0_[k].cols;
424 height = pyramid0_[k].rows;
429 curFlowX.create(height, width, CV_32F);
430 curFlowY.create(height, width, CV_32F);
440 if (flags & cv::OPTFLOW_USE_INITIAL_FLOW)
442 resize(flowx0, curFlowX, Size(width, height), 0, 0, INTER_LINEAR);
443 resize(flowy0, curFlowY, Size(width, height), 0, 0, INTER_LINEAR);
444 multiply(scale, curFlowX, curFlowX);
445 multiply(scale, curFlowY, curFlowY);
455 resize(prevFlowX, curFlowX, Size(width, height), 0, 0, INTER_LINEAR);
456 resize(prevFlowY, curFlowY, Size(width, height), 0, 0, INTER_LINEAR);
457 multiply(1./pyrScale, curFlowX, curFlowX);
458 multiply(1./pyrScale, curFlowY, curFlowY);
461 oclMat M = allocMatFromBuf(5*height, width, CV_32F, M_);
462 oclMat bufM = allocMatFromBuf(5*height, width, CV_32F, bufM_);
465 allocMatFromBuf(5*height, width, CV_32F, R_[0]),
466 allocMatFromBuf(5*height, width, CV_32F, R_[1])
471 optflow_farneback::polynomialExpansionOcl(pyramid0_[k], polyN, R[0]);
472 optflow_farneback::polynomialExpansionOcl(pyramid1_[k], polyN, R[1]);
476 oclMat blurredFrame[2] =
478 allocMatFromBuf(size.height, size.width, CV_32F, blurredFrame_[0]),
479 allocMatFromBuf(size.height, size.width, CV_32F, blurredFrame_[1])
483 allocMatFromBuf(height, width, CV_32F, pyrLevel_[0]),
484 allocMatFromBuf(height, width, CV_32F, pyrLevel_[1])
487 Mat g = getGaussianKernel(smoothSize, sigma, CV_32F);
488 optflow_farneback::setGaussianBlurKernel(g.ptr<float>(smoothSize/2), smoothSize/2);
490 for (int i = 0; i < 2; i++)
492 optflow_farneback::gaussianBlurOcl(frames_[i], smoothSize/2, blurredFrame[i]);
493 resize(blurredFrame[i], pyrLevel[i], Size(width, height), INTER_LINEAR);
494 optflow_farneback::polynomialExpansionOcl(pyrLevel[i], polyN, R[i]);
498 optflow_farneback::updateMatricesOcl(curFlowX, curFlowY, R[0], R[1], M);
500 if (flags & OPTFLOW_FARNEBACK_GAUSSIAN)
502 Mat g = getGaussianKernel(winSize, winSize/2*0.3f, CV_32F);
503 optflow_farneback::setGaussianBlurKernel(g.ptr<float>(winSize/2), winSize/2);
505 for (int i = 0; i < numIters; i++)
507 if (flags & OPTFLOW_FARNEBACK_GAUSSIAN)
508 updateFlow_gaussianBlur(R[0], R[1], curFlowX, curFlowY, M, bufM, winSize, i < numIters-1);
510 updateFlow_boxFilter(R[0], R[1], curFlowX, curFlowY, M, bufM, winSize, i < numIters-1);
513 prevFlowX = curFlowX;
514 prevFlowY = curFlowY;