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