cf3b0fc8cbfdb6966d2a41ecfb2ac4cddfe62b66
[profile/ivi/opencv.git] / modules / gpu / test / utility.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                        Intel License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.\r
14 // Third party copyrights are property of their respective owners.\r
15 //\r
16 // Redistribution and use in source and binary forms, with or without modification,\r
17 // are permitted provided that the following conditions are met:\r
18 //\r
19 //   * Redistribution's of source code must retain the above copyright notice,\r
20 //     this list of conditions and the following disclaimer.\r
21 //\r
22 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
23 //     this list of conditions and the following disclaimer in the documentation\r
24 //     and/or other materials provided with the distribution.\r
25 //\r
26 //   * The name of Intel Corporation may not be used to endorse or promote products\r
27 //     derived from this software without specific prior written permission.\r
28 //\r
29 // This software is provided by the copyright holders and contributors "as is" and\r
30 // any express or implied warranties, including, but not limited to, the implied\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
33 // indirect, incidental, special, exemplary, or consequential damages\r
34 // (including, but not limited to, procurement of substitute goods or services;\r
35 // loss of use, data, or profits; or business interruption) however caused\r
36 // and on any theory of liability, whether in contract, strict liability,\r
37 // or tort (including negligence or otherwise) arising in any way out of\r
38 // the use of this software, even if advised of the possibility of such damage.\r
39 //\r
40 //M*/\r
41 \r
42 #include "test_precomp.hpp"\r
43 \r
44 using namespace std;\r
45 using namespace cv;\r
46 using namespace cv::gpu;\r
47 using namespace cvtest;\r
48 using namespace testing;\r
49 using namespace testing::internal;\r
50 \r
51 //////////////////////////////////////////////////////////////////////\r
52 // random generators\r
53 \r
54 int randomInt(int minVal, int maxVal)\r
55 {\r
56     RNG& rng = TS::ptr()->get_rng();\r
57     return rng.uniform(minVal, maxVal);\r
58 }\r
59 \r
60 double randomDouble(double minVal, double maxVal)\r
61 {\r
62     RNG& rng = TS::ptr()->get_rng();\r
63     return rng.uniform(minVal, maxVal);\r
64 }\r
65 \r
66 Size randomSize(int minVal, int maxVal)\r
67 {\r
68     return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));\r
69 }\r
70 \r
71 Scalar randomScalar(double minVal, double maxVal)\r
72 {\r
73     return Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal));\r
74 }\r
75 \r
76 Mat randomMat(Size size, int type, double minVal, double maxVal)\r
77 {\r
78     return randomMat(TS::ptr()->get_rng(), size, type, minVal, maxVal, false);\r
79 }\r
80 \r
81 //////////////////////////////////////////////////////////////////////\r
82 // GpuMat create\r
83 \r
84 cv::gpu::GpuMat createMat(cv::Size size, int type, bool useRoi)\r
85 {\r
86     Size size0 = size;\r
87 \r
88     if (useRoi)\r
89     {\r
90         size0.width += randomInt(5, 15);\r
91         size0.height += randomInt(5, 15);\r
92     }\r
93 \r
94     GpuMat d_m(size0, type);\r
95 \r
96     if (size0 != size)\r
97         d_m = d_m(Rect((size0.width - size.width) / 2, (size0.height - size.height) / 2, size.width, size.height));\r
98 \r
99     return d_m;\r
100 }\r
101 \r
102 GpuMat loadMat(const Mat& m, bool useRoi)\r
103 {\r
104     GpuMat d_m = createMat(m.size(), m.type(), useRoi);\r
105     d_m.upload(m);\r
106     return d_m;\r
107 }\r
108 \r
109 //////////////////////////////////////////////////////////////////////\r
110 // Image load\r
111 \r
112 Mat readImage(const std::string& fileName, int flags)\r
113 {\r
114     return imread(TS::ptr()->get_data_path() + fileName, flags);\r
115 }\r
116 \r
117 Mat readImageType(const std::string& fname, int type)\r
118 {\r
119     Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? IMREAD_GRAYSCALE : IMREAD_COLOR);\r
120     if (CV_MAT_CN(type) == 4)\r
121     {\r
122         Mat temp;\r
123         cvtColor(src, temp, cv::COLOR_BGR2BGRA);\r
124         swap(src, temp);\r
125     }\r
126     src.convertTo(src, CV_MAT_DEPTH(type), CV_MAT_DEPTH(type) == CV_32F ? 1.0 / 255.0 : 1.0);\r
127     return src;\r
128 }\r
129 \r
130 //////////////////////////////////////////////////////////////////////\r
131 // Gpu devices\r
132 \r
133 bool supportFeature(const DeviceInfo& info, FeatureSet feature)\r
134 {\r
135     return TargetArchs::builtWith(feature) && info.supports(feature);\r
136 }\r
137 \r
138 DeviceManager& DeviceManager::instance()\r
139 {\r
140     static DeviceManager obj;\r
141     return obj;\r
142 }\r
143 \r
144 void DeviceManager::load(int i)\r
145 {\r
146     devices_.clear();\r
147     devices_.reserve(1);\r
148 \r
149     ostringstream msg;\r
150 \r
151     if (i < 0 || i >= getCudaEnabledDeviceCount())\r
152     {\r
153         msg << "Incorrect device number - " << i;\r
154         throw runtime_error(msg.str());\r
155     }\r
156 \r
157     DeviceInfo info(i);\r
158 \r
159     if (!info.isCompatible())\r
160     {\r
161         msg << "Device " << i << " [" << info.name() << "] is NOT compatible with current GPU module build";\r
162         throw runtime_error(msg.str());\r
163     }\r
164 \r
165     devices_.push_back(info);\r
166 }\r
167 \r
168 void DeviceManager::loadAll()\r
169 {\r
170     int deviceCount = getCudaEnabledDeviceCount();\r
171 \r
172     devices_.clear();\r
173     devices_.reserve(deviceCount);\r
174 \r
175     for (int i = 0; i < deviceCount; ++i)\r
176     {\r
177         DeviceInfo info(i);\r
178         if (info.isCompatible())\r
179         {\r
180             devices_.push_back(info);\r
181         }\r
182     }\r
183 }\r
184 \r
185 //////////////////////////////////////////////////////////////////////\r
186 // Additional assertion\r
187 \r
188 Mat getMat(InputArray arr)\r
189 {\r
190     if (arr.kind() == _InputArray::GPU_MAT)\r
191     {\r
192         Mat m;\r
193         arr.getGpuMat().download(m);\r
194         return m;\r
195     }\r
196 \r
197     return arr.getMat();\r
198 }\r
199 \r
200 double checkNorm(InputArray m1, InputArray m2)\r
201 {\r
202     return norm(getMat(m1), getMat(m2), NORM_INF);\r
203 }\r
204 \r
205 void minMaxLocGold(const Mat& src, double* minVal_, double* maxVal_, Point* minLoc_, Point* maxLoc_, const Mat& mask)\r
206 {\r
207     if (src.depth() != CV_8S)\r
208     {\r
209         minMaxLoc(src, minVal_, maxVal_, minLoc_, maxLoc_, mask);\r
210         return;\r
211     }\r
212 \r
213     // OpenCV's minMaxLoc doesn't support CV_8S type\r
214     double minVal = numeric_limits<double>::max();\r
215     Point minLoc(-1, -1);\r
216 \r
217     double maxVal = -numeric_limits<double>::max();\r
218     Point maxLoc(-1, -1);\r
219 \r
220     for (int y = 0; y < src.rows; ++y)\r
221     {\r
222         const schar* src_row = src.ptr<signed char>(y);\r
223         const uchar* mask_row = mask.empty() ? 0 : mask.ptr<unsigned char>(y);\r
224 \r
225         for (int x = 0; x < src.cols; ++x)\r
226         {\r
227             if (!mask_row || mask_row[x])\r
228             {\r
229                 schar val = src_row[x];\r
230 \r
231                 if (val < minVal)\r
232                 {\r
233                     minVal = val;\r
234                     minLoc = cv::Point(x, y);\r
235                 }\r
236 \r
237                 if (val > maxVal)\r
238                 {\r
239                     maxVal = val;\r
240                     maxLoc = cv::Point(x, y);\r
241                 }\r
242             }\r
243         }\r
244     }\r
245 \r
246     if (minVal_) *minVal_ = minVal;\r
247     if (maxVal_) *maxVal_ = maxVal;\r
248 \r
249     if (minLoc_) *minLoc_ = minLoc;\r
250     if (maxLoc_) *maxLoc_ = maxLoc;\r
251 }\r
252 \r
253 namespace\r
254 {\r
255     template <typename T, typename OutT> std::string printMatValImpl(const Mat& m, Point p)\r
256     {\r
257         const int cn = m.channels();\r
258 \r
259         ostringstream ostr;\r
260         ostr << "(";\r
261 \r
262         p.x /= cn;\r
263 \r
264         ostr << static_cast<OutT>(m.at<T>(p.y, p.x * cn));\r
265         for (int c = 1; c < m.channels(); ++c)\r
266         {\r
267             ostr << ", " << static_cast<OutT>(m.at<T>(p.y, p.x * cn + c));\r
268         }\r
269         ostr << ")";\r
270 \r
271         return ostr.str();\r
272     }\r
273 \r
274     std::string printMatVal(const Mat& m, Point p)\r
275     {\r
276         typedef std::string (*func_t)(const Mat& m, Point p);\r
277 \r
278         static const func_t funcs[] =\r
279         {\r
280             printMatValImpl<uchar, int>, printMatValImpl<schar, int>, printMatValImpl<ushort, int>, printMatValImpl<short, int>,\r
281             printMatValImpl<int, int>, printMatValImpl<float, float>, printMatValImpl<double, double>\r
282         };\r
283 \r
284         return funcs[m.depth()](m, p);\r
285     }\r
286 }\r
287 \r
288 testing::AssertionResult assertMatNear(const char* expr1, const char* expr2, const char* eps_expr, cv::InputArray m1_, cv::InputArray m2_, double eps)\r
289 {\r
290     Mat m1 = getMat(m1_);\r
291     Mat m2 = getMat(m2_);\r
292 \r
293     if (m1.size() != m2.size())\r
294     {\r
295         return AssertionFailure() << "Matrices \"" << expr1 << "\" and \"" << expr2 << "\" have different sizes : \""\r
296                                   << expr1 << "\" [" << PrintToString(m1.size()) << "] vs \""\r
297                                   << expr2 << "\" [" << PrintToString(m2.size()) << "]";\r
298     }\r
299 \r
300     if (m1.type() != m2.type())\r
301     {\r
302         return AssertionFailure() << "Matrices \"" << expr1 << "\" and \"" << expr2 << "\" have different types : \""\r
303                                   << expr1 << "\" [" << PrintToString(MatType(m1.type())) << "] vs \""\r
304                                   << expr2 << "\" [" << PrintToString(MatType(m2.type())) << "]";\r
305     }\r
306 \r
307     Mat diff;\r
308     absdiff(m1.reshape(1), m2.reshape(1), diff);\r
309 \r
310     double maxVal = 0.0;\r
311     Point maxLoc;\r
312     minMaxLocGold(diff, 0, &maxVal, 0, &maxLoc);\r
313 \r
314     if (maxVal > eps)\r
315     {\r
316         return AssertionFailure() << "The max difference between matrices \"" << expr1 << "\" and \"" << expr2\r
317                                   << "\" is " << maxVal << " at (" << maxLoc.y << ", " << maxLoc.x / m1.channels() << ")"\r
318                                   << ", which exceeds \"" << eps_expr << "\", where \""\r
319                                   << expr1 << "\" at (" << maxLoc.y << ", " << maxLoc.x / m1.channels() << ") evaluates to " << printMatVal(m1, maxLoc) << ", \""\r
320                                   << expr2 << "\" at (" << maxLoc.y << ", " << maxLoc.x / m1.channels() << ") evaluates to " << printMatVal(m2, maxLoc) << ", \""\r
321                                   << eps_expr << "\" evaluates to " << eps;\r
322     }\r
323 \r
324     return AssertionSuccess();\r
325 }\r
326 \r
327 double checkSimilarity(InputArray m1, InputArray m2)\r
328 {\r
329     Mat diff;\r
330     matchTemplate(getMat(m1), getMat(m2), diff, CV_TM_CCORR_NORMED);\r
331     return std::abs(diff.at<float>(0, 0) - 1.f);\r
332 }\r
333 \r
334 //////////////////////////////////////////////////////////////////////\r
335 // Helper structs for value-parameterized tests\r
336 \r
337 vector<MatDepth> depths(int depth_start, int depth_end)\r
338 {\r
339     vector<MatDepth> v;\r
340 \r
341     v.reserve((depth_end - depth_start + 1));\r
342 \r
343     for (int depth = depth_start; depth <= depth_end; ++depth)\r
344         v.push_back(depth);\r
345 \r
346     return v;\r
347 }\r
348 \r
349 vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end)\r
350 {\r
351     vector<MatType> v;\r
352 \r
353     v.reserve((depth_end - depth_start + 1) * (cn_end - cn_start + 1));\r
354 \r
355     for (int depth = depth_start; depth <= depth_end; ++depth)\r
356     {\r
357         for (int cn = cn_start; cn <= cn_end; ++cn)\r
358         {\r
359             v.push_back(CV_MAKETYPE(depth, cn));\r
360         }\r
361     }\r
362 \r
363     return v;\r
364 }\r
365 \r
366 const vector<MatType>& all_types()\r
367 {\r
368     static vector<MatType> v = types(CV_8U, CV_64F, 1, 4);\r
369 \r
370     return v;\r
371 }\r
372 \r
373 void cv::gpu::PrintTo(const DeviceInfo& info, ostream* os)\r
374 {\r
375     (*os) << info.name();\r
376 }\r
377 \r
378 void PrintTo(const UseRoi& useRoi, std::ostream* os)\r
379 {\r
380     if (useRoi)\r
381         (*os) << "sub matrix";\r
382     else\r
383         (*os) << "whole matrix";\r
384 }\r
385 \r
386 void PrintTo(const Inverse& inverse, std::ostream* os)\r
387 {\r
388     if (inverse)\r
389         (*os) << "inverse";\r
390     else\r
391         (*os) << "direct";\r
392 }\r
393 \r
394 void showDiff(InputArray gold_, InputArray actual_, double eps)\r
395 {\r
396     Mat gold = getMat(gold_);\r
397     Mat actual = getMat(actual_);\r
398 \r
399     Mat diff;\r
400     absdiff(gold, actual, diff);\r
401     threshold(diff, diff, eps, 255.0, cv::THRESH_BINARY);\r
402 \r
403     namedWindow("gold", WINDOW_NORMAL);\r
404     namedWindow("actual", WINDOW_NORMAL);\r
405     namedWindow("diff", WINDOW_NORMAL);\r
406 \r
407     imshow("gold", gold);\r
408     imshow("actual", actual);\r
409     imshow("diff", diff);\r
410 \r
411     waitKey();\r
412 }\r