CLAHE Python bindings
[profile/ivi/opencv.git] / modules / gpu / perf4au / main.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) 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 <cstdio>
44
45 #ifdef HAVE_CVCONFIG_H
46 #include "cvconfig.h"
47 #endif
48
49 #include "opencv2/ts/ts.hpp"
50 #include "opencv2/ts/gpu_perf.hpp"
51
52 #include "opencv2/core/core.hpp"
53 #include "opencv2/gpu/gpu.hpp"
54 #include "opencv2/highgui/highgui.hpp"
55 #include "opencv2/video/video.hpp"
56 #include "opencv2/legacy/legacy.hpp"
57
58 int main(int argc, char* argv[])
59 {
60     perf::printCudaInfo();
61
62     perf::Regression::Init("gpu_perf4au");
63     perf::TestBase::Init(argc, argv);
64     testing::InitGoogleTest(&argc, argv);
65
66     return RUN_ALL_TESTS();
67 }
68
69 //////////////////////////////////////////////////////////
70 // HoughLinesP
71
72 DEF_PARAM_TEST_1(Image, std::string);
73
74 PERF_TEST_P(Image, HoughLinesP, testing::Values(std::string("im1_1280x800.jpg")))
75 {
76     declare.time(30.0);
77
78     std::string fileName = GetParam();
79
80     const float rho = 1.f;
81     const float theta = 1.f;
82     const int threshold = 40;
83     const int minLineLenght = 20;
84     const int maxLineGap = 5;
85
86     cv::Mat image = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
87
88     if (PERF_RUN_GPU())
89     {
90         cv::gpu::GpuMat d_image(image);
91         cv::gpu::GpuMat d_lines;
92         cv::gpu::HoughLinesBuf d_buf;
93
94         cv::gpu::HoughLinesP(d_image, d_lines, d_buf, rho, theta, minLineLenght, maxLineGap);
95
96         TEST_CYCLE()
97         {
98             cv::gpu::HoughLinesP(d_image, d_lines, d_buf, rho, theta, minLineLenght, maxLineGap);
99         }
100     }
101     else
102     {
103         cv::Mat mask;
104         cv::Canny(image, mask, 50, 100);
105
106         std::vector<cv::Vec4i> lines;
107         cv::HoughLinesP(mask, lines, rho, theta, threshold, minLineLenght, maxLineGap);
108
109         TEST_CYCLE()
110         {
111             cv::HoughLinesP(mask, lines, rho, theta, threshold, minLineLenght, maxLineGap);
112         }
113     }
114
115     SANITY_CHECK(0);
116 }
117
118 //////////////////////////////////////////////////////////
119 // GoodFeaturesToTrack
120
121 DEF_PARAM_TEST(Image_Depth, std::string, perf::MatDepth);
122
123 PERF_TEST_P(Image_Depth, GoodFeaturesToTrack,
124                 testing::Combine(
125                 testing::Values(std::string("im1_1280x800.jpg")),
126                 testing::Values(CV_8U, CV_16U)
127                 ))
128 {
129     declare.time(60);
130
131     const std::string fileName = std::tr1::get<0>(GetParam());
132     const int depth = std::tr1::get<1>(GetParam());
133
134     const int maxCorners = 5000;
135     const double qualityLevel = 0.05;
136     const int minDistance = 5;
137     const int blockSize = 3;
138     const bool useHarrisDetector = true;
139     const double k = 0.05;
140
141     cv::Mat src = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
142     if (src.empty())
143         FAIL() << "Unable to load source image [" << fileName << "]";
144
145     if (depth != CV_8U)
146         src.convertTo(src, depth);
147
148     cv::Mat mask(src.size(), CV_8UC1, cv::Scalar::all(1));
149     mask(cv::Rect(0, 0, 100, 100)).setTo(cv::Scalar::all(0));
150
151     if (PERF_RUN_GPU())
152     {
153         cv::gpu::GoodFeaturesToTrackDetector_GPU d_detector(maxCorners, qualityLevel, minDistance, blockSize, useHarrisDetector, k);
154
155         cv::gpu::GpuMat d_src(src);
156         cv::gpu::GpuMat d_mask(mask);
157         cv::gpu::GpuMat d_pts;
158
159         d_detector(d_src, d_pts, d_mask);
160
161         TEST_CYCLE()
162         {
163             d_detector(d_src, d_pts, d_mask);
164         }
165     }
166     else
167     {
168         if (depth != CV_8U)
169             FAIL() << "Unsupported depth";
170
171         cv::Mat pts;
172
173         cv::goodFeaturesToTrack(src, pts, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k);
174
175         TEST_CYCLE()
176         {
177             cv::goodFeaturesToTrack(src, pts, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k);
178         }
179     }
180
181     SANITY_CHECK(0);
182 }
183
184 //////////////////////////////////////////////////////////
185 // OpticalFlowPyrLKSparse
186
187 typedef std::pair<std::string, std::string> string_pair;
188
189 DEF_PARAM_TEST(ImagePair_Depth_GraySource, string_pair, perf::MatDepth, bool);
190
191 PERF_TEST_P(ImagePair_Depth_GraySource, OpticalFlowPyrLKSparse,
192                 testing::Combine(
193                     testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
194                     testing::Values(CV_8U, CV_16U),
195                     testing::Bool()
196                     ))
197 {
198     declare.time(60);
199
200     const string_pair fileNames = std::tr1::get<0>(GetParam());
201     const int depth = std::tr1::get<1>(GetParam());
202     const bool graySource = std::tr1::get<2>(GetParam());
203
204     // PyrLK params
205     const cv::Size winSize(15, 15);
206     const int maxLevel = 5;
207     const cv::TermCriteria criteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 30, 0.01);
208
209     // GoodFeaturesToTrack params
210     const int maxCorners = 5000;
211     const double qualityLevel = 0.05;
212     const int minDistance = 5;
213     const int blockSize = 3;
214     const bool useHarrisDetector = true;
215     const double k = 0.05;
216
217     cv::Mat src1 = cv::imread(fileNames.first, graySource ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
218     if (src1.empty())
219         FAIL() << "Unable to load source image [" << fileNames.first << "]";
220
221     cv::Mat src2 = cv::imread(fileNames.second, graySource ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
222     if (src2.empty())
223         FAIL() << "Unable to load source image [" << fileNames.second << "]";
224
225     cv::Mat gray_src;
226     if (graySource)
227         gray_src = src1;
228     else
229         cv::cvtColor(src1, gray_src, cv::COLOR_BGR2GRAY);
230
231     cv::Mat pts;
232     cv::goodFeaturesToTrack(gray_src, pts, maxCorners, qualityLevel, minDistance, cv::noArray(), blockSize, useHarrisDetector, k);
233
234     if (depth != CV_8U)
235     {
236         src1.convertTo(src1, depth);
237         src2.convertTo(src2, depth);
238     }
239
240     if (PERF_RUN_GPU())
241     {
242         cv::gpu::GpuMat d_src1(src1);
243         cv::gpu::GpuMat d_src2(src2);
244         cv::gpu::GpuMat d_pts(pts.reshape(2, 1));
245         cv::gpu::GpuMat d_nextPts;
246         cv::gpu::GpuMat d_status;
247
248         cv::gpu::PyrLKOpticalFlow d_pyrLK;
249         d_pyrLK.winSize = winSize;
250         d_pyrLK.maxLevel = maxLevel;
251         d_pyrLK.iters = criteria.maxCount;
252         d_pyrLK.useInitialFlow = false;
253
254         d_pyrLK.sparse(d_src1, d_src2, d_pts, d_nextPts, d_status);
255
256         TEST_CYCLE()
257         {
258             d_pyrLK.sparse(d_src1, d_src2, d_pts, d_nextPts, d_status);
259         }
260     }
261     else
262     {
263         if (depth != CV_8U)
264             FAIL() << "Unsupported depth";
265
266         cv::Mat nextPts;
267         cv::Mat status;
268
269         cv::calcOpticalFlowPyrLK(src1, src2, pts, nextPts, status, cv::noArray(), winSize, maxLevel, criteria);
270
271         TEST_CYCLE()
272         {
273             cv::calcOpticalFlowPyrLK(src1, src2, pts, nextPts, status, cv::noArray(), winSize, maxLevel, criteria);
274         }
275     }
276
277     SANITY_CHECK(0);
278 }
279
280 //////////////////////////////////////////////////////////
281 // OpticalFlowFarneback
282
283 DEF_PARAM_TEST(ImagePair_Depth, string_pair, perf::MatDepth);
284
285 PERF_TEST_P(ImagePair_Depth, OpticalFlowFarneback,
286                 testing::Combine(
287                     testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
288                     testing::Values(CV_8U, CV_16U)
289                     ))
290 {
291     declare.time(500);
292
293     const string_pair fileNames = std::tr1::get<0>(GetParam());
294     const int depth = std::tr1::get<1>(GetParam());
295
296     const double pyrScale = 0.5;
297     const int numLevels = 6;
298     const int winSize = 7;
299     const int numIters = 15;
300     const int polyN = 7;
301     const double polySigma = 1.5;
302     const int flags = cv::OPTFLOW_USE_INITIAL_FLOW;
303
304     cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
305     if (src1.empty())
306         FAIL() << "Unable to load source image [" << fileNames.first << "]";
307
308     cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
309     if (src2.empty())
310         FAIL() << "Unable to load source image [" << fileNames.second << "]";
311
312     if (depth != CV_8U)
313     {
314         src1.convertTo(src1, depth);
315         src2.convertTo(src2, depth);
316     }
317
318     if (PERF_RUN_GPU())
319     {
320         cv::gpu::GpuMat d_src1(src1);
321         cv::gpu::GpuMat d_src2(src2);
322         cv::gpu::GpuMat d_u(src1.size(), CV_32FC1, cv::Scalar::all(0));
323         cv::gpu::GpuMat d_v(src1.size(), CV_32FC1, cv::Scalar::all(0));
324
325         cv::gpu::FarnebackOpticalFlow d_farneback;
326         d_farneback.pyrScale = pyrScale;
327         d_farneback.numLevels = numLevels;
328         d_farneback.winSize = winSize;
329         d_farneback.numIters = numIters;
330         d_farneback.polyN = polyN;
331         d_farneback.polySigma = polySigma;
332         d_farneback.flags = flags;
333
334         d_farneback(d_src1, d_src2, d_u, d_v);
335
336         TEST_CYCLE_N(10)
337         {
338             d_farneback(d_src1, d_src2, d_u, d_v);
339         }
340     }
341     else
342     {
343         if (depth != CV_8U)
344             FAIL() << "Unsupported depth";
345
346         cv::Mat flow(src1.size(), CV_32FC2, cv::Scalar::all(0));
347
348         cv::calcOpticalFlowFarneback(src1, src2, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
349
350         TEST_CYCLE_N(10)
351         {
352             cv::calcOpticalFlowFarneback(src1, src2, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
353         }
354     }
355
356     SANITY_CHECK(0);
357 }
358
359 //////////////////////////////////////////////////////////
360 // OpticalFlowBM
361
362 void calcOpticalFlowBM(const cv::Mat& prev, const cv::Mat& curr,
363                        cv::Size bSize, cv::Size shiftSize, cv::Size maxRange, int usePrevious,
364                        cv::Mat& velx, cv::Mat& vely)
365 {
366     cv::Size sz((curr.cols - bSize.width + shiftSize.width)/shiftSize.width, (curr.rows - bSize.height + shiftSize.height)/shiftSize.height);
367
368     velx.create(sz, CV_32FC1);
369     vely.create(sz, CV_32FC1);
370
371     CvMat cvprev = prev;
372     CvMat cvcurr = curr;
373
374     CvMat cvvelx = velx;
375     CvMat cvvely = vely;
376
377     cvCalcOpticalFlowBM(&cvprev, &cvcurr, bSize, shiftSize, maxRange, usePrevious, &cvvelx, &cvvely);
378 }
379
380 DEF_PARAM_TEST(ImagePair_BlockSize_ShiftSize_MaxRange, string_pair, cv::Size, cv::Size, cv::Size);
381
382 PERF_TEST_P(ImagePair_BlockSize_ShiftSize_MaxRange, OpticalFlowBM,
383                 testing::Combine(
384                     testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
385                     testing::Values(cv::Size(16, 16)),
386                     testing::Values(cv::Size(2, 2)),
387                     testing::Values(cv::Size(16, 16))
388                     ))
389 {
390     declare.time(3000);
391
392     const string_pair fileNames = std::tr1::get<0>(GetParam());
393     const cv::Size block_size = std::tr1::get<1>(GetParam());
394     const cv::Size shift_size = std::tr1::get<2>(GetParam());
395     const cv::Size max_range = std::tr1::get<3>(GetParam());
396
397     cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
398     if (src1.empty())
399         FAIL() << "Unable to load source image [" << fileNames.first << "]";
400
401     cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
402     if (src2.empty())
403         FAIL() << "Unable to load source image [" << fileNames.second << "]";
404
405     if (PERF_RUN_GPU())
406     {
407         cv::gpu::GpuMat d_src1(src1);
408         cv::gpu::GpuMat d_src2(src2);
409         cv::gpu::GpuMat d_velx, d_vely, buf;
410
411         cv::gpu::calcOpticalFlowBM(d_src1, d_src2, block_size, shift_size, max_range, false, d_velx, d_vely, buf);
412
413         TEST_CYCLE_N(10)
414         {
415             cv::gpu::calcOpticalFlowBM(d_src1, d_src2, block_size, shift_size, max_range, false, d_velx, d_vely, buf);
416         }
417     }
418     else
419     {
420         cv::Mat velx, vely;
421
422         calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
423
424         TEST_CYCLE_N(10)
425         {
426             calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
427         }
428     }
429
430     SANITY_CHECK(0);
431 }
432
433 PERF_TEST_P(ImagePair_BlockSize_ShiftSize_MaxRange, FastOpticalFlowBM,
434                 testing::Combine(
435                     testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
436                     testing::Values(cv::Size(16, 16)),
437                     testing::Values(cv::Size(1, 1)),
438                     testing::Values(cv::Size(16, 16))
439                     ))
440 {
441     declare.time(3000);
442
443     const string_pair fileNames = std::tr1::get<0>(GetParam());
444     const cv::Size block_size = std::tr1::get<1>(GetParam());
445     const cv::Size shift_size = std::tr1::get<2>(GetParam());
446     const cv::Size max_range = std::tr1::get<3>(GetParam());
447
448     cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
449     if (src1.empty())
450         FAIL() << "Unable to load source image [" << fileNames.first << "]";
451
452     cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
453     if (src2.empty())
454         FAIL() << "Unable to load source image [" << fileNames.second << "]";
455
456     if (PERF_RUN_GPU())
457     {
458         cv::gpu::GpuMat d_src1(src1);
459         cv::gpu::GpuMat d_src2(src2);
460         cv::gpu::GpuMat d_velx, d_vely;
461
462         cv::gpu::FastOpticalFlowBM fastBM;
463
464         fastBM(d_src1, d_src2, d_velx, d_vely, max_range.width, block_size.width);
465
466         TEST_CYCLE_N(10)
467         {
468             fastBM(d_src1, d_src2, d_velx, d_vely, max_range.width, block_size.width);
469         }
470     }
471     else
472     {
473         cv::Mat velx, vely;
474
475         calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
476
477         TEST_CYCLE_N(10)
478         {
479             calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
480         }
481     }
482
483     SANITY_CHECK(0);
484 }