Merge pull request #1552 from Exocoder:gtk3
[profile/ivi/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
76     TEST_DECLARE_INPUT_PARAMETER(src);
77     TEST_DECLARE_OUTPUT_PARAMETER(dst);
78
79     virtual void SetUp()
80     {
81         type = GET_PARAM(0);
82         interpolation = GET_PARAM(1);
83         mapInverse = GET_PARAM(2);
84         useRoi = GET_PARAM(3);
85
86         if (mapInverse)
87             interpolation |= WARP_INVERSE_MAP;
88     }
89
90     void random_roi()
91     {
92         dsize = randomSize(1, MAX_VALUE);
93
94         Size roiSize = randomSize(1, MAX_VALUE);
95         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
96         randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
97
98         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
99         randomSubMat(dst, dst_roi, dsize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
100
101         UMAT_UPLOAD_INPUT_PARAMETER(src);
102         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
103     }
104
105     void Near(double threshold = 0.0)
106     {
107         OCL_EXPECT_MATS_NEAR(dst, threshold);
108     }
109 };
110
111 /////warpAffine
112
113 typedef WarpTestBase WarpAffine;
114
115 OCL_TEST_P(WarpAffine, Mat)
116 {
117     for (int j = 0; j < test_loop_times; j++)
118     {
119         random_roi();
120
121         Mat M = getRotationMatrix2D(Point2f(src_roi.cols / 2.0f, src_roi.rows / 2.0f),
122             rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
123
124         OCL_OFF(cv::warpAffine(src_roi, dst_roi, M, dsize, interpolation));
125         OCL_ON(cv::warpAffine(usrc_roi, udst_roi, M, dsize, interpolation));
126
127         Near(1.0);
128     }
129 }
130
131 //// warpPerspective
132
133 typedef WarpTestBase WarpPerspective;
134
135 OCL_TEST_P(WarpPerspective, Mat)
136 {
137     for (int j = 0; j < test_loop_times; j++)
138     {
139         random_roi();
140
141         float cols = static_cast<float>(src_roi.cols), rows = static_cast<float>(src_roi.rows);
142         float cols2 = cols / 2.0f, rows2 = rows / 2.0f;
143         Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };
144         Point2f dp[] = { Point2f(rng.uniform(0.0f, cols2), rng.uniform(0.0f, rows2)),
145             Point2f(rng.uniform(cols2, cols), rng.uniform(0.0f, rows2)),
146             Point2f(rng.uniform(0.0f, cols2), rng.uniform(rows2, rows)),
147             Point2f(rng.uniform(cols2, cols), rng.uniform(rows2, rows)) };
148         Mat M = getPerspectiveTransform(sp, dp);
149
150         OCL_OFF(cv::warpPerspective(src_roi, dst_roi, M, dsize, interpolation));
151         OCL_ON(cv::warpPerspective(usrc_roi, udst_roi, M, dsize, interpolation));
152
153         Near(1.0);
154     }
155 }
156
157
158 /////////////////////////////////////////////////////////////////////////////////////////////////
159 //// resize
160
161 PARAM_TEST_CASE(Resize, MatType, double, double, Interpolation, bool, int)
162 {
163     int type, interpolation;
164     int widthMultiple;
165     double fx, fy;
166     bool useRoi;
167
168     TEST_DECLARE_INPUT_PARAMETER(src);
169     TEST_DECLARE_OUTPUT_PARAMETER(dst);
170
171     virtual void SetUp()
172     {
173         type = GET_PARAM(0);
174         fx = GET_PARAM(1);
175         fy = GET_PARAM(2);
176         interpolation = GET_PARAM(3);
177         useRoi = GET_PARAM(4);
178         widthMultiple = GET_PARAM(5);
179     }
180
181     void random_roi()
182     {
183         CV_Assert(fx > 0 && fy > 0);
184
185         Size srcRoiSize = randomSize(1, MAX_VALUE), dstRoiSize;
186         // Make sure the width is a multiple of the requested value, and no more
187         srcRoiSize.width += widthMultiple - 1 - (srcRoiSize.width - 1) % widthMultiple;
188         dstRoiSize.width = cvRound(srcRoiSize.width * fx);
189         dstRoiSize.height = cvRound(srcRoiSize.height * fy);
190
191         if (dstRoiSize.area() == 0)
192         {
193             random_roi();
194             return;
195         }
196
197         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
198         randomSubMat(src, src_roi, srcRoiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
199
200         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
201         randomSubMat(dst, dst_roi, dstRoiSize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
202
203         UMAT_UPLOAD_INPUT_PARAMETER(src);
204         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
205     }
206
207     void Near(double threshold = 0.0)
208     {
209         OCL_EXPECT_MATS_NEAR(dst, threshold);
210     }
211 };
212
213 OCL_TEST_P(Resize, Mat)
214 {
215     for (int j = 0; j < test_loop_times; j++)
216     {
217         int depth = CV_MAT_DEPTH(type);
218         double eps = depth <= CV_32S ? 1 : 1e-2;
219
220         random_roi();
221
222         OCL_OFF(cv::resize(src_roi, dst_roi, Size(), fx, fy, interpolation));
223         OCL_ON(cv::resize(usrc_roi, udst_roi, Size(), fx, fy, interpolation));
224
225         Near(eps);
226     }
227 }
228
229 /////////////////////////////////////////////////////////////////////////////////////////////////
230 // remap
231
232 PARAM_TEST_CASE(Remap, MatDepth, Channels, std::pair<MatType, MatType>, BorderType, bool)
233 {
234     int srcType, map1Type, map2Type;
235     int borderType;
236     bool useRoi;
237
238     Scalar val;
239
240     TEST_DECLARE_INPUT_PARAMETER(src);
241     TEST_DECLARE_INPUT_PARAMETER(map1);
242     TEST_DECLARE_INPUT_PARAMETER(map2);
243     TEST_DECLARE_OUTPUT_PARAMETER(dst);
244
245     virtual void SetUp()
246     {
247         srcType = CV_MAKE_TYPE(GET_PARAM(0), GET_PARAM(1));
248         map1Type = GET_PARAM(2).first;
249         map2Type = GET_PARAM(2).second;
250         borderType = GET_PARAM(3);
251         useRoi = GET_PARAM(4);
252     }
253
254     void random_roi()
255     {
256         val = randomScalar(-MAX_VALUE, MAX_VALUE);
257         Size srcROISize = randomSize(1, MAX_VALUE);
258         Size dstROISize = randomSize(1, MAX_VALUE);
259
260         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
261         randomSubMat(src, src_roi, srcROISize, srcBorder, srcType, 5, 256);
262
263         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
264         randomSubMat(dst, dst_roi, dstROISize, dstBorder, srcType, -MAX_VALUE, MAX_VALUE);
265
266         int mapMaxValue = MAX_VALUE << 2;
267         Border map1Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
268         randomSubMat(map1, map1_roi, dstROISize, map1Border, map1Type, -mapMaxValue, mapMaxValue);
269
270         Border map2Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
271         if (map2Type != noType)
272         {
273             int mapMinValue = -mapMaxValue;
274             if (map2Type == CV_16UC1 || map2Type == CV_16SC1)
275                 mapMinValue = 0, mapMaxValue = INTER_TAB_SIZE2;
276             randomSubMat(map2, map2_roi, dstROISize, map2Border, map2Type, mapMinValue, mapMaxValue);
277         }
278
279         UMAT_UPLOAD_INPUT_PARAMETER(src);
280         UMAT_UPLOAD_INPUT_PARAMETER(map1);
281         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
282         if (noType != map2Type)
283             UMAT_UPLOAD_INPUT_PARAMETER(map2);
284     }
285
286     void Near(double threshold = 0.0)
287     {
288         OCL_EXPECT_MATS_NEAR(dst, threshold);
289     }
290 };
291
292 typedef Remap Remap_INTER_NEAREST;
293
294 OCL_TEST_P(Remap_INTER_NEAREST, Mat)
295 {
296     for (int j = 0; j < test_loop_times; j++)
297     {
298         random_roi();
299
300         OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_NEAREST, borderType, val));
301         OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_NEAREST, borderType, val));
302
303         Near(1.0);
304     }
305 }
306
307 typedef Remap Remap_INTER_LINEAR;
308
309 OCL_TEST_P(Remap_INTER_LINEAR, Mat)
310 {
311     for (int j = 0; j < test_loop_times; j++)
312     {
313         random_roi();
314
315         OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_LINEAR, borderType, val));
316         OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_LINEAR, borderType, val));
317
318         Near(2.0);
319     }
320 }
321
322 /////////////////////////////////////////////////////////////////////////////////////
323
324 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine, Combine(
325                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
326                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
327                             Bool(),
328                             Bool()));
329
330 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective, Combine(
331                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
332                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
333                             Bool(),
334                             Bool()));
335
336 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Resize, Combine(
337                             Values(CV_8UC1, CV_8UC4, CV_16UC2, CV_32FC1, CV_32FC4),
338                             Values(0.5, 1.5, 2.0, 0.2),
339                             Values(0.5, 1.5, 2.0, 0.2),
340                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR),
341                             Bool(),
342                             Values(1, 16)));
343
344 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpResizeArea, Resize, Combine(
345                             Values((MatType)CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4),
346                             Values(0.7, 0.4, 0.5),
347                             Values(0.3, 0.6, 0.5),
348                             Values((Interpolation)INTER_AREA),
349                             Bool(),
350                             Values(1, 16)));
351
352 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_LINEAR, Combine(
353                             Values(CV_8U, CV_16U, CV_32F),
354                             Values(1, 3, 4),
355                             Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),
356                                    std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),
357                                    std::pair<MatType, MatType>((MatType)CV_32FC2, noType)),
358                             Values((BorderType)BORDER_CONSTANT,
359                                    (BorderType)BORDER_REPLICATE,
360                                    (BorderType)BORDER_WRAP,
361                                    (BorderType)BORDER_REFLECT,
362                                    (BorderType)BORDER_REFLECT_101),
363                             Bool()));
364
365 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_NEAREST, Combine(
366                             Values(CV_8U, CV_16U, CV_32F),
367                             Values(1, 3, 4),
368                             Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),
369                                    std::pair<MatType, MatType>((MatType)CV_32FC2, noType),
370                                    std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),
371                                    std::pair<MatType, MatType>((MatType)CV_16SC2, noType)),
372                             Values((BorderType)BORDER_CONSTANT,
373                                    (BorderType)BORDER_REPLICATE,
374                                    (BorderType)BORDER_WRAP,
375                                    (BorderType)BORDER_REFLECT,
376                                    (BorderType)BORDER_REFLECT_101),
377                             Bool()));
378
379 } } // namespace cvtest::ocl
380
381 #endif // HAVE_OPENCL