Fix cudafeatures2d and cudastereo with new 3.0 APIs for ORB and StereoBM
[profile/ivi/opencv.git] / modules / cudafeatures2d / perf / perf_features2d.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 "perf_precomp.hpp"
44
45 using namespace std;
46 using namespace testing;
47 using namespace perf;
48
49 //////////////////////////////////////////////////////////////////////
50 // FAST
51
52 DEF_PARAM_TEST(Image_Threshold_NonMaxSuppression, string, int, bool);
53
54 PERF_TEST_P(Image_Threshold_NonMaxSuppression, FAST,
55             Combine(Values<string>("gpu/perf/aloe.png"),
56                     Values(20),
57                     Bool()))
58 {
59     const cv::Mat img = readImage(GET_PARAM(0), cv::IMREAD_GRAYSCALE);
60     ASSERT_FALSE(img.empty());
61
62     const int threshold = GET_PARAM(1);
63     const bool nonMaxSuppersion = GET_PARAM(2);
64
65     if (PERF_RUN_CUDA())
66     {
67         cv::cuda::FAST_CUDA d_fast(threshold, nonMaxSuppersion, 0.5);
68
69         const cv::cuda::GpuMat d_img(img);
70         cv::cuda::GpuMat d_keypoints;
71
72         TEST_CYCLE() d_fast(d_img, cv::cuda::GpuMat(), d_keypoints);
73
74         std::vector<cv::KeyPoint> gpu_keypoints;
75         d_fast.downloadKeypoints(d_keypoints, gpu_keypoints);
76
77         sortKeyPoints(gpu_keypoints);
78
79         SANITY_CHECK_KEYPOINTS(gpu_keypoints);
80     }
81     else
82     {
83         std::vector<cv::KeyPoint> cpu_keypoints;
84
85         TEST_CYCLE() cv::FAST(img, cpu_keypoints, threshold, nonMaxSuppersion);
86
87         SANITY_CHECK_KEYPOINTS(cpu_keypoints);
88     }
89 }
90
91 //////////////////////////////////////////////////////////////////////
92 // ORB
93
94 DEF_PARAM_TEST(Image_NFeatures, string, int);
95
96 PERF_TEST_P(Image_NFeatures, ORB,
97             Combine(Values<string>("gpu/perf/aloe.png"),
98                     Values(4000)))
99 {
100     declare.time(300.0);
101
102     const cv::Mat img = readImage(GET_PARAM(0), cv::IMREAD_GRAYSCALE);
103     ASSERT_FALSE(img.empty());
104
105     const int nFeatures = GET_PARAM(1);
106
107     if (PERF_RUN_CUDA())
108     {
109         cv::cuda::ORB_CUDA d_orb(nFeatures);
110
111         const cv::cuda::GpuMat d_img(img);
112         cv::cuda::GpuMat d_keypoints, d_descriptors;
113
114         TEST_CYCLE() d_orb(d_img, cv::cuda::GpuMat(), d_keypoints, d_descriptors);
115
116         std::vector<cv::KeyPoint> gpu_keypoints;
117         d_orb.downloadKeyPoints(d_keypoints, gpu_keypoints);
118
119         cv::Mat gpu_descriptors(d_descriptors);
120
121         gpu_keypoints.resize(10);
122         gpu_descriptors = gpu_descriptors.rowRange(0, 10);
123
124         sortKeyPoints(gpu_keypoints, gpu_descriptors);
125
126         SANITY_CHECK_KEYPOINTS(gpu_keypoints, 1e-4);
127         SANITY_CHECK(gpu_descriptors);
128     }
129     else
130     {
131         cv::Ptr<cv::ORB> orb;
132
133         std::vector<cv::KeyPoint> cpu_keypoints;
134         cv::Mat cpu_descriptors;
135
136         TEST_CYCLE() orb->detectAndCompute(img, cv::noArray(), cpu_keypoints, cpu_descriptors);
137
138         SANITY_CHECK_KEYPOINTS(cpu_keypoints);
139         SANITY_CHECK(cpu_descriptors);
140     }
141 }
142
143 //////////////////////////////////////////////////////////////////////
144 // BFMatch
145
146 DEF_PARAM_TEST(DescSize_Norm, int, NormType);
147
148 PERF_TEST_P(DescSize_Norm, BFMatch,
149             Combine(Values(64, 128, 256),
150                     Values(NormType(cv::NORM_L1), NormType(cv::NORM_L2), NormType(cv::NORM_HAMMING))))
151 {
152     declare.time(20.0);
153
154     const int desc_size = GET_PARAM(0);
155     const int normType = GET_PARAM(1);
156
157     const int type = normType == cv::NORM_HAMMING ? CV_8U : CV_32F;
158
159     cv::Mat query(3000, desc_size, type);
160     declare.in(query, WARMUP_RNG);
161
162     cv::Mat train(3000, desc_size, type);
163     declare.in(train, WARMUP_RNG);
164
165     if (PERF_RUN_CUDA())
166     {
167         cv::cuda::BFMatcher_CUDA d_matcher(normType);
168
169         const cv::cuda::GpuMat d_query(query);
170         const cv::cuda::GpuMat d_train(train);
171         cv::cuda::GpuMat d_trainIdx, d_distance;
172
173         TEST_CYCLE() d_matcher.matchSingle(d_query, d_train, d_trainIdx, d_distance);
174
175         std::vector<cv::DMatch> gpu_matches;
176         d_matcher.matchDownload(d_trainIdx, d_distance, gpu_matches);
177
178         SANITY_CHECK_MATCHES(gpu_matches);
179     }
180     else
181     {
182         cv::BFMatcher matcher(normType);
183
184         std::vector<cv::DMatch> cpu_matches;
185
186         TEST_CYCLE() matcher.match(query, train, cpu_matches);
187
188         SANITY_CHECK_MATCHES(cpu_matches);
189     }
190 }
191
192 //////////////////////////////////////////////////////////////////////
193 // BFKnnMatch
194
195 static void toOneRowMatches(const std::vector< std::vector<cv::DMatch> >& src, std::vector<cv::DMatch>& dst)
196 {
197     dst.clear();
198     for (size_t i = 0; i < src.size(); ++i)
199         for (size_t j = 0; j < src[i].size(); ++j)
200             dst.push_back(src[i][j]);
201 }
202
203 DEF_PARAM_TEST(DescSize_K_Norm, int, int, NormType);
204
205 PERF_TEST_P(DescSize_K_Norm, BFKnnMatch,
206             Combine(Values(64, 128, 256),
207                     Values(2, 3),
208                     Values(NormType(cv::NORM_L1), NormType(cv::NORM_L2))))
209 {
210     declare.time(30.0);
211
212     const int desc_size = GET_PARAM(0);
213     const int k = GET_PARAM(1);
214     const int normType = GET_PARAM(2);
215
216     const int type = normType == cv::NORM_HAMMING ? CV_8U : CV_32F;
217
218     cv::Mat query(3000, desc_size, type);
219     declare.in(query, WARMUP_RNG);
220
221     cv::Mat train(3000, desc_size, type);
222     declare.in(train, WARMUP_RNG);
223
224     if (PERF_RUN_CUDA())
225     {
226         cv::cuda::BFMatcher_CUDA d_matcher(normType);
227
228         const cv::cuda::GpuMat d_query(query);
229         const cv::cuda::GpuMat d_train(train);
230         cv::cuda::GpuMat d_trainIdx, d_distance, d_allDist;
231
232         TEST_CYCLE() d_matcher.knnMatchSingle(d_query, d_train, d_trainIdx, d_distance, d_allDist, k);
233
234         std::vector< std::vector<cv::DMatch> > matchesTbl;
235         d_matcher.knnMatchDownload(d_trainIdx, d_distance, matchesTbl);
236
237         std::vector<cv::DMatch> gpu_matches;
238         toOneRowMatches(matchesTbl, gpu_matches);
239
240         SANITY_CHECK_MATCHES(gpu_matches);
241     }
242     else
243     {
244         cv::BFMatcher matcher(normType);
245
246         std::vector< std::vector<cv::DMatch> > matchesTbl;
247
248         TEST_CYCLE() matcher.knnMatch(query, train, matchesTbl, k);
249
250         std::vector<cv::DMatch> cpu_matches;
251         toOneRowMatches(matchesTbl, cpu_matches);
252
253         SANITY_CHECK_MATCHES(cpu_matches);
254     }
255 }
256
257 //////////////////////////////////////////////////////////////////////
258 // BFRadiusMatch
259
260 PERF_TEST_P(DescSize_Norm, BFRadiusMatch,
261             Combine(Values(64, 128, 256),
262                     Values(NormType(cv::NORM_L1), NormType(cv::NORM_L2))))
263 {
264     declare.time(30.0);
265
266     const int desc_size = GET_PARAM(0);
267     const int normType = GET_PARAM(1);
268
269     const int type = normType == cv::NORM_HAMMING ? CV_8U : CV_32F;
270     const float maxDistance = 10000;
271
272     cv::Mat query(3000, desc_size, type);
273     declare.in(query, WARMUP_RNG);
274
275     cv::Mat train(3000, desc_size, type);
276     declare.in(train, WARMUP_RNG);
277
278     if (PERF_RUN_CUDA())
279     {
280         cv::cuda::BFMatcher_CUDA d_matcher(normType);
281
282         const cv::cuda::GpuMat d_query(query);
283         const cv::cuda::GpuMat d_train(train);
284         cv::cuda::GpuMat d_trainIdx, d_nMatches, d_distance;
285
286         TEST_CYCLE() d_matcher.radiusMatchSingle(d_query, d_train, d_trainIdx, d_distance, d_nMatches, maxDistance);
287
288         std::vector< std::vector<cv::DMatch> > matchesTbl;
289         d_matcher.radiusMatchDownload(d_trainIdx, d_distance, d_nMatches, matchesTbl);
290
291         std::vector<cv::DMatch> gpu_matches;
292         toOneRowMatches(matchesTbl, gpu_matches);
293
294         SANITY_CHECK_MATCHES(gpu_matches);
295     }
296     else
297     {
298         cv::BFMatcher matcher(normType);
299
300         std::vector< std::vector<cv::DMatch> > matchesTbl;
301
302         TEST_CYCLE() matcher.radiusMatch(query, train, matchesTbl, maxDistance);
303
304         std::vector<cv::DMatch> cpu_matches;
305         toOneRowMatches(matchesTbl, cpu_matches);
306
307         SANITY_CHECK_MATCHES(cpu_matches);
308     }
309 }