btv_l1_gpu.cpp 21 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

// S. Farsiu , D. Robinson, M. Elad, P. Milanfar. Fast and robust multiframe super resolution.
// Dennis Mitzel, Thomas Pock, Thomas Schoenemann, Daniel Cremers. Video Super Resolution using Duality Based TV-L1 Optical Flow.

#include "precomp.hpp"

wester committed
48
using namespace std;
wester committed
49
using namespace cv;
wester committed
50
using namespace cv::gpu;
wester committed
51 52 53
using namespace cv::superres;
using namespace cv::superres::detail;

wester committed
54
#if !defined(HAVE_CUDA) || !defined(HAVE_OPENCV_GPU) || defined(DYNAMIC_CUDA_SUPPORT)
wester committed
55

wester committed
56
Ptr<SuperResolution> cv::superres::createSuperResolution_BTVL1_GPU()
wester committed
57
{
wester committed
58
    CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
wester committed
59 60 61 62 63
    return Ptr<SuperResolution>();
}

#else // HAVE_CUDA

wester committed
64
namespace btv_l1_device
wester committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
{
    void buildMotionMaps(PtrStepSzf forwardMotionX, PtrStepSzf forwardMotionY,
                         PtrStepSzf backwardMotionX, PtrStepSzf bacwardMotionY,
                         PtrStepSzf forwardMapX, PtrStepSzf forwardMapY,
                         PtrStepSzf backwardMapX, PtrStepSzf backwardMapY);

    template <int cn>
    void upscale(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);

    void diffSign(PtrStepSzf src1, PtrStepSzf src2, PtrStepSzf dst, cudaStream_t stream);

    void loadBtvWeights(const float* weights, size_t count);
    template <int cn> void calcBtvRegularization(PtrStepSzb src, PtrStepSzb dst, int ksize);
}

namespace
{
wester committed
82 83
    void calcRelativeMotions(const vector<pair<GpuMat, GpuMat> >& forwardMotions, const vector<pair<GpuMat, GpuMat> >& backwardMotions,
                             vector<pair<GpuMat, GpuMat> >& relForwardMotions, vector<pair<GpuMat, GpuMat> >& relBackwardMotions,
wester committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                             int baseIdx, Size size)
    {
        const int count = static_cast<int>(forwardMotions.size());

        relForwardMotions.resize(count);
        relForwardMotions[baseIdx].first.create(size, CV_32FC1);
        relForwardMotions[baseIdx].first.setTo(Scalar::all(0));
        relForwardMotions[baseIdx].second.create(size, CV_32FC1);
        relForwardMotions[baseIdx].second.setTo(Scalar::all(0));

        relBackwardMotions.resize(count);
        relBackwardMotions[baseIdx].first.create(size, CV_32FC1);
        relBackwardMotions[baseIdx].first.setTo(Scalar::all(0));
        relBackwardMotions[baseIdx].second.create(size, CV_32FC1);
        relBackwardMotions[baseIdx].second.setTo(Scalar::all(0));

        for (int i = baseIdx - 1; i >= 0; --i)
        {
wester committed
102 103
            gpu::add(relForwardMotions[i + 1].first, forwardMotions[i].first, relForwardMotions[i].first);
            gpu::add(relForwardMotions[i + 1].second, forwardMotions[i].second, relForwardMotions[i].second);
wester committed
104

wester committed
105 106
            gpu::add(relBackwardMotions[i + 1].first, backwardMotions[i + 1].first, relBackwardMotions[i].first);
            gpu::add(relBackwardMotions[i + 1].second, backwardMotions[i + 1].second, relBackwardMotions[i].second);
wester committed
107 108 109 110
        }

        for (int i = baseIdx + 1; i < count; ++i)
        {
wester committed
111 112
            gpu::add(relForwardMotions[i - 1].first, backwardMotions[i].first, relForwardMotions[i].first);
            gpu::add(relForwardMotions[i - 1].second, backwardMotions[i].second, relForwardMotions[i].second);
wester committed
113

wester committed
114 115
            gpu::add(relBackwardMotions[i - 1].first, forwardMotions[i - 1].first, relBackwardMotions[i].first);
            gpu::add(relBackwardMotions[i - 1].second, forwardMotions[i - 1].second, relBackwardMotions[i].second);
wester committed
116 117 118
        }
    }

wester committed
119
    void upscaleMotions(const vector<pair<GpuMat, GpuMat> >& lowResMotions, vector<pair<GpuMat, GpuMat> >& highResMotions, int scale)
wester committed
120 121 122 123 124
    {
        highResMotions.resize(lowResMotions.size());

        for (size_t i = 0; i < lowResMotions.size(); ++i)
        {
wester committed
125 126
            gpu::resize(lowResMotions[i].first, highResMotions[i].first, Size(), scale, scale, INTER_CUBIC);
            gpu::resize(lowResMotions[i].second, highResMotions[i].second, Size(), scale, scale, INTER_CUBIC);
wester committed
127

wester committed
128 129
            gpu::multiply(highResMotions[i].first, Scalar::all(scale), highResMotions[i].first);
            gpu::multiply(highResMotions[i].second, Scalar::all(scale), highResMotions[i].second);
wester committed
130 131 132
        }
    }

wester committed
133 134
    void buildMotionMaps(const pair<GpuMat, GpuMat>& forwardMotion, const pair<GpuMat, GpuMat>& backwardMotion,
                         pair<GpuMat, GpuMat>& forwardMap, pair<GpuMat, GpuMat>& backwardMap)
wester committed
135 136 137 138 139 140 141
    {
        forwardMap.first.create(forwardMotion.first.size(), CV_32FC1);
        forwardMap.second.create(forwardMotion.first.size(), CV_32FC1);

        backwardMap.first.create(forwardMotion.first.size(), CV_32FC1);
        backwardMap.second.create(forwardMotion.first.size(), CV_32FC1);

wester committed
142
        btv_l1_device::buildMotionMaps(forwardMotion.first, forwardMotion.second,
wester committed
143 144 145 146 147 148 149 150 151 152
                                       backwardMotion.first, backwardMotion.second,
                                       forwardMap.first, forwardMap.second,
                                       backwardMap.first, backwardMap.second);
    }

