Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / ocl / test / test_matrix_operation.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-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // @Authors
19 //    Jia Haipeng, jiahaipeng95@gmail.com
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 //   * Redistribution's of source code must retain the above copyright notice,
25 //     this list of conditions and the following disclaimer.
26 //
27 //   * Redistribution's in binary form must reproduce the above copyright notice,
28 //     this list of conditions and the following disclaimer in the documentation
29 //     and/or other oclMaterials provided with the distribution.
30 //
31 //   * The name of the copyright holders may not be used to endorse or promote products
32 //     derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors "as is" and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46
47 #include "test_precomp.hpp"
48
49 #ifdef HAVE_OPENCL
50
51 using namespace cvtest;
52 using namespace testing;
53 using namespace std;
54
55 ////////////////////////////////converto/////////////////////////////////////////////////
56 PARAM_TEST_CASE(ConvertToTestBase, MatType, MatType)
57 {
58     int type;
59     int dst_type;
60
61     //src mat
62     cv::Mat mat;
63     cv::Mat dst;
64
65     // set up roi
66     int roicols;
67     int roirows;
68     int srcx;
69     int srcy;
70     int dstx;
71     int dsty;
72
73     //src mat with roi
74     cv::Mat mat_roi;
75     cv::Mat dst_roi;
76
77     //ocl dst mat for testing
78     cv::ocl::oclMat gdst_whole;
79
80     //ocl mat with roi
81     cv::ocl::oclMat gmat;
82     cv::ocl::oclMat gdst;
83
84     virtual void SetUp()
85     {
86         type     = GET_PARAM(0);
87         dst_type = GET_PARAM(1);
88
89         cv::RNG &rng = TS::ptr()->get_rng();
90         cv::Size size(MWIDTH, MHEIGHT);
91
92         mat = randomMat(rng, size, type, 5, 16, false);
93         dst  = randomMat(rng, size, type, 5, 16, false);
94     }
95
96     void random_roi()
97     {
98 #ifdef RANDOMROI
99         //randomize ROI
100         cv::RNG &rng = TS::ptr()->get_rng();
101         roicols = rng.uniform(1, mat.cols);
102         roirows = rng.uniform(1, mat.rows);
103         srcx   = rng.uniform(0, mat.cols - roicols);
104         srcy   = rng.uniform(0, mat.rows - roirows);
105         dstx    = rng.uniform(0, dst.cols  - roicols);
106         dsty    = rng.uniform(0, dst.rows  - roirows);
107 #else
108         roicols = mat.cols;
109         roirows = mat.rows;
110         srcx = 0;
111         srcy = 0;
112         dstx = 0;
113         dsty = 0;
114 #endif
115
116         mat_roi = mat(Rect(srcx, srcy, roicols, roirows));
117         dst_roi  = dst(Rect(dstx, dsty, roicols, roirows));
118
119         gdst_whole = dst;
120         gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
121
122         gmat = mat_roi;
123     }
124 };
125
126
127 struct ConvertTo : ConvertToTestBase {};
128
129 TEST_P(ConvertTo, Accuracy)
130 {
131     for(int j = 0; j < LOOP_TIMES; j++)
132     {
133         random_roi();
134
135         mat_roi.convertTo(dst_roi, dst_type);
136         gmat.convertTo(gdst, dst_type);
137
138         EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0);
139     }
140 }
141
142
143
144
145 ///////////////////////////////////////////copyto/////////////////////////////////////////////////////////////
146
147 PARAM_TEST_CASE(CopyToTestBase, MatType, bool)
148 {
149     int type;
150
151     cv::Mat mat;
152     cv::Mat mask;
153     cv::Mat dst;
154
155     // set up roi
156     int roicols;
157     int roirows;
158     int srcx;
159     int srcy;
160     int dstx;
161     int dsty;
162     int maskx;
163     int masky;
164
165     //src mat with roi
166     cv::Mat mat_roi;
167     cv::Mat mask_roi;
168     cv::Mat dst_roi;
169
170     //ocl dst mat for testing
171     cv::ocl::oclMat gdst_whole;
172
173     //ocl mat with roi
174     cv::ocl::oclMat gmat;
175     cv::ocl::oclMat gdst;
176     cv::ocl::oclMat gmask;
177
178     virtual void SetUp()
179     {
180         type = GET_PARAM(0);
181
182         cv::RNG &rng = TS::ptr()->get_rng();
183         cv::Size size(MWIDTH, MHEIGHT);
184
185         mat = randomMat(rng, size, type, 5, 16, false);
186         dst  = randomMat(rng, size, type, 5, 16, false);
187         mask = randomMat(rng, size, CV_8UC1, 0, 2,  false);
188
189         cv::threshold(mask, mask, 0.5, 255., CV_8UC1);
190
191     }
192
193     void random_roi()
194     {
195 #ifdef RANDOMROI
196         //randomize ROI
197         cv::RNG &rng = TS::ptr()->get_rng();
198         roicols = rng.uniform(1, mat.cols);
199         roirows = rng.uniform(1, mat.rows);
200         srcx   = rng.uniform(0, mat.cols - roicols);
201         srcy   = rng.uniform(0, mat.rows - roirows);
202         dstx    = rng.uniform(0, dst.cols  - roicols);
203         dsty    = rng.uniform(0, dst.rows  - roirows);
204         maskx   = rng.uniform(0, mask.cols - roicols);
205         masky   = rng.uniform(0, mask.rows - roirows);
206 #else
207         roicols = mat.cols;
208         roirows = mat.rows;
209         srcx = 0;
210         srcy = 0;
211         dstx = 0;
212         dsty = 0;
213         maskx = 0;
214         masky = 0;
215 #endif
216
217         mat_roi = mat(Rect(srcx, srcy, roicols, roirows));
218         mask_roi = mask(Rect(maskx, masky, roicols, roirows));
219         dst_roi  = dst(Rect(dstx, dsty, roicols, roirows));
220
221         gdst_whole = dst;
222         gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
223
224         gmat = mat_roi;
225         gmask = mask_roi;
226     }
227 };
228
229 struct CopyTo : CopyToTestBase {};
230
231 TEST_P(CopyTo, Without_mask)
232 {
233     for(int j = 0; j < LOOP_TIMES; j++)
234     {
235         random_roi();
236
237         mat_roi.copyTo(dst_roi);
238         gmat.copyTo(gdst);
239
240         EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0);
241     }
242 }
243
244 TEST_P(CopyTo, With_mask)
245 {
246     for(int j = 0; j < LOOP_TIMES; j++)
247     {
248         random_roi();
249
250         mat_roi.copyTo(dst_roi, mask_roi);
251         gmat.copyTo(gdst, gmask);
252
253         EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0);
254     }
255 }
256
257
258
259
260 ///////////////////////////////////////////copyto/////////////////////////////////////////////////////////////
261
262 PARAM_TEST_CASE(SetToTestBase, MatType, bool)
263 {
264     int type;
265     cv::Scalar val;
266
267     cv::Mat mat;
268     cv::Mat mask;
269
270     // set up roi
271     int roicols;
272     int roirows;
273     int srcx;
274     int srcy;
275     int maskx;
276     int masky;
277
278     //src mat with roi
279     cv::Mat mat_roi;
280     cv::Mat mask_roi;
281
282     //ocl dst mat for testing
283     cv::ocl::oclMat gmat_whole;
284
285     //ocl mat with roi
286     cv::ocl::oclMat gmat;
287     cv::ocl::oclMat gmask;
288
289     virtual void SetUp()
290     {
291         type = GET_PARAM(0);
292
293         cv::RNG &rng = TS::ptr()->get_rng();
294         cv::Size size(MWIDTH, MHEIGHT);
295
296         mat = randomMat(rng, size, type, 5, 16, false);
297         mask = randomMat(rng, size, CV_8UC1, 0, 2,  false);
298
299         cv::threshold(mask, mask, 0.5, 255., CV_8UC1);
300         val = cv::Scalar(rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0));
301
302     }
303
304     void random_roi()
305     {
306 #ifdef RANDOMROI
307         //randomize ROI
308         cv::RNG &rng = TS::ptr()->get_rng();
309         roicols = rng.uniform(1, mat.cols);
310         roirows = rng.uniform(1, mat.rows);
311         srcx   = rng.uniform(0, mat.cols - roicols);
312         srcy   = rng.uniform(0, mat.rows - roirows);
313         maskx   = rng.uniform(0, mask.cols - roicols);
314         masky   = rng.uniform(0, mask.rows - roirows);
315 #else
316         roicols = mat.cols;
317         roirows = mat.rows;
318         srcx = 0;
319         srcy = 0;
320         maskx = 0;
321         masky = 0;
322 #endif
323
324         mat_roi = mat(Rect(srcx, srcy, roicols, roirows));
325         mask_roi = mask(Rect(maskx, masky, roicols, roirows));
326
327         gmat_whole = mat;
328         gmat = gmat_whole(Rect(srcx, srcy, roicols, roirows));
329
330         gmask = mask_roi;
331     }
332 };
333
334 struct SetTo : SetToTestBase {};
335
336 TEST_P(SetTo, Without_mask)
337 {
338     for(int j = 0; j < LOOP_TIMES; j++)
339     {
340         random_roi();
341
342         mat_roi.setTo(val);
343         gmat.setTo(val);
344
345         EXPECT_MAT_NEAR(mat, Mat(gmat_whole), 1.);
346     }
347 }
348
349 TEST_P(SetTo, With_mask)
350 {
351     for(int j = 0; j < LOOP_TIMES; j++)
352     {
353         random_roi();
354
355         mat_roi.setTo(val, mask_roi);
356         gmat.setTo(val, gmask);
357
358         EXPECT_MAT_NEAR(mat, Mat(gmat_whole), 1.);
359     }
360 }
361
362 //convertC3C4
363 PARAM_TEST_CASE(convertC3C4, MatType, cv::Size)
364 {
365     int type;
366     cv::Size ksize;
367
368     //src mat
369     cv::Mat mat1;
370     cv::Mat dst;
371
372     // set up roi
373     int roicols;
374     int roirows;
375     int src1x;
376     int src1y;
377     int dstx;
378     int dsty;
379
380     //src mat with roi
381     cv::Mat mat1_roi;
382     cv::Mat dst_roi;
383
384     //ocl dst mat for testing
385     cv::ocl::oclMat gdst_whole;
386
387     //ocl mat with roi
388     cv::ocl::oclMat gmat1;
389     cv::ocl::oclMat gdst;
390
391     virtual void SetUp()
392     {
393         type = GET_PARAM(0);
394         ksize = GET_PARAM(1);
395
396     }
397
398     void random_roi()
399     {
400 #ifdef RANDOMROI
401         //randomize ROI
402         cv::RNG &rng = TS::ptr()->get_rng();
403         roicols = rng.uniform(2, mat1.cols);
404         roirows = rng.uniform(2, mat1.rows);
405         src1x   = rng.uniform(0, mat1.cols - roicols);
406         src1y   = rng.uniform(0, mat1.rows - roirows);
407         dstx    = rng.uniform(0, dst.cols  - roicols);
408         dsty    = rng.uniform(0, dst.rows  - roirows);
409 #else
410         roicols = mat1.cols;
411         roirows = mat1.rows;
412         src1x = 0;
413         src1y = 0;
414         dstx = 0;
415         dsty = 0;
416 #endif
417
418         mat1_roi = mat1(Rect(src1x, src1y, roicols, roirows));
419         dst_roi  = dst(Rect(dstx, dsty, roicols, roirows));
420
421         gdst_whole = dst;
422         gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
423
424
425         gmat1 = mat1_roi;
426     }
427
428 };
429
430 TEST_P(convertC3C4, Accuracy)
431 {
432     cv::RNG &rng = TS::ptr()->get_rng();
433     for(int j = 0; j < LOOP_TIMES; j++)
434     {
435         //random_roi();
436         int width = rng.uniform(2, MWIDTH);
437         int height = rng.uniform(2, MHEIGHT);
438         cv::Size size(width, height);
439
440         mat1 = randomMat(rng, size, type, 0, 40, false);
441         gmat1 = mat1;
442
443         EXPECT_MAT_NEAR(mat1, Mat(gmat1), 0.0);
444     }
445
446 }
447
448 INSTANTIATE_TEST_CASE_P(MatrixOperation, ConvertTo, Combine(
449                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32SC1, CV_32SC4, CV_32FC1, CV_32FC4),
450                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32SC1, CV_32SC4, CV_32FC1, CV_32FC4)));
451
452 INSTANTIATE_TEST_CASE_P(MatrixOperation, CopyTo, Combine(
453                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32SC1, CV_32SC3, CV_32SC4, CV_32FC1, CV_32FC3, CV_32FC4),
454                             Values(false))); // Values(false) is the reserved parameter
455
456 INSTANTIATE_TEST_CASE_P(MatrixOperation, SetTo, Combine(
457                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32SC1, CV_32SC3, CV_32SC4, CV_32FC1, CV_32FC3, CV_32FC4),
458                             Values(false))); // Values(false) is the reserved parameter
459
460 INSTANTIATE_TEST_CASE_P(MatrixOperation, convertC3C4, Combine(
461                             Values(CV_8UC3,  CV_32SC3,  CV_32FC3),
462                             Values(cv::Size())));
463 #endif