Merge pull request #1263 from abidrahmank:pyCLAHE_24
[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 "test_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 void showDiff(InputArray gold_, InputArray actual_, double eps)
105 {
106     Mat gold;
107     if (gold_.kind() == _InputArray::MAT)
108         gold = gold_.getMat();
109     else
110         gold_.getGpuMat().download(gold);
111
112     Mat actual;
113     if (actual_.kind() == _InputArray::MAT)
114         actual = actual_.getMat();
115     else
116         actual_.getGpuMat().download(actual);
117
118     Mat diff;
119     absdiff(gold, actual, diff);
120     threshold(diff, diff, eps, 255.0, cv::THRESH_BINARY);
121
122     namedWindow("gold", WINDOW_NORMAL);
123     namedWindow("actual", WINDOW_NORMAL);
124     namedWindow("diff", WINDOW_NORMAL);
125
126     imshow("gold", gold);
127     imshow("actual", actual);
128     imshow("diff", diff);
129
130     waitKey();
131 }
132 */
133
134
135
136 vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end)
137 {
138     vector<MatType> v;
139
140     v.reserve((depth_end - depth_start + 1) * (cn_end - cn_start + 1));
141
142     for (int depth = depth_start; depth <= depth_end; ++depth)
143     {
144         for (int cn = cn_start; cn <= cn_end; ++cn)
145         {
146             v.push_back(CV_MAKETYPE(depth, cn));
147         }
148     }
149
150     return v;
151 }
152
153 const vector<MatType> &all_types()
154 {
155     static vector<MatType> v = types(CV_8U, CV_64F, 1, 4);
156
157     return v;
158 }
159
160 Mat readImage(const string &fileName, int flags)
161 {
162     return imread(string(cvtest::TS::ptr()->get_data_path()) + fileName, flags);
163 }
164
165 Mat readImageType(const string &fname, int type)
166 {
167     Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? IMREAD_GRAYSCALE : IMREAD_COLOR);
168     if (CV_MAT_CN(type) == 4)
169     {
170         Mat temp;
171         cvtColor(src, temp, cv::COLOR_BGR2BGRA);
172         swap(src, temp);
173     }
174     src.convertTo(src, CV_MAT_DEPTH(type));
175     return src;
176 }
177
178 double checkNorm(const Mat &m)
179 {
180     return norm(m, NORM_INF);
181 }
182
183 double checkNorm(const Mat &m1, const Mat &m2)
184 {
185     return norm(m1, m2, NORM_INF);
186 }
187
188 double checkSimilarity(const Mat &m1, const Mat &m2)
189 {
190     Mat diff;
191     matchTemplate(m1, m2, diff, CV_TM_CCORR_NORMED);
192     return std::abs(diff.at<float>(0, 0) - 1.f);
193 }
194
195 /*
196 void cv::ocl::PrintTo(const DeviceInfo& info, ostream* os)
197 {
198     (*os) << info.name();
199 }
200 */
201
202 void PrintTo(const Inverse &inverse, std::ostream *os)
203 {
204     if (inverse)
205         (*os) << "inverse";
206     else
207         (*os) << "direct";
208 }
209
210 double checkRectSimilarity(Size sz, std::vector<Rect>& ob1, std::vector<Rect>& ob2)
211 {
212     double final_test_result = 0.0;
213     size_t sz1 = ob1.size();
214     size_t sz2 = ob2.size();
215
216     if(sz1 != sz2)
217     {
218         return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
219     }
220     else
221     {
222         if(sz1==0 && sz2==0)
223             return 0;
224         cv::Mat cpu_result(sz, CV_8UC1);
225         cpu_result.setTo(0);
226
227         for(vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); r++)
228         {      
229             cv::Mat cpu_result_roi(cpu_result, *r);
230             cpu_result_roi.setTo(1);
231             cpu_result.copyTo(cpu_result);
232         }
233         int cpu_area = cv::countNonZero(cpu_result > 0);
234
235         cv::Mat gpu_result(sz, CV_8UC1);
236         gpu_result.setTo(0);
237         for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); r2++)
238         {
239             cv::Mat gpu_result_roi(gpu_result, *r2);
240             gpu_result_roi.setTo(1);
241             gpu_result.copyTo(gpu_result);
242         }
243
244         cv::Mat result_;
245         multiply(cpu_result, gpu_result, result_);
246         int result = cv::countNonZero(result_ > 0);
247         if(cpu_area!=0 && result!=0)
248             final_test_result = 1.0 - (double)result/(double)cpu_area;
249         else if(cpu_area==0 && result!=0)
250             final_test_result = -1;
251     }
252     return final_test_result;
253 }
254