    void upscale(const GpuMat& src, GpuMat& dst, int scale, Stream& stream)
    {
        typedef void (*func_t)(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);
        static const func_t funcs[] =
        {
wester committed
153
            0, btv_l1_device::upscale<1>, 0, btv_l1_device::upscale<3>, btv_l1_device::upscale<4>
wester committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        };

        CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );

        dst.create(src.rows * scale, src.cols * scale, src.type());
        dst.setTo(Scalar::all(0));

        const func_t func = funcs[src.channels()];

        func(src, dst, scale, StreamAccessor::getStream(stream));
    }

    void diffSign(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
    {
        dst.create(src1.size(), src1.type());

wester committed
170
        btv_l1_device::diffSign(src1.reshape(1), src2.reshape(1), dst.reshape(1), StreamAccessor::getStream(stream));
wester committed
171 172
    }

wester committed
173
    void calcBtvWeights(int btvKernelSize, double alpha, vector<float>& btvWeights)
wester committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187
    {
        const size_t size = btvKernelSize * btvKernelSize;

        btvWeights.resize(size);

        const int ksize = (btvKernelSize - 1) / 2;
        const float alpha_f = static_cast<float>(alpha);

        for (int m = 0, ind = 0; m <= ksize; ++m)
        {
            for (int l = ksize; l + m >= 0; --l, ++ind)
                btvWeights[ind] = pow(alpha_f, std::abs(m) + std::abs(l));
        }

wester committed
188
        btv_l1_device::loadBtvWeights(&btvWeights[0], size);
wester committed
189 190 191 192 193 194 195 196
    }

    void calcBtvRegularization(const GpuMat& src, GpuMat& dst, int btvKernelSize)
    {
        typedef void (*func_t)(PtrStepSzb src, PtrStepSzb dst, int ksize);
        static const func_t funcs[] =
        {
            0,
wester committed
197
            btv_l1_device::calcBtvRegularization<1>,
wester committed
198
            0,
wester committed
199 200
            btv_l1_device::calcBtvRegularization<3>,
            btv_l1_device::calcBtvRegularization<4>
wester committed
201 202 203 204 205 206 207 208 209 210
        };

        dst.create(src.size(), src.type());
        dst.setTo(Scalar::all(0));

        const int ksize = (btvKernelSize - 1) / 2;

        funcs[src.channels()](src, dst, ksize);
    }

wester committed
211
    class BTVL1_GPU_Base
wester committed
212 213
    {
    public:
wester committed
214
        BTVL1_GPU_Base();
wester committed
215

wester committed
216 217
        void process(const vector<GpuMat>& src, GpuMat& dst,
                     const vector<pair<GpuMat, GpuMat> >& forwardMotions, const vector<pair<GpuMat, GpuMat> >& backwardMotions,
wester committed
218 219 220 221 222 223 224 225 226 227 228 229 230
                     int baseIdx);

        void collectGarbage();

    protected:
        int scale_;
        int iterations_;
        double lambda_;
        double tau_;
        double alpha_;
        int btvKernelSize_;
        int blurKernelSize_;
        double blurSigma_;
wester committed
231
        Ptr<DenseOpticalFlowExt> opticalFlow_;
wester committed
232 233

    private:
wester committed
234
        vector<Ptr<FilterEngine_GPU> > filters_;
wester committed
235 236 237 238
        int curBlurKernelSize_;
        double curBlurSigma_;
        int curSrcType_;

wester committed
239
        vector<float> btvWeights_;
wester committed
240 241 242
        int curBtvKernelSize_;
        double curAlpha_;

wester committed
243 244
        vector<pair<GpuMat, GpuMat> > lowResForwardMotions_;
        vector<pair<GpuMat, GpuMat> > lowResBackwardMotions_;
wester committed
245

wester committed
246 247
        vector<pair<GpuMat, GpuMat> > highResForwardMotions_;
        vector<pair<GpuMat, GpuMat> > highResBackwardMotions_;
wester committed
248

wester committed
249 250
        vector<pair<GpuMat, GpuMat> > forwardMaps_;
        vector<pair<GpuMat, GpuMat> > backwardMaps_;
wester committed
251 252 253

        GpuMat highRes_;

wester committed
254 255 256
        vector<Stream> streams_;
        vector<GpuMat> diffTerms_;
        vector<GpuMat> a_, b_, c_;
wester committed
257 258 259
        GpuMat regTerm_;
    };

wester committed
260
    BTVL1_GPU_Base::BTVL1_GPU_Base()
wester committed
261 262 263 264 265 266 267 268 269
    {
        scale_ = 4;
        iterations_ = 180;
        lambda_ = 0.03;
        tau_ = 1.3;
        alpha_ = 0.7;
        btvKernelSize_ = 7;
        blurKernelSize_ = 5;
        blurSigma_ = 0.0;
wester committed
270
        opticalFlow_ = createOptFlow_Farneback_GPU();
wester committed
271 272 273 274 275 276 277 278 279

        curBlurKernelSize_ = -1;
        curBlurSigma_ = -1.0;
        curSrcType_ = -1;

        curBtvKernelSize_ = -1;
        curAlpha_ = -1.0;
    }

wester committed
280 281
    void BTVL1_GPU_Base::process(const vector<GpuMat>& src, GpuMat& dst,
                                 const vector<pair<GpuMat, GpuMat> >& forwardMotions, const vector<pair<GpuMat, GpuMat> >& backwardMotions,
wester committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
                                 int baseIdx)
    {
        CV_Assert( scale_ > 1 );
        CV_Assert( iterations_ > 0 );
        CV_Assert( tau_ > 0.0 );
        CV_Assert( alpha_ > 0.0 );
        CV_Assert( btvKernelSize_ > 0 && btvKernelSize_ <= 16 );
        CV_Assert( blurKernelSize_ > 0 );
        CV_Assert( blurSigma_ >= 0.0 );

        // update blur filter and btv weights

        if (filters_.size() != src.size() || blurKernelSize_ != curBlurKernelSize_ || blurSigma_ != curBlurSigma_ || src[0].type() != curSrcType_)
        {
            filters_.resize(src.size());
            for (size_t i = 0; i < src.size(); ++i)
wester committed
298
                filters_[i] = createGaussianFilter_GPU(src[0].type(), Size(blurKernelSize_, blurKernelSize_), blurSigma_);
wester committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
            curBlurKernelSize_ = blurKernelSize_;
            curBlurSigma_ = blurSigma_;
            curSrcType_ = src[0].type();
        }

        if (btvWeights_.empty() || btvKernelSize_ != curBtvKernelSize_ || alpha_ != curAlpha_)
        {
            calcBtvWeights(btvKernelSize_, alpha_, btvWeights_);
            curBtvKernelSize_ = btvKernelSize_;
            curAlpha_ = alpha_;
        }

        // calc motions between input frames

        calcRelativeMotions(forwardMotions, backwardMotions, lowResForwardMotions_, lowResBackwardMotions_, baseIdx, src[0].size());

        upscaleMotions(lowResForwardMotions_, highResForwardMotions_, scale_);
        upscaleMotions(lowResBackwardMotions_, highResBackwardMotions_, scale_);

        forwardMaps_.resize(highResForwardMotions_.size());
        backwardMaps_.resize(highResForwardMotions_.size());
        for (size_t i = 0; i < highResForwardMotions_.size(); ++i)
            buildMotionMaps(highResForwardMotions_[i], highResBackwardMotions_[i], forwardMaps_[i], backwardMaps_[i]);

        // initial estimation

        const Size lowResSize = src[0].size();
        const Size highResSize(lowResSize.width * scale_, lowResSize.height * scale_);

wester committed
328
        gpu::resize(src[baseIdx], highRes_, highResSize, 0, 0, INTER_CUBIC);
wester committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342

        // iterations

        streams_.resize(src.size());
        diffTerms_.resize(src.size());
        a_.resize(src.size());
        b_.resize(src.size());
        c_.resize(src.size());

        for (int i = 0; i < iterations_; ++i)
        {
            for (size_t k = 0; k < src.size(); ++k)
            {
                // a = M * Ih
wester committed
343
                gpu::remap(highRes_, a_[k], backwardMaps_[k].first, backwardMaps_[k].second, INTER_NEAREST, BORDER_REPLICATE, Scalar(), streams_[k]);
wester committed
344
                // b = HM * Ih
wester committed
345
                filters_[k]->apply(a_[k], b_[k], Rect(0,0,-1,-1), streams_[k]);
wester committed
346
                // c = DHF * Ih
wester committed
347
                gpu::resize(b_[k], c_[k], lowResSize, 0, 0, INTER_NEAREST, streams_[k]);
wester committed
348 349 350 351 352 353

                diffSign(src[k], c_[k], c_[k], streams_[k]);

                // a = Dt * diff
                upscale(c_[k], a_[k], scale_, streams_[k]);
                // b = HtDt * diff
wester committed
354
                filters_[k]->apply(a_[k], b_[k], Rect(0,0,-1,-1), streams_[k]);
wester committed
355
                // diffTerm = MtHtDt * diff
wester committed
356
                gpu::remap(b_[k], diffTerms_[k], forwardMaps_[k].first, forwardMaps_[k].second, INTER_NEAREST, BORDER_REPLICATE, Scalar(), streams_[k]);
wester committed
357 358 359 360 361
            }

            if (lambda_ > 0)
            {
                calcBtvRegularization(highRes_, regTerm_, btvKernelSize_);
wester committed
362
                gpu::addWeighted(highRes_, 1.0, regTerm_, -tau_ * lambda_, 0.0, highRes_);
wester committed
363 364 365 366 367
            }

            for (size_t k = 0; k < src.size(); ++k)
            {
                streams_[k].waitForCompletion();
wester committed
368
                gpu::addWeighted(highRes_, 1.0, diffTerms_[k], tau_, 0.0, highRes_);
wester committed
369 370 371 372 373 374 375
            }
        }

        Rect inner(btvKernelSize_, btvKernelSize_, highRes_.cols - 2 * btvKernelSize_, highRes_.rows - 2 * btvKernelSize_);
        highRes_(inner).copyTo(dst);
    }

wester committed
376
    void BTVL1_GPU_Base::collectGarbage()
wester committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    {
        filters_.clear();

        lowResForwardMotions_.clear();
        lowResBackwardMotions_.clear();

        highResForwardMotions_.clear();
        highResBackwardMotions_.clear();

        forwardMaps_.clear();
        backwardMaps_.clear();

        highRes_.release();

        diffTerms_.clear();
        a_.clear();
        b_.clear();
        c_.clear();
        regTerm_.release();
    }

////////////////////////////////////////////////////////////

wester committed
400
    class BTVL1_GPU : public SuperResolution, private BTVL1_GPU_Base
wester committed
401 402
    {
    public:
wester committed
403 404 405
        AlgorithmInfo* info() const;

        BTVL1_GPU();
wester committed
406 407 408 409 410 411 412 413

        void collectGarbage();

    protected:
        void initImpl(Ptr<FrameSource>& frameSource);
        void processImpl(Ptr<FrameSource>& frameSource, OutputArray output);

    private:
wester committed
414 415
        int temporalAreaRadius_;

wester committed
416 417 418 419 420 421
        void readNextFrame(Ptr<FrameSource>& frameSource);
        void processFrame(int idx);

        GpuMat curFrame_;
        GpuMat prevFrame_;

wester committed
422 423 424 425
        vector<GpuMat> frames_;
        vector<pair<GpuMat, GpuMat> > forwardMotions_;
        vector<pair<GpuMat, GpuMat> > backwardMotions_;
        vector<GpuMat> outputs_;
wester committed
426 427 428 429 430

        int storePos_;
        int procPos_;
        int outPos_;

wester committed
431 432 433
        vector<GpuMat> srcFrames_;
        vector<pair<GpuMat, GpuMat> > srcForwardMotions_;
        vector<pair<GpuMat, GpuMat> > srcBackwardMotions_;
wester committed
434 435 436
        GpuMat finalOutput_;
    };

wester committed
437 438 439 440 441 442 443 444 445 446 447 448 449
    CV_INIT_ALGORITHM(BTVL1_GPU, "SuperResolution.BTVL1_GPU",
                      obj.info()->addParam(obj, "scale", obj.scale_, false, 0, 0, "Scale factor.");
                      obj.info()->addParam(obj, "iterations", obj.iterations_, false, 0, 0, "Iteration count.");
                      obj.info()->addParam(obj, "tau", obj.tau_, false, 0, 0, "Asymptotic value of steepest descent method.");
                      obj.info()->addParam(obj, "lambda", obj.lambda_, false, 0, 0, "Weight parameter to balance data term and smoothness term.");
                      obj.info()->addParam(obj, "alpha", obj.alpha_, false, 0, 0, "Parameter of spacial distribution in Bilateral-TV.");
                      obj.info()->addParam(obj, "btvKernelSize", obj.btvKernelSize_, false, 0, 0, "Kernel size of Bilateral-TV filter.");
                      obj.info()->addParam(obj, "blurKernelSize", obj.blurKernelSize_, false, 0, 0, "Gaussian blur kernel size.");
                      obj.info()->addParam(obj, "blurSigma", obj.blurSigma_, false, 0, 0, "Gaussian blur sigma.");
                      obj.info()->addParam(obj, "temporalAreaRadius", obj.temporalAreaRadius_, false, 0, 0, "Radius of the temporal search area.");
                      obj.info()->addParam<DenseOpticalFlowExt>(obj, "opticalFlow", obj.opticalFlow_, false, 0, 0, "Dense optical flow algorithm."));

    BTVL1_GPU::BTVL1_GPU()
wester committed
450 451 452 453
    {
        temporalAreaRadius_ = 4;
    }

wester committed
454
    void BTVL1_GPU::collectGarbage()
wester committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    {
        curFrame_.release();
        prevFrame_.release();

        frames_.clear();
        forwardMotions_.clear();
        backwardMotions_.clear();
        outputs_.clear();

        srcFrames_.clear();
        srcForwardMotions_.clear();
        srcBackwardMotions_.clear();
        finalOutput_.release();

        SuperResolution::collectGarbage();
wester committed
470
        BTVL1_GPU_Base::collectGarbage();
wester committed
471 472
    }

wester committed
473
    void BTVL1_GPU::initImpl(Ptr<FrameSource>& frameSource)
wester committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    {
        const int cacheSize = 2 * temporalAreaRadius_ + 1;

        frames_.resize(cacheSize);
        forwardMotions_.resize(cacheSize);
        backwardMotions_.resize(cacheSize);
        outputs_.resize(cacheSize);

        storePos_ = -1;

        for (int t = -temporalAreaRadius_; t <= temporalAreaRadius_; ++t)
            readNextFrame(frameSource);

        for (int i = 0; i <= temporalAreaRadius_; ++i)
            processFrame(i);

        procPos_ = temporalAreaRadius_;
        outPos_ = -1;
    }

wester committed
494
    void BTVL1_GPU::processImpl(Ptr<FrameSource>& frameSource, OutputArray _output)
wester committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    {
        if (outPos_ >= storePos_)
        {
            _output.release();
            return;
        }

        readNextFrame(frameSource);

        if (procPos_ < storePos_)
        {
            ++procPos_;
            processFrame(procPos_);
        }

        ++outPos_;
        const GpuMat& curOutput = at(outPos_, outputs_);

wester committed
513
        if (_output.kind() == _InputArray::GPU_MAT)
wester committed
514 515 516 517 518 519 520 521
            curOutput.convertTo(_output.getGpuMatRef(), CV_8U);
        else
        {
            curOutput.convertTo(finalOutput_, CV_8U);
            arrCopy(finalOutput_, _output);
        }
    }

wester committed
522
    void BTVL1_GPU::readNextFrame(Ptr<FrameSource>& frameSource)
wester committed
523 524 525 526 527 528 529 530 531 532 533
    {
        frameSource->nextFrame(curFrame_);

        if (curFrame_.empty())
            return;

        ++storePos_;
        curFrame_.convertTo(at(storePos_, frames_), CV_32F);

        if (storePos_ > 0)
        {
wester committed
534 535
            pair<GpuMat, GpuMat>& forwardMotion = at(storePos_ - 1, forwardMotions_);
            pair<GpuMat, GpuMat>& backwardMotion = at(storePos_, backwardMotions_);
wester committed
536 537 538 539 540 541 542 543

            opticalFlow_->calc(prevFrame_, curFrame_, forwardMotion.first, forwardMotion.second);
            opticalFlow_->calc(curFrame_, prevFrame_, backwardMotion.first, backwardMotion.second);
        }

        curFrame_.copyTo(prevFrame_);
    }

wester committed
544
    void BTVL1_GPU::processFrame(int idx)
wester committed
545
    {
wester committed
546
        const int startIdx = max(idx - temporalAreaRadius_, 0);
wester committed
547
        const int procIdx = idx;
wester committed
548
        const int endIdx = min(startIdx + 2 * temporalAreaRadius_, storePos_);
wester committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

        const int count = endIdx - startIdx + 1;

        srcFrames_.resize(count);
        srcForwardMotions_.resize(count);
        srcBackwardMotions_.resize(count);

        int baseIdx = -1;

        for (int i = startIdx, k = 0; i <= endIdx; ++i, ++k)
        {
            if (i == procIdx)
                baseIdx = k;

            srcFrames_[k] = at(i, frames_);

            if (i < endIdx)
                srcForwardMotions_[k] = at(i, forwardMotions_);
            if (i > startIdx)
                srcBackwardMotions_[k] = at(i, backwardMotions_);
        }

        process(srcFrames_, at(idx, outputs_), srcForwardMotions_, srcBackwardMotions_, baseIdx);
    }
}

wester committed
575
Ptr<SuperResolution> cv::superres::createSuperResolution_BTVL1_GPU()
wester committed
576
{
wester committed
577
    return new BTVL1_GPU;
wester committed
578 579 580
}

#endif // HAVE_CUDA