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) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #include "test_precomp.hpp"
44 
45 #ifdef HAVE_CUDA
46 
47 using namespace cvtest;
48 
49 ///////////////////////////////////////////////////////////////////
50 // Gold implementation
51 
52 namespace
53 {
54     template <typename T, template <typename> class Interpolator>
resizeImpl(const cv::Mat & src,cv::Mat & dst,double fx,double fy)55     void resizeImpl(const cv::Mat& src, cv::Mat& dst, double fx, double fy)
56     {
57         const int cn = src.channels();
58 
59         cv::Size dsize(cv::saturate_cast<int>(src.cols * fx), cv::saturate_cast<int>(src.rows * fy));
60 
61         dst.create(dsize, src.type());
62 
63         float ifx = static_cast<float>(1.0 / fx);
64         float ify = static_cast<float>(1.0 / fy);
65 
66         for (int y = 0; y < dsize.height; ++y)
67         {
68             for (int x = 0; x < dsize.width; ++x)
69             {
70                 for (int c = 0; c < cn; ++c)
71                     dst.at<T>(y, x * cn + c) = Interpolator<T>::getValue(src, y * ify, x * ifx, c, cv::BORDER_REPLICATE);
72             }
73         }
74     }
75 
resizeGold(const cv::Mat & src,cv::Mat & dst,double fx,double fy,int interpolation)76     void resizeGold(const cv::Mat& src, cv::Mat& dst, double fx, double fy, int interpolation)
77     {
78         typedef void (*func_t)(const cv::Mat& src, cv::Mat& dst, double fx, double fy);
79 
80         static const func_t nearest_funcs[] =
81         {
82             resizeImpl<unsigned char, NearestInterpolator>,
83             resizeImpl<signed char, NearestInterpolator>,
84             resizeImpl<unsigned short, NearestInterpolator>,
85             resizeImpl<short, NearestInterpolator>,
86             resizeImpl<int, NearestInterpolator>,
87             resizeImpl<float, NearestInterpolator>
88         };
89 
90 
91         static const func_t linear_funcs[] =
92         {
93             resizeImpl<unsigned char, LinearInterpolator>,
94             resizeImpl<signed char, LinearInterpolator>,
95             resizeImpl<unsigned short, LinearInterpolator>,
96             resizeImpl<short, LinearInterpolator>,
97             resizeImpl<int, LinearInterpolator>,
98             resizeImpl<float, LinearInterpolator>
99         };
100 
101         static const func_t cubic_funcs[] =
102         {
103             resizeImpl<unsigned char, CubicInterpolator>,
104             resizeImpl<signed char, CubicInterpolator>,
105             resizeImpl<unsigned short, CubicInterpolator>,
106             resizeImpl<short, CubicInterpolator>,
107             resizeImpl<int, CubicInterpolator>,
108             resizeImpl<float, CubicInterpolator>
109         };
110 
111         static const func_t* funcs[] = {nearest_funcs, linear_funcs, cubic_funcs};
112 
113         funcs[interpolation][src.depth()](src, dst, fx, fy);
114     }
115 }
116 
117 ///////////////////////////////////////////////////////////////////
118 // Test
119 
PARAM_TEST_CASE(Resize,cv::cuda::DeviceInfo,cv::Size,MatType,double,Interpolation,UseRoi)120 PARAM_TEST_CASE(Resize, cv::cuda::DeviceInfo, cv::Size, MatType, double, Interpolation, UseRoi)
121 {
122     cv::cuda::DeviceInfo devInfo;
123     cv::Size size;
124     double coeff;
125     int interpolation;
126     int type;
127     bool useRoi;
128 
129     virtual void SetUp()
130     {
131         devInfo = GET_PARAM(0);
132         size = GET_PARAM(1);
133         type = GET_PARAM(2);
134         coeff = GET_PARAM(3);
135         interpolation = GET_PARAM(4);
136         useRoi = GET_PARAM(5);
137 
138         cv::cuda::setDevice(devInfo.deviceID());
139     }
140 };
141 
CUDA_TEST_P(Resize,Accuracy)142 CUDA_TEST_P(Resize, Accuracy)
143 {
144     cv::Mat src = randomMat(size, type);
145 
146     cv::cuda::GpuMat dst = createMat(cv::Size(cv::saturate_cast<int>(src.cols * coeff), cv::saturate_cast<int>(src.rows * coeff)), type, useRoi);
147     cv::cuda::resize(loadMat(src, useRoi), dst, cv::Size(), coeff, coeff, interpolation);
148 
149     cv::Mat dst_gold;
150     resizeGold(src, dst_gold, coeff, coeff, interpolation);
151 
152     EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-2 : 1.0);
153 }
154 
155 INSTANTIATE_TEST_CASE_P(CUDA_Warping, Resize, testing::Combine(
156     ALL_DEVICES,
157     DIFFERENT_SIZES,
158     testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
159     testing::Values(0.3, 0.5, 1.5, 2.0),
160     testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC)),
161     WHOLE_SUBMAT));
162 
163 /////////////////
164 
PARAM_TEST_CASE(ResizeSameAsHost,cv::cuda::DeviceInfo,cv::Size,MatType,double,Interpolation,UseRoi)165 PARAM_TEST_CASE(ResizeSameAsHost, cv::cuda::DeviceInfo, cv::Size, MatType, double, Interpolation, UseRoi)
166 {
167     cv::cuda::DeviceInfo devInfo;
168     cv::Size size;
169     double coeff;
170     int interpolation;
171     int type;
172     bool useRoi;
173 
174     virtual void SetUp()
175     {
176         devInfo = GET_PARAM(0);
177         size = GET_PARAM(1);
178         type = GET_PARAM(2);
179         coeff = GET_PARAM(3);
180         interpolation = GET_PARAM(4);
181         useRoi = GET_PARAM(5);
182 
183         cv::cuda::setDevice(devInfo.deviceID());
184     }
185 };
186 
187 // downscaling only: used for classifiers
CUDA_TEST_P(ResizeSameAsHost,Accuracy)188 CUDA_TEST_P(ResizeSameAsHost, Accuracy)
189 {
190     cv::Mat src = randomMat(size, type);
191 
192     cv::cuda::GpuMat dst = createMat(cv::Size(cv::saturate_cast<int>(src.cols * coeff), cv::saturate_cast<int>(src.rows * coeff)), type, useRoi);
193     cv::cuda::resize(loadMat(src, useRoi), dst, cv::Size(), coeff, coeff, interpolation);
194 
195     cv::Mat dst_gold;
196     cv::resize(src, dst_gold, cv::Size(), coeff, coeff, interpolation);
197 
198     EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-2 : 1.0);
199 }
200 
201 INSTANTIATE_TEST_CASE_P(CUDA_Warping, ResizeSameAsHost, testing::Combine(
202     ALL_DEVICES,
203     DIFFERENT_SIZES,
204     testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
205     testing::Values(0.3, 0.5),
206     testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_AREA)),
207     WHOLE_SUBMAT));
208 
209 #endif // HAVE_CUDA
210