1 /*M///////////////////////////////////////////////////////////////////////////////////////
\r
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
\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
10 // Intel License Agreement
\r
11 // For Open Source Computer Vision Library
\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
\r
14 // Third party copyrights are property of their respective owners.
\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
19 // * Redistribution's of source code must retain the above copyright notice,
\r
20 // this list of conditions and the following disclaimer.
\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
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
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
42 #include "test_precomp.hpp"
\r
44 using namespace std;
\r
46 using namespace cv::gpu;
\r
47 using namespace cvtest;
\r
48 using namespace testing;
\r
49 using namespace testing::internal;
\r
51 //////////////////////////////////////////////////////////////////////
\r
52 // random generators
\r
54 int randomInt(int minVal, int maxVal)
\r
56 RNG& rng = TS::ptr()->get_rng();
\r
57 return rng.uniform(minVal, maxVal);
\r
60 double randomDouble(double minVal, double maxVal)
\r
62 RNG& rng = TS::ptr()->get_rng();
\r
63 return rng.uniform(minVal, maxVal);
\r
66 Size randomSize(int minVal, int maxVal)
\r
68 return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
\r
71 Scalar randomScalar(double minVal, double maxVal)
\r
73 return Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal));
\r
76 Mat randomMat(Size size, int type, double minVal, double maxVal)
\r
78 return randomMat(TS::ptr()->get_rng(), size, type, minVal, maxVal, false);
\r
81 //////////////////////////////////////////////////////////////////////
\r
84 cv::gpu::GpuMat createMat(cv::Size size, int type, bool useRoi)
\r
90 size0.width += randomInt(5, 15);
\r
91 size0.height += randomInt(5, 15);
\r
94 GpuMat d_m(size0, type);
\r
97 d_m = d_m(Rect((size0.width - size.width) / 2, (size0.height - size.height) / 2, size.width, size.height));
\r
102 GpuMat loadMat(const Mat& m, bool useRoi)
\r
104 GpuMat d_m = createMat(m.size(), m.type(), useRoi);
\r
109 //////////////////////////////////////////////////////////////////////
\r
112 Mat readImage(const std::string& fileName, int flags)
\r
114 return imread(TS::ptr()->get_data_path() + fileName, flags);
\r
117 Mat readImageType(const std::string& fname, int type)
\r
119 Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? IMREAD_GRAYSCALE : IMREAD_COLOR);
\r
120 if (CV_MAT_CN(type) == 4)
\r
123 cvtColor(src, temp, cv::COLOR_BGR2BGRA);
\r
126 src.convertTo(src, CV_MAT_DEPTH(type), CV_MAT_DEPTH(type) == CV_32F ? 1.0 / 255.0 : 1.0);
\r
130 //////////////////////////////////////////////////////////////////////
\r
133 bool supportFeature(const DeviceInfo& info, FeatureSet feature)
\r
135 return TargetArchs::builtWith(feature) && info.supports(feature);
\r
138 DeviceManager& DeviceManager::instance()
\r
140 static DeviceManager obj;
\r
144 void DeviceManager::load(int i)
\r
147 devices_.reserve(1);
\r
151 if (i < 0 || i >= getCudaEnabledDeviceCount())
\r
153 msg << "Incorrect device number - " << i;
\r
154 throw runtime_error(msg.str());
\r
157 DeviceInfo info(i);
\r
159 if (!info.isCompatible())
\r
161 msg << "Device " << i << " [" << info.name() << "] is NOT compatible with current GPU module build";
\r
162 throw runtime_error(msg.str());
\r
165 devices_.push_back(info);
\r
168 void DeviceManager::loadAll()
\r
170 int deviceCount = getCudaEnabledDeviceCount();
\r
173 devices_.reserve(deviceCount);
\r
175 for (int i = 0; i < deviceCount; ++i)
\r
177 DeviceInfo info(i);
\r
178 if (info.isCompatible())
\r
180 devices_.push_back(info);
\r
185 //////////////////////////////////////////////////////////////////////
\r
186 // Additional assertion
\r
188 Mat getMat(InputArray arr)
\r
190 if (arr.kind() == _InputArray::GPU_MAT)
\r
193 arr.getGpuMat().download(m);
\r
197 return arr.getMat();
\r
200 double checkNorm(InputArray m1, InputArray m2)
\r
202 return norm(getMat(m1), getMat(m2), NORM_INF);
\r
205 void minMaxLocGold(const Mat& src, double* minVal_, double* maxVal_, Point* minLoc_, Point* maxLoc_, const Mat& mask)
\r
207 if (src.depth() != CV_8S)
\r
209 minMaxLoc(src, minVal_, maxVal_, minLoc_, maxLoc_, mask);
\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
217 double maxVal = -numeric_limits<double>::max();
\r
218 Point maxLoc(-1, -1);
\r
220 for (int y = 0; y < src.rows; ++y)
\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
225 for (int x = 0; x < src.cols; ++x)
\r
227 if (!mask_row || mask_row[x])
\r
229 schar val = src_row[x];
\r
234 minLoc = cv::Point(x, y);
\r
240 maxLoc = cv::Point(x, y);
\r
246 if (minVal_) *minVal_ = minVal;
\r
247 if (maxVal_) *maxVal_ = maxVal;
\r
249 if (minLoc_) *minLoc_ = minLoc;
\r
250 if (maxLoc_) *maxLoc_ = maxLoc;
\r
255 template <typename T, typename OutT> std::string printMatValImpl(const Mat& m, Point p)
\r
257 const int cn = m.channels();
\r
259 ostringstream ostr;
\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
267 ostr << ", " << static_cast<OutT>(m.at<T>(p.y, p.x * cn + c));
\r
274 std::string printMatVal(const Mat& m, Point p)
\r
276 typedef std::string (*func_t)(const Mat& m, Point p);
\r
278 static const func_t funcs[] =
\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
284 return funcs[m.depth()](m, p);
\r
288 testing::AssertionResult assertMatNear(const char* expr1, const char* expr2, const char* eps_expr, cv::InputArray m1_, cv::InputArray m2_, double eps)
\r
290 Mat m1 = getMat(m1_);
\r
291 Mat m2 = getMat(m2_);
\r
293 if (m1.size() != m2.size())
\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
300 if (m1.type() != m2.type())
\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
308 absdiff(m1.reshape(1), m2.reshape(1), diff);
\r
310 double maxVal = 0.0;
\r
312 minMaxLocGold(diff, 0, &maxVal, 0, &maxLoc);
\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
324 return AssertionSuccess();
\r
327 double checkSimilarity(InputArray m1, InputArray m2)
\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
334 //////////////////////////////////////////////////////////////////////
\r
335 // Helper structs for value-parameterized tests
\r
337 vector<MatDepth> depths(int depth_start, int depth_end)
\r
339 vector<MatDepth> v;
\r
341 v.reserve((depth_end - depth_start + 1));
\r
343 for (int depth = depth_start; depth <= depth_end; ++depth)
\r
344 v.push_back(depth);
\r
349 vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end)
\r
353 v.reserve((depth_end - depth_start + 1) * (cn_end - cn_start + 1));
\r
355 for (int depth = depth_start; depth <= depth_end; ++depth)
\r
357 for (int cn = cn_start; cn <= cn_end; ++cn)
\r
359 v.push_back(CV_MAKETYPE(depth, cn));
\r
366 const vector<MatType>& all_types()
\r
368 static vector<MatType> v = types(CV_8U, CV_64F, 1, 4);
\r
373 void cv::gpu::PrintTo(const DeviceInfo& info, ostream* os)
\r
375 (*os) << info.name();
\r
378 void PrintTo(const UseRoi& useRoi, std::ostream* os)
\r
381 (*os) << "sub matrix";
\r
383 (*os) << "whole matrix";
\r
386 void PrintTo(const Inverse& inverse, std::ostream* os)
\r
389 (*os) << "inverse";
\r
394 void showDiff(InputArray gold_, InputArray actual_, double eps)
\r
396 Mat gold = getMat(gold_);
\r
397 Mat actual = getMat(actual_);
\r
400 absdiff(gold, actual, diff);
\r
401 threshold(diff, diff, eps, 255.0, cv::THRESH_BINARY);
\r
403 namedWindow("gold", WINDOW_NORMAL);
\r
404 namedWindow("actual", WINDOW_NORMAL);
\r
405 namedWindow("diff", WINDOW_NORMAL);
\r
407 imshow("gold", gold);
\r
408 imshow("actual", actual);
\r
409 imshow("diff", diff);
\r