Merge pull request #1704 from SpecLad:merge-2.4
[profile/ivi/opencv.git] / modules / ocl / perf / perf_opticalflow.cpp
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) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Fangfang Bai, fangfang@multicorewareinc.com
19 //    Jin Ma,       jin@multicorewareinc.com
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 //   * Redistribution's of source code must retain the above copyright notice,
25 //     this list of conditions and the following disclaimer.
26 //
27 //   * Redistribution's in binary form must reproduce the above copyright notice,
28 //     this list of conditions and the following disclaimer in the documentation
29 //     and/or other materials provided with the distribution.
30 //
31 //   * The name of the copyright holders may not be used to endorse or promote products
32 //     derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors as is and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46 #include "perf_precomp.hpp"
47
48 ///////////// PyrLKOpticalFlow ////////////////////////
49
50 using namespace perf;
51 using std::tr1::get;
52 using std::tr1::tuple;
53 using std::tr1::make_tuple;
54
55 CV_ENUM(LoadMode, IMREAD_GRAYSCALE, IMREAD_COLOR)
56
57 typedef tuple<int, tuple<string, string, LoadMode> > PyrLKOpticalFlowParamType;
58 typedef TestBaseWithParam<PyrLKOpticalFlowParamType> PyrLKOpticalFlowFixture;
59
60 PERF_TEST_P(PyrLKOpticalFlowFixture,
61             PyrLKOpticalFlow,
62             ::testing::Combine(
63                 ::testing::Values(1000, 2000, 4000),
64                 ::testing::Values(
65                     make_tuple<string, string, LoadMode>
66                     (
67                         string("gpu/opticalflow/rubberwhale1.png"),
68                         string("gpu/opticalflow/rubberwhale2.png"),
69                         LoadMode(IMREAD_COLOR)
70                         ),
71                     make_tuple<string, string, LoadMode>
72                     (
73                         string("gpu/stereobm/aloe-L.png"),
74                         string("gpu/stereobm/aloe-R.png"),
75                         LoadMode(IMREAD_GRAYSCALE)
76                         )
77                     )
78                 )
79             )
80 {
81     PyrLKOpticalFlowParamType params = GetParam();
82     tuple<string, string, LoadMode> fileParam = get<1>(params);
83     const int pointsCount = get<0>(params);
84     const int openMode = static_cast<int>(get<2>(fileParam));
85     const string fileName0 = get<0>(fileParam), fileName1 = get<1>(fileParam);
86     Mat frame0 = imread(getDataPath(fileName0), openMode);
87     Mat frame1 = imread(getDataPath(fileName1), openMode);
88
89     declare.in(frame0, frame1);
90
91     ASSERT_FALSE(frame0.empty()) << "can't load " << fileName0;
92     ASSERT_FALSE(frame1.empty()) << "can't load " << fileName1;
93
94     Mat grayFrame;
95     if (openMode == IMREAD_COLOR)
96         cvtColor(frame0, grayFrame, COLOR_BGR2GRAY);
97     else
98         grayFrame = frame0;
99
100     vector<Point2f> pts, nextPts;
101     vector<unsigned char> status;
102     vector<float> err;
103     goodFeaturesToTrack(grayFrame, pts, pointsCount, 0.01, 0.0);
104     Mat ptsMat(1, static_cast<int>(pts.size()), CV_32FC2, (void *)&pts[0]);
105
106     if (RUN_PLAIN_IMPL)
107     {
108         TEST_CYCLE()
109                 cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts, status, err);
110     }
111     else if (RUN_OCL_IMPL)
112     {
113         ocl::PyrLKOpticalFlow oclPyrLK;
114         ocl::oclMat oclFrame0(frame0), oclFrame1(frame1);
115         ocl::oclMat oclPts(ptsMat);
116         ocl::oclMat oclNextPts, oclStatus, oclErr;
117
118         OCL_TEST_CYCLE()
119                 oclPyrLK.sparse(oclFrame0, oclFrame1, oclPts, oclNextPts, oclStatus, &oclErr);
120     }
121     else
122         OCL_PERF_ELSE
123
124     int value = 0;
125     SANITY_CHECK(value);
126 }
127
128 PERF_TEST(tvl1flowFixture, tvl1flow)
129 {
130     Mat frame0 = imread(getDataPath("gpu/opticalflow/rubberwhale1.png"), cv::IMREAD_GRAYSCALE);
131     ASSERT_FALSE(frame0.empty()) << "can't load rubberwhale1.png";
132
133     Mat frame1 = imread(getDataPath("gpu/opticalflow/rubberwhale2.png"), cv::IMREAD_GRAYSCALE);
134     ASSERT_FALSE(frame1.empty()) << "can't load rubberwhale2.png";
135
136     const Size srcSize = frame0.size();
137     const double eps = 1.2;
138     Mat flow(srcSize, CV_32FC2), flow1(srcSize, CV_32FC1), flow2(srcSize, CV_32FC1);
139     declare.in(frame0, frame1).out(flow1, flow2).time(159);
140
141     if (RUN_PLAIN_IMPL)
142     {
143         Ptr<DenseOpticalFlow> alg = createOptFlow_DualTVL1();
144
145         TEST_CYCLE() alg->calc(frame0, frame1, flow);
146
147         alg->collectGarbage();
148         Mat flows[2] = { flow1, flow2 };
149         split(flow, flows);
150
151         SANITY_CHECK(flow1, eps);
152         SANITY_CHECK(flow2, eps);
153     }
154     else if (RUN_OCL_IMPL)
155     {
156         ocl::OpticalFlowDual_TVL1_OCL oclAlg;
157         ocl::oclMat oclFrame0(frame0), oclFrame1(frame1), oclFlow1(srcSize, CV_32FC1),
158                 oclFlow2(srcSize, CV_32FC1);
159
160         OCL_TEST_CYCLE() oclAlg(oclFrame0, oclFrame1, oclFlow1, oclFlow2);
161
162         oclAlg.collectGarbage();
163
164         oclFlow1.download(flow1);
165         oclFlow2.download(flow2);
166
167         SANITY_CHECK(flow1, eps);
168         SANITY_CHECK(flow2, eps);
169     }
170     else
171         OCL_PERF_ELSE
172 }
173
174 ///////////// FarnebackOpticalFlow ////////////////////////
175
176 CV_ENUM(farneFlagType, 0, OPTFLOW_FARNEBACK_GAUSSIAN)
177
178 typedef tuple<tuple<int, double>, farneFlagType, bool> FarnebackOpticalFlowParams;
179 typedef TestBaseWithParam<FarnebackOpticalFlowParams> FarnebackOpticalFlowFixture;
180
181 PERF_TEST_P(FarnebackOpticalFlowFixture, FarnebackOpticalFlow,
182             ::testing::Combine(
183                 ::testing::Values(make_tuple<int, double>(5, 1.1),
184                                   make_tuple<int, double>(7, 1.5)),
185                 farneFlagType::all(),
186                 ::testing::Bool()))
187 {
188     Mat frame0 = imread(getDataPath("gpu/opticalflow/rubberwhale1.png"), cv::IMREAD_GRAYSCALE);
189     ASSERT_FALSE(frame0.empty()) << "can't load rubberwhale1.png";
190
191     Mat frame1 = imread(getDataPath("gpu/opticalflow/rubberwhale2.png"), cv::IMREAD_GRAYSCALE);
192     ASSERT_FALSE(frame1.empty()) << "can't load rubberwhale2.png";
193
194     const Size srcSize = frame0.size();
195
196     const FarnebackOpticalFlowParams params = GetParam();
197     const tuple<int, double> polyParams = get<0>(params);
198     const int polyN = get<0>(polyParams), flags = get<1>(params);
199     const double polySigma = get<1>(polyParams), pyrScale = 0.5;
200     const bool useInitFlow = get<2>(params);
201     const double eps = 1.5;
202
203     Mat flowx(srcSize, CV_32FC1), flowy(srcSize, CV_32FC1), flow(srcSize, CV_32FC2);
204     declare.in(frame0, frame1).out(flowx, flowy);
205
206     ocl::FarnebackOpticalFlow farn;
207     farn.pyrScale = pyrScale;
208     farn.polyN = polyN;
209     farn.polySigma = polySigma;
210     farn.flags = flags;
211
212     if (RUN_PLAIN_IMPL)
213     {
214         if (useInitFlow)
215         {
216             calcOpticalFlowFarneback(
217                         frame0, frame1, flow, farn.pyrScale, farn.numLevels, farn.winSize,
218                         farn.numIters, farn.polyN, farn.polySigma, farn.flags);
219             farn.flags |= OPTFLOW_USE_INITIAL_FLOW;
220         }
221
222         TEST_CYCLE()
223                 calcOpticalFlowFarneback(
224                     frame0, frame1, flow, farn.pyrScale, farn.numLevels, farn.winSize,
225                     farn.numIters, farn.polyN, farn.polySigma, farn.flags);
226
227         Mat flowxy[2] = { flowx, flowy };
228         split(flow, flowxy);
229
230         SANITY_CHECK(flowx, eps);
231         SANITY_CHECK(flowy, eps);
232     }
233     else if (RUN_OCL_IMPL)
234     {
235         ocl::oclMat oclFrame0(frame0), oclFrame1(frame1),
236                 oclFlowx(srcSize, CV_32FC1), oclFlowy(srcSize, CV_32FC1);
237
238         if (useInitFlow)
239         {
240             farn(oclFrame0, oclFrame1, oclFlowx, oclFlowy);
241             farn.flags |= OPTFLOW_USE_INITIAL_FLOW;
242         }
243
244         OCL_TEST_CYCLE()
245                 farn(oclFrame0, oclFrame1, oclFlowx, oclFlowy);
246
247         oclFlowx.download(flowx);
248         oclFlowy.download(flowy);
249
250         SANITY_CHECK(flowx, eps);
251         SANITY_CHECK(flowy, eps);
252     }
253     else
254         OCL_PERF_ELSE
255 }