refactor cudaoptflow public API:
* use opaque algorithm interfaces * add stream support
This commit is contained in:
		| @@ -44,256 +44,338 @@ | ||||
|  | ||||
| #if !defined HAVE_CUDA || defined(CUDA_DISABLER) | ||||
|  | ||||
| cv::cuda::OpticalFlowDual_TVL1_CUDA::OpticalFlowDual_TVL1_CUDA() { throw_no_cuda(); } | ||||
| void cv::cuda::OpticalFlowDual_TVL1_CUDA::operator ()(const GpuMat&, const GpuMat&, GpuMat&, GpuMat&) { throw_no_cuda(); } | ||||
| void cv::cuda::OpticalFlowDual_TVL1_CUDA::collectGarbage() {} | ||||
| void cv::cuda::OpticalFlowDual_TVL1_CUDA::procOneScale(const GpuMat&, const GpuMat&, GpuMat&, GpuMat&, GpuMat&) { throw_no_cuda(); } | ||||
| Ptr<OpticalFlowDual_TVL1> cv::cuda::OpticalFlowDual_TVL1::create(double, double, double, int, int, double, int, double, double, bool) { throw_no_cuda(); return Ptr<OpticalFlowDual_TVL1>(); } | ||||
|  | ||||
| #else | ||||
|  | ||||
| using namespace cv; | ||||
| using namespace cv::cuda; | ||||
|  | ||||
| cv::cuda::OpticalFlowDual_TVL1_CUDA::OpticalFlowDual_TVL1_CUDA() | ||||
| { | ||||
|     tau            = 0.25; | ||||
|     lambda         = 0.15; | ||||
|     theta          = 0.3; | ||||
|     nscales        = 5; | ||||
|     warps          = 5; | ||||
|     epsilon        = 0.01; | ||||
|     iterations     = 300; | ||||
|     scaleStep      = 0.8; | ||||
|     gamma           = 0.0; | ||||
|     useInitialFlow = false; | ||||
| } | ||||
|  | ||||
| void cv::cuda::OpticalFlowDual_TVL1_CUDA::operator ()(const GpuMat& I0, const GpuMat& I1, GpuMat& flowx, GpuMat& flowy) | ||||
| { | ||||
|     CV_Assert( I0.type() == CV_8UC1 || I0.type() == CV_32FC1 ); | ||||
|     CV_Assert( I0.size() == I1.size() ); | ||||
|     CV_Assert( I0.type() == I1.type() ); | ||||
|     CV_Assert( !useInitialFlow || (flowx.size() == I0.size() && flowx.type() == CV_32FC1 && flowy.size() == flowx.size() && flowy.type() == flowx.type()) ); | ||||
|     CV_Assert( nscales > 0 ); | ||||
|  | ||||
|     // allocate memory for the pyramid structure | ||||
|     I0s.resize(nscales); | ||||
|     I1s.resize(nscales); | ||||
|     u1s.resize(nscales); | ||||
|     u2s.resize(nscales); | ||||
|     u3s.resize(nscales); | ||||
|  | ||||
|     I0.convertTo(I0s[0], CV_32F, I0.depth() == CV_8U ? 1.0 : 255.0); | ||||
|     I1.convertTo(I1s[0], CV_32F, I1.depth() == CV_8U ? 1.0 : 255.0); | ||||
|  | ||||
|     if (!useInitialFlow) | ||||
|     { | ||||
|         flowx.create(I0.size(), CV_32FC1); | ||||
|         flowy.create(I0.size(), CV_32FC1); | ||||
|     } | ||||
|  | ||||
|     u1s[0] = flowx; | ||||
|     u2s[0] = flowy; | ||||
|     if (gamma) | ||||
|         u3s[0].create(I0.size(), CV_32FC1); | ||||
|  | ||||
|     I1x_buf.create(I0.size(), CV_32FC1); | ||||
|     I1y_buf.create(I0.size(), CV_32FC1); | ||||
|  | ||||
|     I1w_buf.create(I0.size(), CV_32FC1); | ||||
|     I1wx_buf.create(I0.size(), CV_32FC1); | ||||
|     I1wy_buf.create(I0.size(), CV_32FC1); | ||||
|  | ||||
|     grad_buf.create(I0.size(), CV_32FC1); | ||||
|     rho_c_buf.create(I0.size(), CV_32FC1); | ||||
|  | ||||
|     p11_buf.create(I0.size(), CV_32FC1); | ||||
|     p12_buf.create(I0.size(), CV_32FC1); | ||||
|     p21_buf.create(I0.size(), CV_32FC1); | ||||
|     p22_buf.create(I0.size(), CV_32FC1); | ||||
|     if (gamma) | ||||
|     { | ||||
|         p31_buf.create(I0.size(), CV_32FC1); | ||||
|         p32_buf.create(I0.size(), CV_32FC1); | ||||
|     } | ||||
|     diff_buf.create(I0.size(), CV_32FC1); | ||||
|  | ||||
|     // create the scales | ||||
|     for (int s = 1; s < nscales; ++s) | ||||
|     { | ||||
|         cuda::resize(I0s[s-1], I0s[s], Size(), scaleStep, scaleStep); | ||||
|         cuda::resize(I1s[s-1], I1s[s], Size(), scaleStep, scaleStep); | ||||
|  | ||||
|         if (I0s[s].cols < 16 || I0s[s].rows < 16) | ||||
|         { | ||||
|             nscales = s; | ||||
|             break; | ||||
|         } | ||||
|  | ||||
|         if (useInitialFlow) | ||||
|         { | ||||
|             cuda::resize(u1s[s-1], u1s[s], Size(), scaleStep, scaleStep); | ||||
|             cuda::resize(u2s[s-1], u2s[s], Size(), scaleStep, scaleStep); | ||||
|  | ||||
|             cuda::multiply(u1s[s], Scalar::all(scaleStep), u1s[s]); | ||||
|             cuda::multiply(u2s[s], Scalar::all(scaleStep), u2s[s]); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             u1s[s].create(I0s[s].size(), CV_32FC1); | ||||
|             u2s[s].create(I0s[s].size(), CV_32FC1); | ||||
|         } | ||||
|         if (gamma) | ||||
|             u3s[s].create(I0s[s].size(), CV_32FC1); | ||||
|     } | ||||
|  | ||||
|     if (!useInitialFlow) | ||||
|     { | ||||
|         u1s[nscales-1].setTo(Scalar::all(0)); | ||||
|         u2s[nscales-1].setTo(Scalar::all(0)); | ||||
|     } | ||||
|     if (gamma) | ||||
|         u3s[nscales - 1].setTo(Scalar::all(0)); | ||||
|  | ||||
|     // pyramidal structure for computing the optical flow | ||||
|     for (int s = nscales - 1; s >= 0; --s) | ||||
|     { | ||||
|         // compute the optical flow at the current scale | ||||
|         procOneScale(I0s[s], I1s[s], u1s[s], u2s[s], u3s[s]); | ||||
|  | ||||
|         // if this was the last scale, finish now | ||||
|         if (s == 0) | ||||
|             break; | ||||
|  | ||||
|         // otherwise, upsample the optical flow | ||||
|  | ||||
|         // zoom the optical flow for the next finer scale | ||||
|         cuda::resize(u1s[s], u1s[s - 1], I0s[s - 1].size()); | ||||
|         cuda::resize(u2s[s], u2s[s - 1], I0s[s - 1].size()); | ||||
|         if (gamma) | ||||
|             cuda::resize(u3s[s], u3s[s - 1], I0s[s - 1].size()); | ||||
|  | ||||
|         // scale the optical flow with the appropriate zoom factor | ||||
|         cuda::multiply(u1s[s - 1], Scalar::all(1/scaleStep), u1s[s - 1]); | ||||
|         cuda::multiply(u2s[s - 1], Scalar::all(1/scaleStep), u2s[s - 1]); | ||||
|     } | ||||
| } | ||||
|  | ||||
| namespace tvl1flow | ||||
| { | ||||
|     void centeredGradient(PtrStepSzf src, PtrStepSzf dx, PtrStepSzf dy); | ||||
|     void warpBackward(PtrStepSzf I0, PtrStepSzf I1, PtrStepSzf I1x, PtrStepSzf I1y, PtrStepSzf u1, PtrStepSzf u2, PtrStepSzf I1w, PtrStepSzf I1wx, PtrStepSzf I1wy, PtrStepSzf grad, PtrStepSzf rho); | ||||
|     void centeredGradient(PtrStepSzf src, PtrStepSzf dx, PtrStepSzf dy, cudaStream_t stream); | ||||
|     void warpBackward(PtrStepSzf I0, PtrStepSzf I1, PtrStepSzf I1x, PtrStepSzf I1y, | ||||
|                       PtrStepSzf u1, PtrStepSzf u2, | ||||
|                       PtrStepSzf I1w, PtrStepSzf I1wx, PtrStepSzf I1wy, | ||||
|                       PtrStepSzf grad, PtrStepSzf rho, | ||||
|                       cudaStream_t stream); | ||||
|     void estimateU(PtrStepSzf I1wx, PtrStepSzf I1wy, | ||||
|                    PtrStepSzf grad, PtrStepSzf rho_c, | ||||
|                    PtrStepSzf p11, PtrStepSzf p12, PtrStepSzf p21, PtrStepSzf p22, PtrStepSzf p31, PtrStepSzf p32, | ||||
|                    PtrStepSzf u1, PtrStepSzf u2, PtrStepSzf u3, PtrStepSzf error, | ||||
|                    float l_t, float theta, float gamma, bool calcError); | ||||
|     void estimateDualVariables(PtrStepSzf u1, PtrStepSzf u2, PtrStepSzf u3, PtrStepSzf p11, PtrStepSzf p12, PtrStepSzf p21, PtrStepSzf p22, PtrStepSzf p31, PtrStepSzf p32, float taut, const float gamma); | ||||
|                    float l_t, float theta, float gamma, bool calcError, | ||||
|                    cudaStream_t stream); | ||||
|     void estimateDualVariables(PtrStepSzf u1, PtrStepSzf u2, PtrStepSzf u3, | ||||
|                                PtrStepSzf p11, PtrStepSzf p12, PtrStepSzf p21, PtrStepSzf p22, PtrStepSzf p31, PtrStepSzf p32, | ||||
|                                float taut, float gamma, | ||||
|                                cudaStream_t stream); | ||||
| } | ||||
|  | ||||
| void cv::cuda::OpticalFlowDual_TVL1_CUDA::procOneScale(const GpuMat& I0, const GpuMat& I1, GpuMat& u1, GpuMat& u2, GpuMat& u3) | ||||
| namespace | ||||
| { | ||||
|     using namespace tvl1flow; | ||||
|  | ||||
|     const double scaledEpsilon = epsilon * epsilon * I0.size().area(); | ||||
|  | ||||
|     CV_DbgAssert( I1.size() == I0.size() ); | ||||
|     CV_DbgAssert( I1.type() == I0.type() ); | ||||
|     CV_DbgAssert( u1.size() == I0.size() ); | ||||
|     CV_DbgAssert( u2.size() == u1.size() ); | ||||
|  | ||||
|     GpuMat I1x = I1x_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     GpuMat I1y = I1y_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     centeredGradient(I1, I1x, I1y); | ||||
|  | ||||
|     GpuMat I1w = I1w_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     GpuMat I1wx = I1wx_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     GpuMat I1wy = I1wy_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|  | ||||
|     GpuMat grad = grad_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     GpuMat rho_c = rho_c_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|  | ||||
|     GpuMat p11 = p11_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     GpuMat p12 = p12_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     GpuMat p21 = p21_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     GpuMat p22 = p22_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     GpuMat p31, p32; | ||||
|     if (gamma) | ||||
|     class OpticalFlowDual_TVL1_Impl : public OpticalFlowDual_TVL1 | ||||
|     { | ||||
|         p31 = p31_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         p32 = p32_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|     } | ||||
|     p11.setTo(Scalar::all(0)); | ||||
|     p12.setTo(Scalar::all(0)); | ||||
|     p21.setTo(Scalar::all(0)); | ||||
|     p22.setTo(Scalar::all(0)); | ||||
|     if (gamma) | ||||
|     { | ||||
|         p31.setTo(Scalar::all(0)); | ||||
|         p32.setTo(Scalar::all(0)); | ||||
|     } | ||||
|  | ||||
|     GpuMat diff = diff_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|  | ||||
|     const float l_t = static_cast<float>(lambda * theta); | ||||
|     const float taut = static_cast<float>(tau / theta); | ||||
|  | ||||
|     for (int warpings = 0; warpings < warps; ++warpings) | ||||
|     { | ||||
|         warpBackward(I0, I1, I1x, I1y, u1, u2, I1w, I1wx, I1wy, grad, rho_c); | ||||
|  | ||||
|         double error = std::numeric_limits<double>::max(); | ||||
|         double prevError = 0.0; | ||||
|         for (int n = 0; error > scaledEpsilon && n < iterations; ++n) | ||||
|     public: | ||||
|         OpticalFlowDual_TVL1_Impl(double tau, double lambda, double theta, int nscales, int warps, double epsilon, | ||||
|                                   int iterations, double scaleStep, double gamma, bool useInitialFlow) : | ||||
|             tau_(tau), lambda_(lambda), gamma_(gamma), theta_(theta), nscales_(nscales), warps_(warps), | ||||
|             epsilon_(epsilon), iterations_(iterations), scaleStep_(scaleStep), useInitialFlow_(useInitialFlow) | ||||
|         { | ||||
|             // some tweaks to make sum operation less frequently | ||||
|             bool calcError = (epsilon > 0) && (n & 0x1) && (prevError < scaledEpsilon); | ||||
|             estimateU(I1wx, I1wy, grad, rho_c, p11, p12, p21, p22, p31, p32, u1, u2, u3, diff, l_t, static_cast<float>(theta), gamma, calcError); | ||||
|             if (calcError) | ||||
|         } | ||||
|  | ||||
|         virtual double getTau() const { return tau_; } | ||||
|         virtual void setTau(double tau) { tau_ = tau; } | ||||
|  | ||||
|         virtual double getLambda() const { return lambda_; } | ||||
|         virtual void setLambda(double lambda) { lambda_ = lambda; } | ||||
|  | ||||
|         virtual double getGamma() const { return gamma_; } | ||||
|         virtual void setGamma(double gamma) { gamma_ = gamma; } | ||||
|  | ||||
|         virtual double getTheta() const { return theta_; } | ||||
|         virtual void setTheta(double theta) { theta_ = theta; } | ||||
|  | ||||
|         virtual int getNumScales() const { return nscales_; } | ||||
|         virtual void setNumScales(int nscales) { nscales_ = nscales; } | ||||
|  | ||||
|         virtual int getNumWarps() const { return warps_; } | ||||
|         virtual void setNumWarps(int warps) { warps_ = warps; } | ||||
|  | ||||
|         virtual double getEpsilon() const { return epsilon_; } | ||||
|         virtual void setEpsilon(double epsilon) { epsilon_ = epsilon; } | ||||
|  | ||||
|         virtual int getNumIterations() const { return iterations_; } | ||||
|         virtual void setNumIterations(int iterations) { iterations_ = iterations; } | ||||
|  | ||||
|         virtual double getScaleStep() const { return scaleStep_; } | ||||
|         virtual void setScaleStep(double scaleStep) { scaleStep_ = scaleStep; } | ||||
|  | ||||
|         virtual bool getUseInitialFlow() const { return useInitialFlow_; } | ||||
|         virtual void setUseInitialFlow(bool useInitialFlow) { useInitialFlow_ = useInitialFlow; } | ||||
|  | ||||
|         virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow, Stream& stream); | ||||
|  | ||||
|     private: | ||||
|         double tau_; | ||||
|         double lambda_; | ||||
|         double gamma_; | ||||
|         double theta_; | ||||
|         int nscales_; | ||||
|         int warps_; | ||||
|         double epsilon_; | ||||
|         int iterations_; | ||||
|         double scaleStep_; | ||||
|         bool useInitialFlow_; | ||||
|  | ||||
|     private: | ||||
|         void calcImpl(const GpuMat& I0, const GpuMat& I1, GpuMat& flowx, GpuMat& flowy, Stream& stream); | ||||
|         void procOneScale(const GpuMat& I0, const GpuMat& I1, GpuMat& u1, GpuMat& u2, GpuMat& u3, Stream& stream); | ||||
|  | ||||
|         std::vector<GpuMat> I0s; | ||||
|         std::vector<GpuMat> I1s; | ||||
|         std::vector<GpuMat> u1s; | ||||
|         std::vector<GpuMat> u2s; | ||||
|         std::vector<GpuMat> u3s; | ||||
|  | ||||
|         GpuMat I1x_buf; | ||||
|         GpuMat I1y_buf; | ||||
|  | ||||
|         GpuMat I1w_buf; | ||||
|         GpuMat I1wx_buf; | ||||
|         GpuMat I1wy_buf; | ||||
|  | ||||
|         GpuMat grad_buf; | ||||
|         GpuMat rho_c_buf; | ||||
|  | ||||
|         GpuMat p11_buf; | ||||
|         GpuMat p12_buf; | ||||
|         GpuMat p21_buf; | ||||
|         GpuMat p22_buf; | ||||
|         GpuMat p31_buf; | ||||
|         GpuMat p32_buf; | ||||
|  | ||||
|         GpuMat diff_buf; | ||||
|         GpuMat norm_buf; | ||||
|     }; | ||||
|  | ||||
|     void OpticalFlowDual_TVL1_Impl::calc(InputArray _frame0, InputArray _frame1, InputOutputArray _flow, Stream& stream) | ||||
|     { | ||||
|         const GpuMat frame0 = _frame0.getGpuMat(); | ||||
|         const GpuMat frame1 = _frame1.getGpuMat(); | ||||
|  | ||||
|         BufferPool pool(stream); | ||||
|         GpuMat flowx = pool.getBuffer(frame0.size(), CV_32FC1); | ||||
|         GpuMat flowy = pool.getBuffer(frame0.size(), CV_32FC1); | ||||
|  | ||||
|         calcImpl(frame0, frame1, flowx, flowy, stream); | ||||
|  | ||||
|         GpuMat flows[] = {flowx, flowy}; | ||||
|         cuda::merge(flows, 2, _flow, stream); | ||||
|     } | ||||
|  | ||||
|     void OpticalFlowDual_TVL1_Impl::calcImpl(const GpuMat& I0, const GpuMat& I1, GpuMat& flowx, GpuMat& flowy, Stream& stream) | ||||
|     { | ||||
|         CV_Assert( I0.type() == CV_8UC1 || I0.type() == CV_32FC1 ); | ||||
|         CV_Assert( I0.size() == I1.size() ); | ||||
|         CV_Assert( I0.type() == I1.type() ); | ||||
|         CV_Assert( !useInitialFlow_ || (flowx.size() == I0.size() && flowx.type() == CV_32FC1 && flowy.size() == flowx.size() && flowy.type() == flowx.type()) ); | ||||
|         CV_Assert( nscales_ > 0 ); | ||||
|  | ||||
|         // allocate memory for the pyramid structure | ||||
|         I0s.resize(nscales_); | ||||
|         I1s.resize(nscales_); | ||||
|         u1s.resize(nscales_); | ||||
|         u2s.resize(nscales_); | ||||
|         u3s.resize(nscales_); | ||||
|  | ||||
|         I0.convertTo(I0s[0], CV_32F, I0.depth() == CV_8U ? 1.0 : 255.0, stream); | ||||
|         I1.convertTo(I1s[0], CV_32F, I1.depth() == CV_8U ? 1.0 : 255.0, stream); | ||||
|  | ||||
|         if (!useInitialFlow_) | ||||
|         { | ||||
|             flowx.create(I0.size(), CV_32FC1); | ||||
|             flowy.create(I0.size(), CV_32FC1); | ||||
|         } | ||||
|  | ||||
|         u1s[0] = flowx; | ||||
|         u2s[0] = flowy; | ||||
|         if (gamma_) | ||||
|         { | ||||
|             u3s[0].create(I0.size(), CV_32FC1); | ||||
|         } | ||||
|  | ||||
|         I1x_buf.create(I0.size(), CV_32FC1); | ||||
|         I1y_buf.create(I0.size(), CV_32FC1); | ||||
|  | ||||
|         I1w_buf.create(I0.size(), CV_32FC1); | ||||
|         I1wx_buf.create(I0.size(), CV_32FC1); | ||||
|         I1wy_buf.create(I0.size(), CV_32FC1); | ||||
|  | ||||
|         grad_buf.create(I0.size(), CV_32FC1); | ||||
|         rho_c_buf.create(I0.size(), CV_32FC1); | ||||
|  | ||||
|         p11_buf.create(I0.size(), CV_32FC1); | ||||
|         p12_buf.create(I0.size(), CV_32FC1); | ||||
|         p21_buf.create(I0.size(), CV_32FC1); | ||||
|         p22_buf.create(I0.size(), CV_32FC1); | ||||
|         if (gamma_) | ||||
|         { | ||||
|             p31_buf.create(I0.size(), CV_32FC1); | ||||
|             p32_buf.create(I0.size(), CV_32FC1); | ||||
|         } | ||||
|         diff_buf.create(I0.size(), CV_32FC1); | ||||
|  | ||||
|         // create the scales | ||||
|         for (int s = 1; s < nscales_; ++s) | ||||
|         { | ||||
|             cuda::resize(I0s[s-1], I0s[s], Size(), scaleStep_, scaleStep_, INTER_LINEAR, stream); | ||||
|             cuda::resize(I1s[s-1], I1s[s], Size(), scaleStep_, scaleStep_, INTER_LINEAR, stream); | ||||
|  | ||||
|             if (I0s[s].cols < 16 || I0s[s].rows < 16) | ||||
|             { | ||||
|                 error = cuda::sum(diff, norm_buf)[0]; | ||||
|                 prevError = error; | ||||
|                 nscales_ = s; | ||||
|                 break; | ||||
|             } | ||||
|  | ||||
|             if (useInitialFlow_) | ||||
|             { | ||||
|                 cuda::resize(u1s[s-1], u1s[s], Size(), scaleStep_, scaleStep_, INTER_LINEAR, stream); | ||||
|                 cuda::resize(u2s[s-1], u2s[s], Size(), scaleStep_, scaleStep_, INTER_LINEAR, stream); | ||||
|  | ||||
|                 cuda::multiply(u1s[s], Scalar::all(scaleStep_), u1s[s], 1, -1, stream); | ||||
|                 cuda::multiply(u2s[s], Scalar::all(scaleStep_), u2s[s], 1, -1, stream); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 error = std::numeric_limits<double>::max(); | ||||
|                 prevError -= scaledEpsilon; | ||||
|                 u1s[s].create(I0s[s].size(), CV_32FC1); | ||||
|                 u2s[s].create(I0s[s].size(), CV_32FC1); | ||||
|             } | ||||
|             if (gamma_) | ||||
|             { | ||||
|                 u3s[s].create(I0s[s].size(), CV_32FC1); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if (!useInitialFlow_) | ||||
|         { | ||||
|             u1s[nscales_-1].setTo(Scalar::all(0), stream); | ||||
|             u2s[nscales_-1].setTo(Scalar::all(0), stream); | ||||
|         } | ||||
|         if (gamma_) | ||||
|         { | ||||
|             u3s[nscales_ - 1].setTo(Scalar::all(0), stream); | ||||
|         } | ||||
|  | ||||
|         // pyramidal structure for computing the optical flow | ||||
|         for (int s = nscales_ - 1; s >= 0; --s) | ||||
|         { | ||||
|             // compute the optical flow at the current scale | ||||
|             procOneScale(I0s[s], I1s[s], u1s[s], u2s[s], u3s[s], stream); | ||||
|  | ||||
|             // if this was the last scale, finish now | ||||
|             if (s == 0) | ||||
|                 break; | ||||
|  | ||||
|             // otherwise, upsample the optical flow | ||||
|  | ||||
|             // zoom the optical flow for the next finer scale | ||||
|             cuda::resize(u1s[s], u1s[s - 1], I0s[s - 1].size(), 0, 0, INTER_LINEAR, stream); | ||||
|             cuda::resize(u2s[s], u2s[s - 1], I0s[s - 1].size(), 0, 0, INTER_LINEAR, stream); | ||||
|             if (gamma_) | ||||
|             { | ||||
|                 cuda::resize(u3s[s], u3s[s - 1], I0s[s - 1].size(), 0, 0, INTER_LINEAR, stream); | ||||
|             } | ||||
|  | ||||
|             estimateDualVariables(u1, u2, u3, p11, p12, p21, p22, p31, p32, taut, gamma); | ||||
|             // scale the optical flow with the appropriate zoom factor | ||||
|             cuda::multiply(u1s[s - 1], Scalar::all(1/scaleStep_), u1s[s - 1], 1, -1, stream); | ||||
|             cuda::multiply(u2s[s - 1], Scalar::all(1/scaleStep_), u2s[s - 1], 1, -1, stream); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     void OpticalFlowDual_TVL1_Impl::procOneScale(const GpuMat& I0, const GpuMat& I1, GpuMat& u1, GpuMat& u2, GpuMat& u3, Stream& _stream) | ||||
|     { | ||||
|         using namespace tvl1flow; | ||||
|  | ||||
|         cudaStream_t stream = StreamAccessor::getStream(_stream); | ||||
|  | ||||
|         const double scaledEpsilon = epsilon_ * epsilon_ * I0.size().area(); | ||||
|  | ||||
|         CV_DbgAssert( I1.size() == I0.size() ); | ||||
|         CV_DbgAssert( I1.type() == I0.type() ); | ||||
|         CV_DbgAssert( u1.size() == I0.size() ); | ||||
|         CV_DbgAssert( u2.size() == u1.size() ); | ||||
|  | ||||
|         GpuMat I1x = I1x_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         GpuMat I1y = I1y_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         centeredGradient(I1, I1x, I1y, stream); | ||||
|  | ||||
|         GpuMat I1w = I1w_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         GpuMat I1wx = I1wx_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         GpuMat I1wy = I1wy_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|  | ||||
|         GpuMat grad = grad_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         GpuMat rho_c = rho_c_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|  | ||||
|         GpuMat p11 = p11_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         GpuMat p12 = p12_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         GpuMat p21 = p21_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         GpuMat p22 = p22_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         GpuMat p31, p32; | ||||
|         if (gamma_) | ||||
|         { | ||||
|             p31 = p31_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|             p32 = p32_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|         } | ||||
|         p11.setTo(Scalar::all(0), _stream); | ||||
|         p12.setTo(Scalar::all(0), _stream); | ||||
|         p21.setTo(Scalar::all(0), _stream); | ||||
|         p22.setTo(Scalar::all(0), _stream); | ||||
|         if (gamma_) | ||||
|         { | ||||
|             p31.setTo(Scalar::all(0), _stream); | ||||
|             p32.setTo(Scalar::all(0), _stream); | ||||
|         } | ||||
|  | ||||
|         GpuMat diff = diff_buf(Rect(0, 0, I0.cols, I0.rows)); | ||||
|  | ||||
|         const float l_t = static_cast<float>(lambda_ * theta_); | ||||
|         const float taut = static_cast<float>(tau_ / theta_); | ||||
|  | ||||
|         for (int warpings = 0; warpings < warps_; ++warpings) | ||||
|         { | ||||
|             warpBackward(I0, I1, I1x, I1y, u1, u2, I1w, I1wx, I1wy, grad, rho_c, stream); | ||||
|  | ||||
|             double error = std::numeric_limits<double>::max(); | ||||
|             double prevError = 0.0; | ||||
|             for (int n = 0; error > scaledEpsilon && n < iterations_; ++n) | ||||
|             { | ||||
|                 // some tweaks to make sum operation less frequently | ||||
|                 bool calcError = (epsilon_ > 0) && (n & 0x1) && (prevError < scaledEpsilon); | ||||
|                 estimateU(I1wx, I1wy, grad, rho_c, p11, p12, p21, p22, p31, p32, u1, u2, u3, diff, l_t, static_cast<float>(theta_), gamma_, calcError, stream); | ||||
|                 if (calcError) | ||||
|                 { | ||||
|                     _stream.waitForCompletion(); | ||||
|                     error = cuda::sum(diff, norm_buf)[0]; | ||||
|                     prevError = error; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     error = std::numeric_limits<double>::max(); | ||||
|                     prevError -= scaledEpsilon; | ||||
|                 } | ||||
|  | ||||
|                 estimateDualVariables(u1, u2, u3, p11, p12, p21, p22, p31, p32, taut, gamma_, stream); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| void cv::cuda::OpticalFlowDual_TVL1_CUDA::collectGarbage() | ||||
| Ptr<OpticalFlowDual_TVL1> cv::cuda::OpticalFlowDual_TVL1::create( | ||||
|             double tau, double lambda, double theta, int nscales, int warps, | ||||
|             double epsilon, int iterations, double scaleStep, double gamma, bool useInitialFlow) | ||||
| { | ||||
|     I0s.clear(); | ||||
|     I1s.clear(); | ||||
|     u1s.clear(); | ||||
|     u2s.clear(); | ||||
|     u3s.clear(); | ||||
|  | ||||
|     I1x_buf.release(); | ||||
|     I1y_buf.release(); | ||||
|  | ||||
|     I1w_buf.release(); | ||||
|     I1wx_buf.release(); | ||||
|     I1wy_buf.release(); | ||||
|  | ||||
|     grad_buf.release(); | ||||
|     rho_c_buf.release(); | ||||
|  | ||||
|     p11_buf.release(); | ||||
|     p12_buf.release(); | ||||
|     p21_buf.release(); | ||||
|     p22_buf.release(); | ||||
|     if (gamma) | ||||
|     { | ||||
|         p31_buf.release(); | ||||
|         p32_buf.release(); | ||||
|     } | ||||
|     diff_buf.release(); | ||||
|     norm_buf.release(); | ||||
|     return makePtr<OpticalFlowDual_TVL1_Impl>(tau, lambda, theta, nscales, warps, | ||||
|                                               epsilon, iterations, scaleStep, gamma, useInitialFlow); | ||||
| } | ||||
|  | ||||
| #endif // !defined HAVE_CUDA || defined(CUDA_DISABLER) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Vladislav Vinogradov
					Vladislav Vinogradov