CLAHE Python bindings
[profile/ivi/opencv.git] / modules / ocl / test / utility.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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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 Intel Corporation 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 #define VARNAME(A) #A
44 using namespace std;
45 using namespace cv;
46 using namespace cv::gpu;
47 using namespace cvtest;
48
49
50 //std::string generateVarList(int first,...)
51 //{
52 //      vector<std::string> varname;
53 //
54 //      va_list argp;
55 //      string s;
56 //      stringstream ss;
57 //      va_start(argp,first);
58 //      int i=first;
59 //      while(i!=-1)
60 //      {
61 //              ss<<i<<",";
62 //              i=va_arg(argp,int);
63 //      };
64 //      s=ss.str();
65 //      va_end(argp);
66 //      return s;
67 //};
68
69 //std::string generateVarList(int& p1,int& p2)
70 //{
71 //      stringstream ss;
72 //      ss<<VARNAME(p1)<<":"<<src1x<<","<<VARNAME(p2)<<":"<<src1y;
73 //      return ss.str();
74 //};
75
76 int randomInt(int minVal, int maxVal)
77 {
78     RNG &rng = TS::ptr()->get_rng();
79     return rng.uniform(minVal, maxVal);
80 }
81
82 double randomDouble(double minVal, double maxVal)
83 {
84     RNG &rng = TS::ptr()->get_rng();
85     return rng.uniform(minVal, maxVal);
86 }
87
88 Size randomSize(int minVal, int maxVal)
89 {
90     return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
91 }
92
93 Scalar randomScalar(double minVal, double maxVal)
94 {
95     return Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal));
96 }
97
98 Mat randomMat(Size size, int type, double minVal, double maxVal)
99 {
100     return randomMat(TS::ptr()->get_rng(), size, type, minVal, maxVal, false);
101 }
102
103
104
105
106
107
108
109 /*
110 void showDiff(InputArray gold_, InputArray actual_, double eps)
111 {
112     Mat gold;
113     if (gold_.kind() == _InputArray::MAT)
114         gold = gold_.getMat();
115     else
116         gold_.getGpuMat().download(gold);
117
118     Mat actual;
119     if (actual_.kind() == _InputArray::MAT)
120         actual = actual_.getMat();
121     else
122         actual_.getGpuMat().download(actual);
123
124     Mat diff;
125     absdiff(gold, actual, diff);
126     threshold(diff, diff, eps, 255.0, cv::THRESH_BINARY);
127
128     namedWindow("gold", WINDOW_NORMAL);
129     namedWindow("actual", WINDOW_NORMAL);
130     namedWindow("diff", WINDOW_NORMAL);
131
132     imshow("gold", gold);
133     imshow("actual", actual);
134     imshow("diff", diff);
135
136     waitKey();
137 }
138 */
139
140 /*
141 bool supportFeature(const DeviceInfo& info, FeatureSet feature)
142 {
143     return TargetArchs::builtWith(feature) && info.supports(feature);
144 }
145
146 const vector<DeviceInfo>& devices()
147 {
148     static vector<DeviceInfo> devs;
149     static bool first = true;
150
151     if (first)
152     {
153         int deviceCount = getCudaEnabledDeviceCount();
154
155         devs.reserve(deviceCount);
156
157         for (int i = 0; i < deviceCount; ++i)
158         {
159             DeviceInfo info(i);
160             if (info.isCompatible())
161                 devs.push_back(info);
162         }
163
164         first = false;
165     }
166
167     return devs;
168 }
169
170 vector<DeviceInfo> devices(FeatureSet feature)
171 {
172     const vector<DeviceInfo>& d = devices();
173
174     vector<DeviceInfo> devs_filtered;
175
176     if (TargetArchs::builtWith(feature))
177     {
178         devs_filtered.reserve(d.size());
179
180         for (size_t i = 0, size = d.size(); i < size; ++i)
181         {
182             const DeviceInfo& info = d[i];
183
184             if (info.supports(feature))
185                 devs_filtered.push_back(info);
186         }
187     }
188
189     return devs_filtered;
190 }
191 */
192
193 vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end)
194 {
195     vector<MatType> v;
196
197     v.reserve((depth_end - depth_start + 1) * (cn_end - cn_start + 1));
198
199     for (int depth = depth_start; depth <= depth_end; ++depth)
200     {
201         for (int cn = cn_start; cn <= cn_end; ++cn)
202         {
203             v.push_back(CV_MAKETYPE(depth, cn));
204         }
205     }
206
207     return v;
208 }
209
210 const vector<MatType> &all_types()
211 {
212     static vector<MatType> v = types(CV_8U, CV_64F, 1, 4);
213
214     return v;
215 }
216
217 Mat readImage(const string &fileName, int flags)
218 {
219     return imread(string(cvtest::TS::ptr()->get_data_path()) + fileName, flags);
220 }
221
222 Mat readImageType(const string &fname, int type)
223 {
224     Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? IMREAD_GRAYSCALE : IMREAD_COLOR);
225     if (CV_MAT_CN(type) == 4)
226     {
227         Mat temp;
228         cvtColor(src, temp, cv::COLOR_BGR2BGRA);
229         swap(src, temp);
230     }
231     src.convertTo(src, CV_MAT_DEPTH(type));
232     return src;
233 }
234
235 double checkNorm(const Mat &m)
236 {
237     return norm(m, NORM_INF);
238 }
239
240 double checkNorm(const Mat &m1, const Mat &m2)
241 {
242     return norm(m1, m2, NORM_INF);
243 }
244
245 double checkSimilarity(const Mat &m1, const Mat &m2)
246 {
247     Mat diff;
248     matchTemplate(m1, m2, diff, CV_TM_CCORR_NORMED);
249     return std::abs(diff.at<float>(0, 0) - 1.f);
250 }
251
252 /*
253 void cv::ocl::PrintTo(const DeviceInfo& info, ostream* os)
254 {
255     (*os) << info.name();
256 }
257 */
258
259 void PrintTo(const Inverse &inverse, std::ostream *os)
260 {
261     if (inverse)
262         (*os) << "inverse";
263     else
264         (*os) << "direct";
265 }
266