performance test for INTER_AREA resize has been added.
[profile/ivi/opencv.git] / modules / gpu / test / test_resize.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                        Intel License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.\r
14 // Third party copyrights are property of their respective owners.\r
15 //\r
16 // Redistribution and use in source and binary forms, with or without modification,\r
17 // are permitted provided that the following conditions are met:\r
18 //\r
19 //   * Redistribution's of source code must retain the above copyright notice,\r
20 //     this list of conditions and the following disclaimer.\r
21 //\r
22 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
23 //     this list of conditions and the following disclaimer in the documentation\r
24 //     and/or other materials provided with the distribution.\r
25 //\r
26 //   * The name of Intel Corporation may not be used to endorse or promote products\r
27 //     derived from this software without specific prior written permission.\r
28 //\r
29 // This software is provided by the copyright holders and contributors "as is" and\r
30 // any express or implied warranties, including, but not limited to, the implied\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
33 // indirect, incidental, special, exemplary, or consequential damages\r
34 // (including, but not limited to, procurement of substitute goods or services;\r
35 // loss of use, data, or profits; or business interruption) however caused\r
36 // and on any theory of liability, whether in contract, strict liability,\r
37 // or tort (including negligence or otherwise) arising in any way out of\r
38 // the use of this software, even if advised of the possibility of such damage.\r
39 //\r
40 //M*/\r
41 \r
42 #include "precomp.hpp"\r
43 \r
44 #ifdef HAVE_CUDA\r
45 \r
46 ///////////////////////////////////////////////////////////////////\r
47 // Gold implementation\r
48 \r
49 namespace\r
50 {\r
51     template <typename T, template <typename> class Interpolator>\r
52     void resizeImpl(const cv::Mat& src, cv::Mat& dst, double fx, double fy)\r
53     {\r
54         const int cn = src.channels();\r
55 \r
56         cv::Size dsize(cv::saturate_cast<int>(src.cols * fx), cv::saturate_cast<int>(src.rows * fy));\r
57 \r
58         dst.create(dsize, src.type());\r
59 \r
60         float ifx = static_cast<float>(1.0 / fx);\r
61         float ify = static_cast<float>(1.0 / fy);\r
62 \r
63         for (int y = 0; y < dsize.height; ++y)\r
64         {\r
65             for (int x = 0; x < dsize.width; ++x)\r
66             {\r
67                 for (int c = 0; c < cn; ++c)\r
68                     dst.at<T>(y, x * cn + c) = Interpolator<T>::getValue(src, y * ify, x * ifx, c, cv::BORDER_REPLICATE);\r
69             }\r
70         }\r
71     }\r
72 \r
73     void resizeGold(const cv::Mat& src, cv::Mat& dst, double fx, double fy, int interpolation)\r
74     {\r
75         typedef void (*func_t)(const cv::Mat& src, cv::Mat& dst, double fx, double fy);\r
76 \r
77         static const func_t nearest_funcs[] =\r
78         {\r
79             resizeImpl<unsigned char, NearestInterpolator>,\r
80             resizeImpl<signed char, NearestInterpolator>,\r
81             resizeImpl<unsigned short, NearestInterpolator>,\r
82             resizeImpl<short, NearestInterpolator>,\r
83             resizeImpl<int, NearestInterpolator>,\r
84             resizeImpl<float, NearestInterpolator>\r
85         };\r
86 \r
87 \r
88         static const func_t linear_funcs[] =\r
89         {\r
90             resizeImpl<unsigned char, LinearInterpolator>,\r
91             resizeImpl<signed char, LinearInterpolator>,\r
92             resizeImpl<unsigned short, LinearInterpolator>,\r
93             resizeImpl<short, LinearInterpolator>,\r
94             resizeImpl<int, LinearInterpolator>,\r
95             resizeImpl<float, LinearInterpolator>\r
96         };\r
97 \r
98         static const func_t cubic_funcs[] =\r
99         {\r
100             resizeImpl<unsigned char, CubicInterpolator>,\r
101             resizeImpl<signed char, CubicInterpolator>,\r
102             resizeImpl<unsigned short, CubicInterpolator>,\r
103             resizeImpl<short, CubicInterpolator>,\r
104             resizeImpl<int, CubicInterpolator>,\r
105             resizeImpl<float, CubicInterpolator>\r
106         };\r
107 \r
108         static const func_t* funcs[] = {nearest_funcs, linear_funcs, cubic_funcs};\r
109 \r
110         funcs[interpolation][src.depth()](src, dst, fx, fy);\r
111     }\r
112 }\r
113 \r
114 ///////////////////////////////////////////////////////////////////\r
115 // Test\r
116 \r
117 PARAM_TEST_CASE(Resize, cv::gpu::DeviceInfo, cv::Size, MatType, double, Interpolation, UseRoi)\r
118 {\r
119     cv::gpu::DeviceInfo devInfo;\r
120     cv::Size size;\r
121     double coeff;\r
122     int interpolation;\r
123     int type;\r
124     bool useRoi;\r
125 \r
126     virtual void SetUp()\r
127     {\r
128         devInfo = GET_PARAM(0);\r
129         size = GET_PARAM(1);\r
130         type = GET_PARAM(2);\r
131         coeff = GET_PARAM(3);\r
132         interpolation = GET_PARAM(4);\r
133         useRoi = GET_PARAM(5);\r
134 \r
135         cv::gpu::setDevice(devInfo.deviceID());\r
136     }\r
137 };\r
138 \r
139 TEST_P(Resize, Accuracy)\r
140 {\r
141     cv::Mat src = randomMat(size, type);\r
142 \r
143     cv::gpu::GpuMat dst = createMat(cv::Size(cv::saturate_cast<int>(src.cols * coeff), cv::saturate_cast<int>(src.rows * coeff)), type, useRoi);\r
144     cv::gpu::resize(loadMat(src, useRoi), dst, cv::Size(), coeff, coeff, interpolation);\r
145 \r
146     cv::Mat dst_gold;\r
147     resizeGold(src, dst_gold, coeff, coeff, interpolation);\r
148 \r
149     EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-2 : 1.0);\r
150 }\r
151 \r
152 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Resize, testing::Combine(\r
153     ALL_DEVICES,\r
154     DIFFERENT_SIZES,\r
155     testing::Values(MatType(CV_8UC3), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),\r
156     testing::Values(0.3, 0.5, 1.5, 2.0),\r
157     testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC)),\r
158     WHOLE_SUBMAT));\r
159 \r
160 \r
161 /////////////////\r
162 PARAM_TEST_CASE(ResizeArea, cv::gpu::DeviceInfo, cv::Size, MatType, double, Interpolation, UseRoi)\r
163 {\r
164     cv::gpu::DeviceInfo devInfo;\r
165     cv::Size size;\r
166     double coeff;\r
167     int interpolation;\r
168     int type;\r
169     bool useRoi;\r
170 \r
171     virtual void SetUp()\r
172     {\r
173         devInfo = GET_PARAM(0);\r
174         size = GET_PARAM(1);\r
175         type = GET_PARAM(2);\r
176         coeff = GET_PARAM(3);\r
177         interpolation = GET_PARAM(4);\r
178         useRoi = GET_PARAM(5);\r
179 \r
180         cv::gpu::setDevice(devInfo.deviceID());\r
181     }\r
182 };\r
183 \r
184 TEST_P(ResizeArea, Accuracy)\r
185 {\r
186     cv::Mat src = randomMat(size, type);\r
187 \r
188     cv::gpu::GpuMat dst = createMat(cv::Size(cv::saturate_cast<int>(src.cols * coeff), cv::saturate_cast<int>(src.rows * coeff)), type, useRoi);\r
189     cv::gpu::resize(loadMat(src, useRoi), dst, cv::Size(), coeff, coeff, interpolation);\r
190 \r
191     cv::Mat dst_cpu;\r
192     cv::resize(src, dst_cpu, cv::Size(), coeff, coeff, interpolation);\r
193 \r
194     EXPECT_MAT_NEAR(dst_cpu, dst, src.depth() == CV_32F ? 1e-2 : 1.0);\r
195 }\r
196 \r
197 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, ResizeArea, testing::Combine(\r
198     ALL_DEVICES,\r
199     DIFFERENT_SIZES,\r
200     testing::Values(MatType(CV_8UC3), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),\r
201     testing::Values(0.3, 0.5),\r
202     testing::Values(Interpolation(cv::INTER_AREA)),\r
203     WHOLE_SUBMAT));\r
204 \r
205 ///////////////////////////////////////////////////////////////////\r
206 // Test NPP\r
207 \r
208 PARAM_TEST_CASE(ResizeNPP, cv::gpu::DeviceInfo, MatType, double, Interpolation)\r
209 {\r
210     cv::gpu::DeviceInfo devInfo;\r
211     double coeff;\r
212     int interpolation;\r
213     int type;\r
214 \r
215     virtual void SetUp()\r
216     {\r
217         devInfo = GET_PARAM(0);\r
218         type = GET_PARAM(1);\r
219         coeff = GET_PARAM(2);\r
220         interpolation = GET_PARAM(3);\r
221 \r
222         cv::gpu::setDevice(devInfo.deviceID());\r
223     }\r
224 };\r
225 \r
226 TEST_P(ResizeNPP, Accuracy)\r
227 {\r
228     if (type == CV_8UC1 && interpolation == cv::INTER_CUBIC)\r
229         return;\r
230 \r
231     cv::Mat src = readImageType("stereobp/aloe-L.png", type);\r
232 \r
233     cv::gpu::GpuMat dst;\r
234     cv::gpu::resize(loadMat(src), dst, cv::Size(), coeff, coeff, interpolation);\r
235 \r
236     cv::Mat dst_gold;\r
237     resizeGold(src, dst_gold, coeff, coeff, interpolation);\r
238 \r
239     EXPECT_MAT_SIMILAR(dst_gold, dst, 1e-1);\r
240 }\r
241 \r
242 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, ResizeNPP, testing::Combine(\r
243     ALL_DEVICES,\r
244     testing::Values(MatType(CV_8UC1), MatType(CV_8UC4)),\r
245     testing::Values(0.3, 0.5, 1.5, 2.0),\r
246     testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC))));\r
247 \r
248 #endif // HAVE_CUDA\r