Home | History | Annotate | Download | only in src
      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 
     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:
     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 
     67         virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow, Stream& stream);
     68 
     69         virtual double getFlowSmoothness() const { return alpha_; }
     70         virtual void setFlowSmoothness(double alpha) { alpha_ = static_cast<float>(alpha); }
     71 
     72         virtual double getGradientConstancyImportance() const { return gamma_; }
     73         virtual void setGradientConstancyImportance(double gamma) { gamma_ = static_cast<float>(gamma); }
     74 
     75         virtual double getPyramidScaleFactor() const { return scale_factor_; }
     76         virtual void setPyramidScaleFactor(double scale_factor) { scale_factor_ = static_cast<float>(scale_factor); }
     77 
     78         //! number of lagged non-linearity iterations (inner loop)
     79         virtual int getInnerIterations() const { return inner_iterations_; }
     80         virtual void setInnerIterations(int inner_iterations) { inner_iterations_ = inner_iterations; }
     81 
     82         //! number of warping iterations (number of pyramid levels)
     83         virtual int getOuterIterations() const { return outer_iterations_; }
     84         virtual void setOuterIterations(int outer_iterations) { outer_iterations_ = outer_iterations; }
     85 
     86         //! number of linear system solver iterations
     87         virtual int getSolverIterations() const { return solver_iterations_; }
     88         virtual void setSolverIterations(int solver_iterations) { solver_iterations_ = solver_iterations; }
     89 
     90     private:
     91         //! flow smoothness
     92         float alpha_;
     93 
     94         //! gradient constancy importance
     95         float gamma_;
     96 
     97         //! pyramid scale factor
     98         float scale_factor_;
     99 
    100         //! number of lagged non-linearity iterations (inner loop)
    101         int inner_iterations_;
    102 
    103         //! number of warping iterations (number of pyramid levels)
    104         int outer_iterations_;
    105 
    106         //! number of linear system solver iterations
    107         int solver_iterations_;
    108     };
    109 
    110     static size_t getBufSize(const NCVBroxOpticalFlowDescriptor& desc,
    111                              const NCVMatrix<Ncv32f>& frame0, const NCVMatrix<Ncv32f>& frame1,
    112                              NCVMatrix<Ncv32f>& u, NCVMatrix<Ncv32f>& v,
    113                              size_t textureAlignment)
    114     {
    115         NCVMemStackAllocator gpuCounter(static_cast<Ncv32u>(textureAlignment));
    116 
    117         ncvSafeCall( NCVBroxOpticalFlow(desc, gpuCounter, frame0, frame1, u, v, 0) );
    118 
    119         return gpuCounter.maxSize();
    120     }
    121 
    122     static void outputHandler(const String &msg)
    123     {
    124         CV_Error(cv::Error::GpuApiCallError, msg.c_str());
    125     }
    126 
    127     void BroxOpticalFlowImpl::calc(InputArray _I0, InputArray _I1, InputOutputArray _flow, Stream& stream)
    128     {
    129         const GpuMat frame0 = _I0.getGpuMat();
    130         const GpuMat frame1 = _I1.getGpuMat();
    131 
    132         CV_Assert( frame0.type() == CV_32FC1 );
    133         CV_Assert( frame1.size() == frame0.size() && frame1.type() == frame0.type() );
    134 
    135         ncvSetDebugOutputHandler(outputHandler);
    136 
    137         BufferPool pool(stream);
    138         GpuMat u = pool.getBuffer(frame0.size(), CV_32FC1);
    139         GpuMat v = pool.getBuffer(frame0.size(), CV_32FC1);
    140 
    141         NCVBroxOpticalFlowDescriptor desc;
    142         desc.alpha = alpha_;
    143         desc.gamma = gamma_;
    144         desc.scale_factor = scale_factor_;
    145         desc.number_of_inner_iterations = inner_iterations_;
    146         desc.number_of_outer_iterations = outer_iterations_;
    147         desc.number_of_solver_iterations = solver_iterations_;
    148 
    149         NCVMemSegment frame0MemSeg;
    150         frame0MemSeg.begin.memtype = NCVMemoryTypeDevice;
    151         frame0MemSeg.begin.ptr = const_cast<uchar*>(frame0.data);
    152         frame0MemSeg.size = frame0.step * frame0.rows;
    153 
    154         NCVMemSegment frame1MemSeg;
    155         frame1MemSeg.begin.memtype = NCVMemoryTypeDevice;
    156         frame1MemSeg.begin.ptr = const_cast<uchar*>(frame1.data);
    157         frame1MemSeg.size = frame1.step * frame1.rows;
    158 
    159         NCVMemSegment uMemSeg;
    160         uMemSeg.begin.memtype = NCVMemoryTypeDevice;
    161         uMemSeg.begin.ptr = u.ptr();
    162         uMemSeg.size = u.step * u.rows;
    163 
    164         NCVMemSegment vMemSeg;
    165         vMemSeg.begin.memtype = NCVMemoryTypeDevice;
    166         vMemSeg.begin.ptr = v.ptr();
    167         vMemSeg.size = v.step * v.rows;
    168 
    169         DeviceInfo devInfo;
    170         size_t textureAlignment = devInfo.textureAlignment();
    171 
    172         NCVMatrixReuse<Ncv32f> frame0Mat(frame0MemSeg, static_cast<Ncv32u>(textureAlignment), frame0.cols, frame0.rows, static_cast<Ncv32u>(frame0.step));
    173         NCVMatrixReuse<Ncv32f> frame1Mat(frame1MemSeg, static_cast<Ncv32u>(textureAlignment), frame1.cols, frame1.rows, static_cast<Ncv32u>(frame1.step));
    174         NCVMatrixReuse<Ncv32f> uMat(uMemSeg, static_cast<Ncv32u>(textureAlignment), u.cols, u.rows, static_cast<Ncv32u>(u.step));
    175         NCVMatrixReuse<Ncv32f> vMat(vMemSeg, static_cast<Ncv32u>(textureAlignment), v.cols, v.rows, static_cast<Ncv32u>(v.step));
    176 
    177         size_t bufSize = getBufSize(desc, frame0Mat, frame1Mat, uMat, vMat, textureAlignment);
    178         GpuMat buf = pool.getBuffer(1, static_cast<int>(bufSize), CV_8UC1);
    179 
    180         NCVMemStackAllocator gpuAllocator(NCVMemoryTypeDevice, bufSize, static_cast<Ncv32u>(textureAlignment), buf.ptr());
    181 
    182         ncvSafeCall( NCVBroxOpticalFlow(desc, gpuAllocator, frame0Mat, frame1Mat, uMat, vMat, StreamAccessor::getStream(stream)) );
    183 
    184         GpuMat flows[] = {u, v};
    185         cuda::merge(flows, 2, _flow, stream);
    186     }
    187 }
    188 
    189 Ptr<BroxOpticalFlow> cv::cuda::BroxOpticalFlow::create(double alpha, double gamma, double scale_factor, int inner_iterations, int outer_iterations, int solver_iterations)
    190 {
    191     return makePtr<BroxOpticalFlowImpl>(alpha, gamma, scale_factor, inner_iterations, outer_iterations, solver_iterations);
    192 }
    193 
    194 #endif /* HAVE_CUDA */
    195