Merge pull request #2499 from mlyashko:back_proj_fix1
[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)
162 {
163     int type, interpolation;
164     double fx, fy;
165     bool useRoi;
166
167     TEST_DECLARE_INPUT_PARAMETER(src);
168     TEST_DECLARE_OUTPUT_PARAMETER(dst);
169
170     virtual void SetUp()
171     {
172         type = GET_PARAM(0);
173         fx = GET_PARAM(1);
174         fy = GET_PARAM(2);
175         interpolation = GET_PARAM(3);
176         useRoi = GET_PARAM(4);
177     }
178
179     void random_roi()
180     {
181         CV_Assert(fx > 0 && fy > 0);
182
183         Size srcRoiSize = randomSize(1, MAX_VALUE), dstRoiSize;
184         dstRoiSize.width = cvRound(srcRoiSize.width * fx);
185         dstRoiSize.height = cvRound(srcRoiSize.height * fy);
186
187         if (dstRoiSize.area() == 0)
188         {
189             random_roi();
190             return;
191         }
192
193         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
194         randomSubMat(src, src_roi, srcRoiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
195
196         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
197         randomSubMat(dst, dst_roi, dstRoiSize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
198
199         UMAT_UPLOAD_INPUT_PARAMETER(src);
200         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
201     }
202
203     void Near(double threshold = 0.0)
204     {
205         OCL_EXPECT_MATS_NEAR(dst, threshold);
206     }
207 };
208
209 OCL_TEST_P(Resize, Mat)
210 {
211     for (int j = 0; j < test_loop_times; j++)
212     {
213         int depth = CV_MAT_DEPTH(type);
214         double eps = depth <= CV_32S ? 1 : 1e-2;
215
216         random_roi();
217
218         OCL_OFF(cv::resize(src_roi, dst_roi, Size(), fx, fy, interpolation));
219         OCL_ON(cv::resize(usrc_roi, udst_roi, Size(), fx, fy, interpolation));
220
221         Near(eps);
222     }
223 }
224
225 /////////////////////////////////////////////////////////////////////////////////////////////////
226 // remap
227
228 PARAM_TEST_CASE(Remap, MatDepth, Channels, std::pair<MatType, MatType>, BorderType, bool)
229 {
230     int srcType, map1Type, map2Type;
231     int borderType;
232     bool useRoi;
233
234     Scalar val;
235
236     TEST_DECLARE_INPUT_PARAMETER(src);
237     TEST_DECLARE_INPUT_PARAMETER(map1);
238     TEST_DECLARE_INPUT_PARAMETER(map2);
239     TEST_DECLARE_OUTPUT_PARAMETER(dst);
240
241     virtual void SetUp()
242     {
243         srcType = CV_MAKE_TYPE(GET_PARAM(0), GET_PARAM(1));
244         map1Type = GET_PARAM(2).first;
245         map2Type = GET_PARAM(2).second;
246         borderType = GET_PARAM(3);
247         useRoi = GET_PARAM(4);
248     }
249
250     void random_roi()
251     {
252         val = randomScalar(-MAX_VALUE, MAX_VALUE);
253         Size srcROISize = randomSize(1, MAX_VALUE);
254         Size dstROISize = randomSize(1, MAX_VALUE);
255
256         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
257         randomSubMat(src, src_roi, srcROISize, srcBorder, srcType, 5, 256);
258
259         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
260         randomSubMat(dst, dst_roi, dstROISize, dstBorder, srcType, -MAX_VALUE, MAX_VALUE);
261
262         int mapMaxValue = MAX_VALUE << 2;
263         Border map1Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
264         randomSubMat(map1, map1_roi, dstROISize, map1Border, map1Type, -mapMaxValue, mapMaxValue);
265
266         Border map2Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
267         if (map2Type != noType)
268         {
269             int mapMinValue = -mapMaxValue;
270             if (map2Type == CV_16UC1 || map2Type == CV_16SC1)
271                 mapMinValue = 0, mapMaxValue = INTER_TAB_SIZE2;
272             randomSubMat(map2, map2_roi, dstROISize, map2Border, map2Type, mapMinValue, mapMaxValue);
273         }
274
275         UMAT_UPLOAD_INPUT_PARAMETER(src);
276         UMAT_UPLOAD_INPUT_PARAMETER(map1);
277         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
278         if (noType != map2Type)
279             UMAT_UPLOAD_INPUT_PARAMETER(map2);
280     }
281
282     void Near(double threshold = 0.0)
283     {
284         OCL_EXPECT_MATS_NEAR(dst, threshold);
285     }
286 };
287
288 typedef Remap Remap_INTER_NEAREST;
289
290 OCL_TEST_P(Remap_INTER_NEAREST, Mat)
291 {
292     for (int j = 0; j < test_loop_times; j++)
293     {
294         random_roi();
295
296         OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_NEAREST, borderType, val));
297         OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_NEAREST, borderType, val));
298
299         Near(1.0);
300     }
301 }
302
303 typedef Remap Remap_INTER_LINEAR;
304
305 OCL_TEST_P(Remap_INTER_LINEAR, Mat)
306 {
307     for (int j = 0; j < test_loop_times; j++)
308     {
309         random_roi();
310
311         OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_LINEAR, borderType, val));
312         OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_LINEAR, borderType, val));
313
314         Near(2.0);
315     }
316 }
317
318 /////////////////////////////////////////////////////////////////////////////////////
319
320 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine, Combine(
321                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
322                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
323                             Bool(),
324                             Bool()));
325
326 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective, Combine(
327                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
328                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
329                             Bool(),
330                             Bool()));
331
332 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Resize, Combine(
333                             Values(CV_8UC1, CV_8UC4, CV_16UC2, CV_32FC1, CV_32FC4),
334                             Values(0.5, 1.5, 2.0, 0.2),
335                             Values(0.5, 1.5, 2.0, 0.2),
336                             Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR),
337                             Bool()));
338
339 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpResizeArea, Resize, Combine(
340                             Values((MatType)CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4),
341                             Values(0.7, 0.4, 0.5),
342                             Values(0.3, 0.6, 0.5),
343                             Values((Interpolation)INTER_AREA),
344                             Bool()));
345
346 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_LINEAR, Combine(
347                             Values(CV_8U, CV_16U, CV_32F),
348                             Values(1, 3, 4),
349                             Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),
350                                    std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),
351                                    std::pair<MatType, MatType>((MatType)CV_32FC2, noType)),
352                             Values((BorderType)BORDER_CONSTANT,
353                                    (BorderType)BORDER_REPLICATE,
354                                    (BorderType)BORDER_WRAP,
355                                    (BorderType)BORDER_REFLECT,
356                                    (BorderType)BORDER_REFLECT_101),
357                             Bool()));
358
359 OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_NEAREST, Combine(
360                             Values(CV_8U, CV_16U, CV_32F),
361                             Values(1, 3, 4),
362                             Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),
363                                    std::pair<MatType, MatType>((MatType)CV_32FC2, noType),
364                                    std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),
365                                    std::pair<MatType, MatType>((MatType)CV_16SC2, noType)),
366                             Values((BorderType)BORDER_CONSTANT,
367                                    (BorderType)BORDER_REPLICATE,
368                                    (BorderType)BORDER_WRAP,
369                                    (BorderType)BORDER_REFLECT,
370                                    (BorderType)BORDER_REFLECT_101),
371                             Bool()));
372
373 } } // namespace cvtest::ocl
374
375 #endif // HAVE_OPENCL