1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "precomp.hpp"
44
45 using namespace cv;
46 using namespace cv::cuda;
47
48 #if !defined (HAVE_CUDA) || !defined (HAVE_OPENCV_CUDALEGACY) || defined (CUDA_DISABLER)
49
create(double,double,double,int,int,int)50 Ptr<BroxOpticalFlow> cv::cuda::BroxOpticalFlow::create(double, double, double, int, int, int) { throw_no_cuda(); return Ptr<BroxOpticalFlow>(); }
51
52 #else
53
54 namespace {
55
56 class BroxOpticalFlowImpl : public BroxOpticalFlow
57 {
58 public:
BroxOpticalFlowImpl(double alpha,double gamma,double scale_factor,int inner_iterations,int outer_iterations,int solver_iterations)59 BroxOpticalFlowImpl(double alpha, double gamma, double scale_factor,
60 int inner_iterations, int outer_iterations, int solver_iterations) :
61 alpha_(alpha), gamma_(gamma), scale_factor_(scale_factor),
62 inner_iterations_(inner_iterations), outer_iterations_(outer_iterations),
63 solver_iterations_(solver_iterations)
64 {
65 }
66
getDefaultName() const67 virtual String getDefaultName() const { return "DenseOpticalFlow.BroxOpticalFlow"; }
68
69 virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow, Stream& stream);
70
getFlowSmoothness() const71 virtual double getFlowSmoothness() const { return alpha_; }
setFlowSmoothness(double alpha)72 virtual void setFlowSmoothness(double alpha) { alpha_ = static_cast<float>(alpha); }
73
getGradientConstancyImportance() const74 virtual double getGradientConstancyImportance() const { return gamma_; }
setGradientConstancyImportance(double gamma)75 virtual void setGradientConstancyImportance(double gamma) { gamma_ = static_cast<float>(gamma); }
76
getPyramidScaleFactor() const77 virtual double getPyramidScaleFactor() const { return scale_factor_; }
setPyramidScaleFactor(double scale_factor)78 virtual void setPyramidScaleFactor(double scale_factor) { scale_factor_ = static_cast<float>(scale_factor); }
79
80 //! number of lagged non-linearity iterations (inner loop)
getInnerIterations() const81 virtual int getInnerIterations() const { return inner_iterations_; }
setInnerIterations(int inner_iterations)82 virtual void setInnerIterations(int inner_iterations) { inner_iterations_ = inner_iterations; }
83
84 //! number of warping iterations (number of pyramid levels)
getOuterIterations() const85 virtual int getOuterIterations() const { return outer_iterations_; }
setOuterIterations(int outer_iterations)86 virtual void setOuterIterations(int outer_iterations) { outer_iterations_ = outer_iterations; }
87
88 //! number of linear system solver iterations
getSolverIterations() const89 virtual int getSolverIterations() const { return solver_iterations_; }
setSolverIterations(int solver_iterations)90 virtual void setSolverIterations(int solver_iterations) { solver_iterations_ = solver_iterations; }
91
92 private:
93 //! flow smoothness
94 float alpha_;
95
96 //! gradient constancy importance
97 float gamma_;
98
99 //! pyramid scale factor
100 float scale_factor_;
101
102 //! number of lagged non-linearity iterations (inner loop)
103 int inner_iterations_;
104
105 //! number of warping iterations (number of pyramid levels)
106 int outer_iterations_;
107
108 //! number of linear system solver iterations
109 int solver_iterations_;
110 };
111
getBufSize(const NCVBroxOpticalFlowDescriptor & desc,const NCVMatrix<Ncv32f> & frame0,const NCVMatrix<Ncv32f> & frame1,NCVMatrix<Ncv32f> & u,NCVMatrix<Ncv32f> & v,size_t textureAlignment)112 static size_t getBufSize(const NCVBroxOpticalFlowDescriptor& desc,
113 const NCVMatrix<Ncv32f>& frame0, const NCVMatrix<Ncv32f>& frame1,
114 NCVMatrix<Ncv32f>& u, NCVMatrix<Ncv32f>& v,
115 size_t textureAlignment)
116 {
117 NCVMemStackAllocator gpuCounter(static_cast<Ncv32u>(textureAlignment));
118
119 ncvSafeCall( NCVBroxOpticalFlow(desc, gpuCounter, frame0, frame1, u, v, 0) );
120
121 return gpuCounter.maxSize();
122 }
123
outputHandler(const String & msg)124 static void outputHandler(const String &msg)
125 {
126 CV_Error(cv::Error::GpuApiCallError, msg.c_str());
127 }
128
calc(InputArray _I0,InputArray _I1,InputOutputArray _flow,Stream & stream)129 void BroxOpticalFlowImpl::calc(InputArray _I0, InputArray _I1, InputOutputArray _flow, Stream& stream)
130 {
131 const GpuMat frame0 = _I0.getGpuMat();
132 const GpuMat frame1 = _I1.getGpuMat();
133
134 CV_Assert( frame0.type() == CV_32FC1 );
135 CV_Assert( frame1.size() == frame0.size() && frame1.type() == frame0.type() );
136
137 ncvSetDebugOutputHandler(outputHandler);
138
139 BufferPool pool(stream);
140 GpuMat u = pool.getBuffer(frame0.size(), CV_32FC1);
141 GpuMat v = pool.getBuffer(frame0.size(), CV_32FC1);
142
143 NCVBroxOpticalFlowDescriptor desc;
144 desc.alpha = alpha_;
145 desc.gamma = gamma_;
146 desc.scale_factor = scale_factor_;
147 desc.number_of_inner_iterations = inner_iterations_;
148 desc.number_of_outer_iterations = outer_iterations_;
149 desc.number_of_solver_iterations = solver_iterations_;
150
151 NCVMemSegment frame0MemSeg;
152 frame0MemSeg.begin.memtype = NCVMemoryTypeDevice;
153 frame0MemSeg.begin.ptr = const_cast<uchar*>(frame0.data);
154 frame0MemSeg.size = frame0.step * frame0.rows;
155
156 NCVMemSegment frame1MemSeg;
157 frame1MemSeg.begin.memtype = NCVMemoryTypeDevice;
158 frame1MemSeg.begin.ptr = const_cast<uchar*>(frame1.data);
159 frame1MemSeg.size = frame1.step * frame1.rows;
160
161 NCVMemSegment uMemSeg;
162 uMemSeg.begin.memtype = NCVMemoryTypeDevice;
163 uMemSeg.begin.ptr = u.ptr();
164 uMemSeg.size = u.step * u.rows;
165
166 NCVMemSegment vMemSeg;
167 vMemSeg.begin.memtype = NCVMemoryTypeDevice;
168 vMemSeg.begin.ptr = v.ptr();
169 vMemSeg.size = v.step * v.rows;
170
171 DeviceInfo devInfo;
172 size_t textureAlignment = devInfo.textureAlignment();
173
174 NCVMatrixReuse<Ncv32f> frame0Mat(frame0MemSeg, static_cast<Ncv32u>(textureAlignment), frame0.cols, frame0.rows, static_cast<Ncv32u>(frame0.step));
175 NCVMatrixReuse<Ncv32f> frame1Mat(frame1MemSeg, static_cast<Ncv32u>(textureAlignment), frame1.cols, frame1.rows, static_cast<Ncv32u>(frame1.step));
176 NCVMatrixReuse<Ncv32f> uMat(uMemSeg, static_cast<Ncv32u>(textureAlignment), u.cols, u.rows, static_cast<Ncv32u>(u.step));
177 NCVMatrixReuse<Ncv32f> vMat(vMemSeg, static_cast<Ncv32u>(textureAlignment), v.cols, v.rows, static_cast<Ncv32u>(v.step));
178
179 size_t bufSize = getBufSize(desc, frame0Mat, frame1Mat, uMat, vMat, textureAlignment);
180 GpuMat buf = pool.getBuffer(1, static_cast<int>(bufSize), CV_8UC1);
181
182 NCVMemStackAllocator gpuAllocator(NCVMemoryTypeDevice, bufSize, static_cast<Ncv32u>(textureAlignment), buf.ptr());
183
184 ncvSafeCall( NCVBroxOpticalFlow(desc, gpuAllocator, frame0Mat, frame1Mat, uMat, vMat, StreamAccessor::getStream(stream)) );
185
186 GpuMat flows[] = {u, v};
187 cuda::merge(flows, 2, _flow, stream);
188 }
189 }
190
create(double alpha,double gamma,double scale_factor,int inner_iterations,int outer_iterations,int solver_iterations)191 Ptr<BroxOpticalFlow> cv::cuda::BroxOpticalFlow::create(double alpha, double gamma, double scale_factor, int inner_iterations, int outer_iterations, int solver_iterations)
192 {
193 return makePtr<BroxOpticalFlowImpl>(alpha, gamma, scale_factor, inner_iterations, outer_iterations, solver_iterations);
194 }
195
196 #endif /* HAVE_CUDA */
197