8534658278db9480bbbdec8ada07a894e4c21474
[platform/upstream/opencv.git] / modules / imgproc / test / ocl / test_warp.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 //    Niko Li, newlife20080214@gmail.com
20 //    Jia Haipeng, jiahaipeng95@gmail.com
21 //    Shengen Yan, yanshengen@gmail.com
22 //    Jiang Liyuan, lyuan001.good@163.com
23 //    Rock Li, Rock.Li@amd.com
24 //    Wu Zailong, bullet@yeah.net
25 //    Xu Pang, pangxu010@163.com
26 //    Sen Liu, swjtuls1987@126.com
27 //
28 // Redistribution and use in source and binary forms, with or without modification,
29 // are permitted provided that the following conditions are met:
30 //
31 //   * Redistribution's of source code must retain the above copyright notice,
32 //     this list of conditions and the following disclaimer.
33 //
34 //   * Redistribution's in binary form must reproduce the above copyright notice,
35 //     this list of conditions and the following disclaimer in the documentation
36 //     and/or other materials provided with the distribution.
37 //
38 //   * The name of the copyright holders may not be used to endorse or promote products
39 //     derived from this software without specific prior written permission.
40 //
41 // This software is provided by the copyright holders and contributors "as is" and
42 // any express or implied warranties, including, but not limited to, the implied
43 // warranties of merchantability and fitness for a particular purpose are disclaimed.
44 // In no event shall the Intel Corporation or contributors be liable for any direct,
45 // indirect, incidental, special, exemplary, or consequential damages
46 // (including, but not limited to, procurement of substitute goods or services;
47 // loss of use, data, or profits; or business interruption) however caused
48 // and on any theory of liability, whether in contract, strict liability,
49 // or tort (including negligence or otherwise) arising in any way out of
50 // the use of this software, even if advised of the possibility of such damage.
51 //
52 //M*/
53
54 #include "../test_precomp.hpp"
55 #include "opencv2/ts/ocl_test.hpp"
56
57 #ifdef HAVE_OPENCL
58
59 namespace cvtest {
60 namespace ocl {
61
62 enum
63 {
64     noType = -1
65 };
66
67 /////////////////////////////////////////////////////////////////////////////////////////////////
68 // warpAffine  & warpPerspective
69
70 PARAM_TEST_CASE(WarpTestBase, MatType, Interpolation, bool, bool)
71 {
72     int type, interpolation;
73     Size dsize;
74     bool useRoi, mapInverse;
75     int depth;
76
77     TEST_DECLARE_INPUT_PARAMETER(src);
78     TEST_DECLARE_OUTPUT_PARAMETER(dst);
79
80     virtual void SetUp()
81     {
82         type = GET_PARAM(0);
83         interpolation = GET_PARAM(1);
84         mapInverse = GET_PARAM(2);
85         useRoi = GET_PARAM(3);
86         depth = CV_MAT_DEPTH(type);
87
88         if (mapInverse)
89             interpolation |= WARP_INVERSE_MAP;
90     }
91
92     void random_roi()
93     {
94         dsize = randomSize(1, MAX_VALUE);
95
96         Size roiSize = randomSize(1, MAX_VALUE);
97         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
98         randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
99
100         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
101         randomSubMat(dst, dst_roi, dsize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
102
103         UMAT_UPLOAD_INPUT_PARAMETER(src);
104         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
105     }
106
107     void Near(double threshold = 0.0)
108     {
109         if (depth < CV_32F)
110             EXPECT_MAT_N_DIFF(dst_roi, udst_roi, cvRound(dst_roi.total()*threshold));
111         else
112             OCL_EXPECT_MATS_NEAR_RELATIVE(dst, threshold);
113     }
114 };
115
116 PARAM_TEST_CASE(WarpTest_cols4_Base, MatType, Interpolation, bool, bool)
117 {
118     int type, interpolation;
119     Size dsize;
120     bool useRoi, mapInverse;
121     int depth;
122
123     TEST_DECLARE_INPUT_PARAMETER(src);
124     TEST_DECLARE_OUTPUT_PARAMETER(dst);
125
126     virtual void SetUp()
127     {
128         type = GET_PARAM(0);
129         interpolation = GET_PARAM(1);
130         mapInverse = GET_PARAM(2);
131         useRoi = GET_PARAM(3);
132         depth = CV_MAT_DEPTH(type);
133
134         if (mapInverse)
135             interpolation |= WARP_INVERSE_MAP;
136     }
137
138     void random_roi()
139     {
140         dsize = randomSize(1, MAX_VALUE);
141         dsize.width = ((dsize.width >> 2) + 1) * 4;
142
143         Size roiSize = randomSize(1, MAX_VALUE);
144         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
145         randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
146
147         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
148         randomSubMat(dst, dst_roi, dsize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
149
150         UMAT_UPLOAD_INPUT_PARAMETER(src);
151         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
152     }
153
154     void Near(double threshold = 0.0)
155     {
156         if (depth < CV_32F)
157             EXPECT_MAT_N_DIFF(dst_roi, udst_roi, cvRound(dst_roi.total()*threshold));
158         else
159             OCL_EXPECT_MATS_NEAR_RELATIVE(dst, threshold);
160     }
161 };
162
163 /////warpAffine
164
165 typedef WarpTestBase WarpAffine;
166
167 /////warpAffine
168
169 typedef WarpTestBase WarpAffine;
170
171 OCL_TEST_P(WarpAffine, Mat)
172 {
173     for (int j = 0; j < test_loop_times; j++)
174     {
175         double eps = depth < CV_32F ? 0.04 : 0.06;
176         random_roi();
177
178         Mat M = getRotationMatrix2D(Point2f(src_roi.cols / 2.0f, src_roi.rows / 2.0f),
179             rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
180
181         OCL_OFF(cv::warpAffine(src_roi, dst_roi, M, dsize, interpolation));
182         OCL_ON(cv::warpAffine(usrc_roi, udst_roi, M, dsize, interpolation));
183
184         Near(eps);
185     }
186 }
187
188 typedef WarpTest_cols4_Base WarpAffine_cols4;
189
190 OCL_TEST_P(WarpAffine_cols4, Mat)
191 {
192     for (int j = 0; j < test_loop_times; j++)
193     {
194         double eps = depth < CV_32F ? 0.04 : 0.06;
195         random_roi();
196
197         Mat M = getRotationMatrix2D(Point2f(src_roi.cols / 2.0f, src_roi.rows / 2.0f),
198             rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
199
200         OCL_OFF(cv::warpAffine(src_roi, dst_roi, M, dsize, interpolation));
201         OCL_ON(cv::warpAffine(usrc_roi, udst_roi, M, dsize, interpolation));
202
203         Near(eps);
204     }
205 }
206
207 //// warpPerspective
208
209 typedef WarpTestBase WarpPerspective;
210
211 OCL_TEST_P(WarpPerspective, Mat)
212 {
213     for (int j = 0; j < test_loop_times; j++)
214     {
215         double eps = depth < CV_32F ? 0.03 : 0.06;
216         random_roi();
217
218         float cols = static_cast<float>(src_roi.cols), rows = static_cast<float>(src_roi.rows);
219         float cols2 = cols / 2.0f, rows2 = rows / 2.0f;
220         Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };
221         Point2f dp[] = { Point2f(rng.uniform(0.0f, cols2), rng.uniform(0.0f, rows2)),
222             Point2f(rng.uniform(cols2, cols), rng.uniform(0.0f, rows2)),
223             Point2f(rng.uniform(0.0f, cols2), rng.uniform(rows2, rows)),
224             Point2f(rng.uniform(cols2, cols), rng.uniform(rows2, rows)) };
225         Mat M = getPerspectiveTransform(sp, dp);
226
227         OCL_OFF(cv::warpPerspective(src_roi, dst_roi, M, dsize, interpolation));
228         OCL_ON(cv::warpPerspective(usrc_roi, udst_roi, M, dsize, interpolation));
229
230         Near(eps);
231     }
232 }
233
234 typedef WarpTest_cols4_Base WarpPerspective_cols4;
235
236 OCL_TEST_P(WarpPerspective_cols4, Mat)
237 {
238     for (int j = 0; j < test_loop_times; j++)
239     {
240         double eps = depth < CV_32F ? 0.03 : 0.06;
241         random_roi();
242
243         float cols = static_cast<float>(src_roi.cols), rows = static_cast<float>(src_roi.rows);
244         float cols2 = cols / 2.0f, rows2 = rows / 2.0f;
245         Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };
246         Point2f dp[] = { Point2f(rng.uniform(0.0f, cols2), rng.uniform(0.0f, rows2)),
247             Point2f(rng.uniform(cols2, cols), rng.uniform(0.0f, rows2)),
248             Point2f(rng.uniform(0.0f, cols2), rng.uniform(rows2, rows)),
249             Point2f(rng.uniform(cols2, cols), rng.uniform(rows2, rows)) };
250         Mat M = getPerspectiveTransform(sp, dp);
251
252         OCL_OFF(cv::warpPerspective(src_roi, dst_roi, M, dsize, interpolation));
253         OCL_ON(cv::warpPerspective(usrc_roi, udst_roi, M, dsize, interpolation));
254
255         Near(eps);
256     }
257 }
258
259 /////////////////////////////////////////////////////////////////////////////////////////////////
260 //// resize
261
262 PARAM_TEST_CASE(Resize, MatType, double, double, Interpolation, bool, int)
263 {
264     int type, interpolation;
265     int widthMultiple;
266     double fx, fy;
267     bool useRoi;
268
269     TEST_DECLARE_INPUT_PARAMETER(src);
270     TEST_DECLARE_OUTPUT_PARAMETER(dst);
271
272     virtual void SetUp()
273     {
274         type = GET_PARAM(0);
275         fx = GET_PARAM(1);
276         fy = GET_PARAM(2);
277         interpolation = GET_PARAM(3);
278         useRoi = GET_PARAM(4);
279         widthMultiple = GET_PARAM(5);
280     }
281
282     void random_roi()
283     {
284         CV_Assert(fx > 0 && fy > 0);
285
286         Size srcRoiSize = randomSize(10, MAX_VALUE), dstRoiSize;
287         // Make sure the width is a multiple of the requested value, and no more
288         srcRoiSize.width += widthMultiple - 1 - (srcRoiSize.width - 1) % widthMultiple;
289         dstRoiSize.width = cvRound(srcRoiSize.width * fx);
290         dstRoiSize.height = cvRound(srcRoiSize.height * fy);
291
292         if (dstRoiSize.area() == 0)
293         {
294             random_roi();
295             return;
296         }
297
298         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
299         randomSubMat(src, src_roi, srcRoiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
300
301         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
302         randomSubMat(dst, dst_roi, dstRoiSize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
303
304         UMAT_UPLOAD_INPUT_PARAMETER(src);
305         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
306     }
307
308     void Near(double threshold = 0.0)
309     {
310         OCL_EXPECT_MATS_NEAR(dst, threshold);
311     }
312 };
313
314 OCL_TEST_P(Resize, Mat)
315 {
316     for (int j = 0; j < test_loop_times; j++)
317     {
318         int depth = CV_MAT_DEPTH(type);
319         double eps = depth <= CV_32S ? 1 : 5e-2;
320
321         random_roi();
322
323         OCL_OFF(cv::resize(src_roi, dst_roi, Size(), fx, fy, interpolation));
324         OCL_ON(cv::resize(usrc_roi, udst_roi, Size(), fx, fy, interpolation));
325
326         Near(eps);
327     }
328 }
329
330 /////////////////////////////////////////////////////////////////////////////////////////////////
331 // remap
332
333 PARAM_TEST_CASE(Remap, MatDepth, Channels, std::pair<MatType, MatType>, BorderType, bool)
334 {
335     int srcType, map1Type, map2Type;
336     int borderType;
337     bool useRoi;
338
339     Scalar val;
340
341     TEST_DECLARE_INPUT_PARAMETER(src);
342     TEST_DECLARE_INPUT_PARAMETER(map1);
343     TEST_DECLARE_INPUT_PARAMETER(map2);
344     TEST_DECLARE_OUTPUT_PARAMETER(dst);
345
346     virtual void SetUp()
347     {
348         srcType = CV_MAKE_TYPE(GET_PARAM(0), GET_PARAM(1));
349         map1Type = GET_PARAM(2).first;
350         map2Type = GET_PARAM(2).second;
351         borderType = GET_PARAM(3);
352         useRoi = GET_PARAM(4);
353     }
354
355     void random_roi()
356     {
357         val = randomScalar(-MAX_VALUE, MAX_VALUE);
358         Size srcROISize = randomSize(1, MAX_VALUE);
359         Size dstROISize = randomSize(1, MAX_VALUE);
360
361         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
362         randomSubMat(src, src_roi, srcROISize, srcBorder, srcType, 5, 256);
363
364         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
365         randomSubMat(dst, dst_roi, dstROISize, dstBorder, srcType, -MAX_VALUE, MAX_VALUE);
366
367         int mapMaxValue = MAX_VALUE << 2;
368         Border map1Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
369         randomSubMat(map1, map1_roi, dstROISize, map1Border, map1Type, -mapMaxValue, mapMaxValue);
370
371         Border map2Border = randomBorder(0, useRoi ? MAX_VALUE + 1 : 0);
372         if (map2Type != noType)
373         {
374             int mapMinValue = -mapMaxValue;
375             if (map2Type == CV_16UC1 || map2Type == CV_16SC1)
376                 mapMinValue = 0, mapMaxValue = INTER_TAB_SIZE2;
377             randomSubMat(map2, map2_roi, dstROISize, map2Border, map2Type, mapMinValue, mapMaxValue);
378         }
379
380         UMAT_UPLOAD_INPUT_PARAMETER(src);
381         UMAT_UPLOAD_INPUT_PARAMETER(map1);
382         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
383         if (noType != map2Type)
384             UMAT_UPLOAD_INPUT_PARAMETER(map2);
385     }
386
387     void Near(double threshold = 0.0)
388     {
389         OCL_EXPECT_MATS_NEAR(dst, threshold);
390     }
391 };
392
393 typedef Remap Remap_INTER_NEAREST;
394
395 OCL_TEST_P(Remap_INTER_NEAREST, Mat)
396 {
397     for (int j = 0; j < test_loop_times; j++)
398     {
399         random_roi();
400
401         OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_NEAREST, borderType, val));
402         OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_NEAREST, borderType, val));
403
404         Near(1.0);
405     }
406 }
407
408 typedef Remap Remap_INTER_LINEAR;
409
410 OCL_TEST_P(Remap_INTER_LINEAR, Mat)
411 {
412     for (int j = 0; j < test_loop_times; j++)
413     {
414         random_roi();
415
416         double eps = 2.0;
417 #ifdef ANDROID
418         // TODO investigate accuracy
419         if (cv::ocl::Device::getDefault().isNVidia())
420             eps = 8.0;
421 #endif
422
423         OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_LINEAR, borderType, val));
424         OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_LINEAR, borderType, val));
425
426         Near(eps);
427     }
428 }
429
430 /////////////////////////////////////////////////////////////////////////////////////
431
432 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine, Combine(
433                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
434                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
435                             Bool(),
436                             Bool()));
437
438 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine_cols4, Combine(
439                             Values((MatType)CV_8UC1),
440                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR),
441                             Bool(),
442                             Bool()));
443
444 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective, Combine(
445                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
446                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
447                             Bool(),
448                             Bool()));
449
450 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective_cols4, Combine(
451                             Values((MatType)CV_8UC1),
452                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR),
453                             Bool(),
454                             Bool()));
455
456 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Resize, Combine(
457                             Values(CV_8UC1, CV_8UC4, CV_16UC2, CV_32FC1, CV_32FC4),
458                             Values(0.5, 1.5, 2.0, 0.2),
459                             Values(0.5, 1.5, 2.0, 0.2),
460                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR),
461                             Bool(),
462                             Values(1, 16)));
463
464 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpResizeArea, Resize, Combine(
465                             Values((MatType)CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4),
466                             Values(0.7, 0.4, 0.5),
467                             Values(0.3, 0.6, 0.5),
468                             Values((Interpolation)INTER_AREA),
469                             Bool(),
470                             Values(1, 16)));
471
472 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_LINEAR, Combine(
473                             Values(CV_8U, CV_16U, CV_32F),
474                             Values(1, 3, 4),
475                             Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),
476                                    std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),
477                                    std::pair<MatType, MatType>((MatType)CV_32FC2, noType)),
478                             Values((BorderType)BORDER_CONSTANT,
479                                    (BorderType)BORDER_REPLICATE,
480                                    (BorderType)BORDER_WRAP,
481                                    (BorderType)BORDER_REFLECT,
482                                    (BorderType)BORDER_REFLECT_101),
483                             Bool()));
484
485 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_NEAREST, Combine(
486                             Values(CV_8U, CV_16U, CV_32F),
487                             Values(1, 3, 4),
488                             Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),
489                                    std::pair<MatType, MatType>((MatType)CV_32FC2, noType),
490                                    std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),
491                                    std::pair<MatType, MatType>((MatType)CV_16SC2, noType)),
492                             Values((BorderType)BORDER_CONSTANT,
493                                    (BorderType)BORDER_REPLICATE,
494                                    (BorderType)BORDER_WRAP,
495                                    (BorderType)BORDER_REFLECT,
496                                    (BorderType)BORDER_REFLECT_101),
497                             Bool()));
498
499 } } // namespace cvtest::ocl
500
501 #endif // HAVE_OPENCL