8eaa7e637dbb60fbe323b0525d28cec11759f65b
[platform/upstream/opencv.git] / modules / ts / src / ocl_test.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) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of the copyright holders may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the OpenCV Foundation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 #include "opencv2/ts/ocl_test.hpp"
45
46 #ifdef HAVE_OPENCL
47
48 #define DUMP_CONFIG_PROPERTY(propertyName, propertyValue) \
49     do { \
50         std::stringstream ssName, ssValue;\
51         ssName << propertyName;\
52         ssValue << (propertyValue); \
53         ::testing::Test::RecordProperty(ssName.str(), ssValue.str()); \
54     } while (false)
55
56 #define DUMP_MESSAGE_STDOUT(msg) \
57     do { \
58         std::cout << msg << std::endl; \
59     } while (false)
60
61 #include <opencv2/core/opencl/opencl_info.hpp>
62
63 #endif // HAVE_OPENCL
64
65 namespace cvtest {
66 namespace ocl {
67
68 using namespace cv;
69
70 int test_loop_times = 1; // TODO Read from command line / environment
71
72 #ifdef HAVE_OPENCL
73 void dumpOpenCLDevice()
74 {
75     cv::dumpOpenCLInformation();
76 }
77 #endif // HAVE_OPENCL
78
79 Mat TestUtils::readImage(const String &fileName, int flags)
80 {
81     return cv::imread(cvtest::TS::ptr()->get_data_path() + fileName, flags);
82 }
83
84 Mat TestUtils::readImageType(const String &fname, int type)
85 {
86     Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
87     if (CV_MAT_CN(type) == 4)
88     {
89         Mat temp;
90         cv::cvtColor(src, temp, cv::COLOR_BGR2BGRA);
91         swap(src, temp);
92     }
93     src.convertTo(src, CV_MAT_DEPTH(type));
94     return src;
95 }
96
97 double TestUtils::checkNorm1(InputArray m, InputArray mask)
98 {
99     return cvtest::norm(m.getMat(), NORM_INF, mask.getMat());
100 }
101
102 double TestUtils::checkNorm2(InputArray m1, InputArray m2, InputArray mask)
103 {
104     return cvtest::norm(m1.getMat(), m2.getMat(), NORM_INF, mask.getMat());
105 }
106
107 double TestUtils::checkSimilarity(InputArray m1, InputArray m2)
108 {
109     Mat diff;
110     matchTemplate(m1.getMat(), m2.getMat(), diff, CV_TM_CCORR_NORMED);
111     return std::abs(diff.at<float>(0, 0) - 1.f);
112 }
113
114 double TestUtils::checkRectSimilarity(const Size & sz, std::vector<Rect>& ob1, std::vector<Rect>& ob2)
115 {
116     double final_test_result = 0.0;
117     size_t sz1 = ob1.size();
118     size_t sz2 = ob2.size();
119
120     if (sz1 != sz2)
121         return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
122     else
123     {
124         if (sz1 == 0 && sz2 == 0)
125             return 0;
126         cv::Mat cpu_result(sz, CV_8UC1);
127         cpu_result.setTo(0);
128
129         for (vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); ++r)
130         {
131             cv::Mat cpu_result_roi(cpu_result, *r);
132             cpu_result_roi.setTo(1);
133             cpu_result.copyTo(cpu_result);
134         }
135         int cpu_area = cv::countNonZero(cpu_result > 0);
136
137         cv::Mat gpu_result(sz, CV_8UC1);
138         gpu_result.setTo(0);
139         for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); ++r2)
140         {
141             cv::Mat gpu_result_roi(gpu_result, *r2);
142             gpu_result_roi.setTo(1);
143             gpu_result.copyTo(gpu_result);
144         }
145
146         cv::Mat result_;
147         multiply(cpu_result, gpu_result, result_);
148         int result = cv::countNonZero(result_ > 0);
149         if (cpu_area!=0 && result!=0)
150             final_test_result = 1.0 - (double)result/(double)cpu_area;
151         else if(cpu_area==0 && result!=0)
152             final_test_result = -1;
153     }
154     return final_test_result;
155 }
156
157 void TestUtils::showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow)
158 {
159     Mat src = _src.getMat(), actual = _actual.getMat(), gold = _gold.getMat();
160
161     Mat diff, diff_thresh;
162     absdiff(gold, actual, diff);
163     diff.convertTo(diff, CV_32F);
164     threshold(diff, diff_thresh, eps, 255.0, cv::THRESH_BINARY);
165
166     if (alwaysShow || cv::countNonZero(diff_thresh.reshape(1)) > 0)
167     {
168 #if 0
169         std::cout << "Source: " << std::endl << src << std::endl;
170         std::cout << "Expected: " << std::endl << gold << std::endl;
171         std::cout << "Actual: " << std::endl << actual << std::endl;
172 #endif
173
174         namedWindow("src", WINDOW_NORMAL);
175         namedWindow("gold", WINDOW_NORMAL);
176         namedWindow("actual", WINDOW_NORMAL);
177         namedWindow("diff", WINDOW_NORMAL);
178
179         imshow("src", src);
180         imshow("gold", gold);
181         imshow("actual", actual);
182         imshow("diff", diff);
183
184         cv::waitKey();
185     }
186 }
187
188 } } // namespace cvtest::ocl