Home | History | Annotate | Download | only in perf
      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 "perf_precomp.hpp"
     44 #include "opencv2/ts/ocl_perf.hpp"
     45 
     46 using namespace std;
     47 using namespace std::tr1;
     48 using namespace testing;
     49 using namespace perf;
     50 using namespace cv;
     51 using namespace cv::superres;
     52 using namespace cv::cuda;
     53 
     54 namespace
     55 {
     56     class OneFrameSource_CPU : public FrameSource
     57     {
     58     public:
     59         explicit OneFrameSource_CPU(const Mat& frame) : frame_(frame) {}
     60 
     61         void nextFrame(OutputArray frame)
     62         {
     63             frame.getMatRef() = frame_;
     64         }
     65 
     66         void reset()
     67         {
     68         }
     69 
     70     private:
     71         Mat frame_;
     72     };
     73 
     74     class OneFrameSource_CUDA : public FrameSource
     75     {
     76     public:
     77         explicit OneFrameSource_CUDA(const GpuMat& frame) : frame_(frame) {}
     78 
     79         void nextFrame(OutputArray frame)
     80         {
     81             frame.getGpuMatRef() = frame_;
     82         }
     83 
     84         void reset()
     85         {
     86         }
     87 
     88     private:
     89         GpuMat frame_;
     90     };
     91 
     92     class ZeroOpticalFlow : public DenseOpticalFlowExt
     93     {
     94     public:
     95         virtual void calc(InputArray frame0, InputArray, OutputArray flow1, OutputArray flow2)
     96         {
     97             cv::Size size = frame0.size();
     98 
     99             if (!flow2.needed())
    100             {
    101                 flow1.create(size, CV_32FC2);
    102                 flow1.setTo(cv::Scalar::all(0));
    103             }
    104             else
    105             {
    106                 flow1.create(size, CV_32FC1);
    107                 flow2.create(size, CV_32FC1);
    108 
    109                 flow1.setTo(cv::Scalar::all(0));
    110                 flow2.setTo(cv::Scalar::all(0));
    111             }
    112         }
    113 
    114         virtual void collectGarbage()
    115         {
    116         }
    117     };
    118 }
    119 
    120 PERF_TEST_P(Size_MatType, SuperResolution_BTVL1,
    121             Combine(Values(szSmall64, szSmall128),
    122                     Values(MatType(CV_8UC1), MatType(CV_8UC3))))
    123 {
    124     declare.time(5 * 60);
    125 
    126     const Size size = get<0>(GetParam());
    127     const int type = get<1>(GetParam());
    128 
    129     Mat frame(size, type);
    130     declare.in(frame, WARMUP_RNG);
    131 
    132     const int scale = 2;
    133     const int iterations = 50;
    134     const int temporalAreaRadius = 1;
    135     Ptr<DenseOpticalFlowExt> opticalFlow(new ZeroOpticalFlow);
    136 
    137     if (PERF_RUN_CUDA())
    138     {
    139         Ptr<SuperResolution> superRes = createSuperResolution_BTVL1_CUDA();
    140 
    141         superRes->setScale(scale);
    142         superRes->setIterations(iterations);
    143         superRes->setTemporalAreaRadius(temporalAreaRadius);
    144         superRes->setOpticalFlow(opticalFlow);
    145 
    146         superRes->setInput(makePtr<OneFrameSource_CUDA>(GpuMat(frame)));
    147 
    148         GpuMat dst;
    149         superRes->nextFrame(dst);
    150 
    151         TEST_CYCLE_N(10) superRes->nextFrame(dst);
    152 
    153         CUDA_SANITY_CHECK(dst, 2);
    154     }
    155     else
    156     {
    157         Ptr<SuperResolution> superRes = createSuperResolution_BTVL1();
    158 
    159         superRes->setScale(scale);
    160         superRes->setIterations(iterations);
    161         superRes->setTemporalAreaRadius(temporalAreaRadius);
    162         superRes->setOpticalFlow(opticalFlow);
    163 
    164         superRes->setInput(makePtr<OneFrameSource_CPU>(frame));
    165 
    166         Mat dst;
    167         superRes->nextFrame(dst);
    168 
    169         TEST_CYCLE_N(10) superRes->nextFrame(dst);
    170 
    171         CPU_SANITY_CHECK(dst);
    172     }
    173 }
    174 
    175 #ifdef HAVE_OPENCL
    176 
    177 namespace cvtest {
    178 namespace ocl {
    179 
    180 typedef Size_MatType SuperResolution_BTVL1;
    181 
    182 OCL_PERF_TEST_P(SuperResolution_BTVL1 ,BTVL1,
    183             Combine(Values(szSmall64, szSmall128),
    184                     Values(MatType(CV_8UC1), MatType(CV_8UC3))))
    185 {
    186     Size_MatType_t params = GetParam();
    187     const Size size = get<0>(params);
    188     const int type = get<1>(params);
    189 
    190     Mat frame(size, type);
    191     UMat dst(1, 1, 0);
    192     declare.in(frame, WARMUP_RNG);
    193 
    194     const int scale = 2;
    195     const int iterations = 50;
    196     const int temporalAreaRadius = 1;
    197 
    198     Ptr<DenseOpticalFlowExt> opticalFlow(new ZeroOpticalFlow);
    199     Ptr<SuperResolution> superRes = createSuperResolution_BTVL1();
    200 
    201     superRes->setScale(scale);
    202     superRes->setIterations(iterations);
    203     superRes->setTemporalAreaRadius(temporalAreaRadius);
    204     superRes->setOpticalFlow(opticalFlow);
    205 
    206     superRes->setInput(makePtr<OneFrameSource_CPU>(frame));
    207 
    208     // skip first frame
    209     superRes->nextFrame(dst);
    210 
    211     OCL_TEST_CYCLE_N(10) superRes->nextFrame(dst);
    212 
    213     SANITY_CHECK_NOTHING();
    214 }
    215 
    216 } } // namespace cvtest::ocl
    217 
    218 #endif // HAVE_OPENCL
    219