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 namespace
50 {
createTransfomMatrix(cv::Size srcSize,double angle)51     cv::Mat createTransfomMatrix(cv::Size srcSize, double angle)
52     {
53         cv::Mat M(3, 3, CV_64FC1);
54 
55         M.at<double>(0, 0) = std::cos(angle); M.at<double>(0, 1) = -std::sin(angle); M.at<double>(0, 2) = srcSize.width / 2;
56         M.at<double>(1, 0) = std::sin(angle); M.at<double>(1, 1) =  std::cos(angle); M.at<double>(1, 2) = 0.0;
57         M.at<double>(2, 0) = 0.0            ; M.at<double>(2, 1) =  0.0            ; M.at<double>(2, 2) = 1.0;
58 
59         return M;
60     }
61 }
62 
63 ///////////////////////////////////////////////////////////////////
64 // Test buildWarpPerspectiveMaps
65 
PARAM_TEST_CASE(BuildWarpPerspectiveMaps,cv::cuda::DeviceInfo,cv::Size,Inverse)66 PARAM_TEST_CASE(BuildWarpPerspectiveMaps, cv::cuda::DeviceInfo, cv::Size, Inverse)
67 {
68     cv::cuda::DeviceInfo devInfo;
69     cv::Size size;
70     bool inverse;
71 
72     virtual void SetUp()
73     {
74         devInfo = GET_PARAM(0);
75         size = GET_PARAM(1);
76         inverse = GET_PARAM(2);
77 
78         cv::cuda::setDevice(devInfo.deviceID());
79     }
80 };
81 
CUDA_TEST_P(BuildWarpPerspectiveMaps,Accuracy)82 CUDA_TEST_P(BuildWarpPerspectiveMaps, Accuracy)
83 {
84     cv::Mat M = createTransfomMatrix(size, CV_PI / 4);
85 
86     cv::cuda::GpuMat xmap, ymap;
87     cv::cuda::buildWarpPerspectiveMaps(M, inverse, size, xmap, ymap);
88 
89     cv::Mat src = randomMat(randomSize(200, 400), CV_8UC1);
90     int interpolation = cv::INTER_NEAREST;
91     int borderMode = cv::BORDER_CONSTANT;
92     int flags = interpolation;
93     if (inverse)
94         flags |= cv::WARP_INVERSE_MAP;
95 
96     cv::Mat dst;
97     cv::remap(src, dst, cv::Mat(xmap), cv::Mat(ymap), interpolation, borderMode);
98 
99     cv::Mat dst_gold;
100     cv::warpPerspective(src, dst_gold, M, size, flags, borderMode);
101 
102     EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
103 }
104 
105 INSTANTIATE_TEST_CASE_P(CUDA_Warping, BuildWarpPerspectiveMaps, testing::Combine(
106     ALL_DEVICES,
107     DIFFERENT_SIZES,
108     DIRECT_INVERSE));
109 
110 ///////////////////////////////////////////////////////////////////
111 // Gold implementation
112 
113 namespace
114 {
warpPerspectiveImpl(const cv::Mat & src,const cv::Mat & M,cv::Size dsize,cv::Mat & dst,int borderType,cv::Scalar borderVal)115     template <typename T, template <typename> class Interpolator> void warpPerspectiveImpl(const cv::Mat& src, const cv::Mat& M, cv::Size dsize, cv::Mat& dst, int borderType, cv::Scalar borderVal)
116     {
117         const int cn = src.channels();
118 
119         dst.create(dsize, src.type());
120 
121         for (int y = 0; y < dsize.height; ++y)
122         {
123             for (int x = 0; x < dsize.width; ++x)
124             {
125                 float coeff = static_cast<float>(M.at<double>(2, 0) * x + M.at<double>(2, 1) * y + M.at<double>(2, 2));
126 
127                 float xcoo = static_cast<float>((M.at<double>(0, 0) * x + M.at<double>(0, 1) * y + M.at<double>(0, 2)) / coeff);
128                 float ycoo = static_cast<float>((M.at<double>(1, 0) * x + M.at<double>(1, 1) * y + M.at<double>(1, 2)) / coeff);
129 
130                 for (int c = 0; c < cn; ++c)
131                     dst.at<T>(y, x * cn + c) = Interpolator<T>::getValue(src, ycoo, xcoo, c, borderType, borderVal);
132             }
133         }
134     }
135 
warpPerspectiveGold(const cv::Mat & src,const cv::Mat & M,bool inverse,cv::Size dsize,cv::Mat & dst,int interpolation,int borderType,cv::Scalar borderVal)136     void warpPerspectiveGold(const cv::Mat& src, const cv::Mat& M, bool inverse, cv::Size dsize, cv::Mat& dst, int interpolation, int borderType, cv::Scalar borderVal)
137     {
138         typedef void (*func_t)(const cv::Mat& src, const cv::Mat& M, cv::Size dsize, cv::Mat& dst, int borderType, cv::Scalar borderVal);
139 
140         static const func_t nearest_funcs[] =
141         {
142             warpPerspectiveImpl<unsigned char, NearestInterpolator>,
143             warpPerspectiveImpl<signed char, NearestInterpolator>,
144             warpPerspectiveImpl<unsigned short, NearestInterpolator>,
145             warpPerspectiveImpl<short, NearestInterpolator>,
146             warpPerspectiveImpl<int, NearestInterpolator>,
147             warpPerspectiveImpl<float, NearestInterpolator>
148         };
149 
150         static const func_t linear_funcs[] =
151         {
152             warpPerspectiveImpl<unsigned char, LinearInterpolator>,
153             warpPerspectiveImpl<signed char, LinearInterpolator>,
154             warpPerspectiveImpl<unsigned short, LinearInterpolator>,
155             warpPerspectiveImpl<short, LinearInterpolator>,
156             warpPerspectiveImpl<int, LinearInterpolator>,
157             warpPerspectiveImpl<float, LinearInterpolator>
158         };
159 
160         static const func_t cubic_funcs[] =
161         {
162             warpPerspectiveImpl<unsigned char, CubicInterpolator>,
163             warpPerspectiveImpl<signed char, CubicInterpolator>,
164             warpPerspectiveImpl<unsigned short, CubicInterpolator>,
165             warpPerspectiveImpl<short, CubicInterpolator>,
166             warpPerspectiveImpl<int, CubicInterpolator>,
167             warpPerspectiveImpl<float, CubicInterpolator>
168         };
169 
170         static const func_t* funcs[] = {nearest_funcs, linear_funcs, cubic_funcs};
171 
172         if (inverse)
173             funcs[interpolation][src.depth()](src, M, dsize, dst, borderType, borderVal);
174         else
175         {
176             cv::Mat iM;
177             cv::invert(M, iM);
178             funcs[interpolation][src.depth()](src, iM, dsize, dst, borderType, borderVal);
179         }
180     }
181 }
182 
183 ///////////////////////////////////////////////////////////////////
184 // Test
185 
PARAM_TEST_CASE(WarpPerspective,cv::cuda::DeviceInfo,cv::Size,MatType,Inverse,Interpolation,BorderType,UseRoi)186 PARAM_TEST_CASE(WarpPerspective, cv::cuda::DeviceInfo, cv::Size, MatType, Inverse, Interpolation, BorderType, UseRoi)
187 {
188     cv::cuda::DeviceInfo devInfo;
189     cv::Size size;
190     int type;
191     bool inverse;
192     int interpolation;
193     int borderType;
194     bool useRoi;
195 
196     virtual void SetUp()
197     {
198         devInfo = GET_PARAM(0);
199         size = GET_PARAM(1);
200         type = GET_PARAM(2);
201         inverse = GET_PARAM(3);
202         interpolation = GET_PARAM(4);
203         borderType = GET_PARAM(5);
204         useRoi = GET_PARAM(6);
205 
206         cv::cuda::setDevice(devInfo.deviceID());
207     }
208 };
209 
CUDA_TEST_P(WarpPerspective,Accuracy)210 CUDA_TEST_P(WarpPerspective, Accuracy)
211 {
212     cv::Mat src = randomMat(size, type);
213     cv::Mat M = createTransfomMatrix(size, CV_PI / 3);
214     int flags = interpolation;
215     if (inverse)
216         flags |= cv::WARP_INVERSE_MAP;
217     cv::Scalar val = randomScalar(0.0, 255.0);
218 
219     cv::cuda::GpuMat dst = createMat(size, type, useRoi);
220     cv::cuda::warpPerspective(loadMat(src, useRoi), dst, M, size, flags, borderType, val);
221 
222     cv::Mat dst_gold;
223     warpPerspectiveGold(src, M, inverse, size, dst_gold, interpolation, borderType, val);
224 
225     EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-1 : 1.0);
226 }
227 
228 INSTANTIATE_TEST_CASE_P(CUDA_Warping, WarpPerspective, testing::Combine(
229     ALL_DEVICES,
230     DIFFERENT_SIZES,
231     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)),
232     DIRECT_INVERSE,
233     testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC)),
234     testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_REFLECT), BorderType(cv::BORDER_WRAP)),
235     WHOLE_SUBMAT));
236 
237 ///////////////////////////////////////////////////////////////////
238 // Test NPP
239 
PARAM_TEST_CASE(WarpPerspectiveNPP,cv::cuda::DeviceInfo,MatType,Inverse,Interpolation)240 PARAM_TEST_CASE(WarpPerspectiveNPP, cv::cuda::DeviceInfo, MatType, Inverse, Interpolation)
241 {
242     cv::cuda::DeviceInfo devInfo;
243     int type;
244     bool inverse;
245     int interpolation;
246 
247     virtual void SetUp()
248     {
249         devInfo = GET_PARAM(0);
250         type = GET_PARAM(1);
251         inverse = GET_PARAM(2);
252         interpolation = GET_PARAM(3);
253 
254         cv::cuda::setDevice(devInfo.deviceID());
255     }
256 };
257 
CUDA_TEST_P(WarpPerspectiveNPP,Accuracy)258 CUDA_TEST_P(WarpPerspectiveNPP, Accuracy)
259 {
260     cv::Mat src = readImageType("stereobp/aloe-L.png", type);
261     ASSERT_FALSE(src.empty());
262 
263     cv::Mat M = createTransfomMatrix(src.size(), CV_PI / 4);
264     int flags = interpolation;
265     if (inverse)
266         flags |= cv::WARP_INVERSE_MAP;
267 
268     cv::cuda::GpuMat dst;
269     cv::cuda::warpPerspective(loadMat(src), dst, M, src.size(), flags);
270 
271     cv::Mat dst_gold;
272     warpPerspectiveGold(src, M, inverse, src.size(), dst_gold, interpolation, cv::BORDER_CONSTANT, cv::Scalar::all(0));
273 
274     EXPECT_MAT_SIMILAR(dst_gold, dst, 2e-2);
275 }
276 
277 INSTANTIATE_TEST_CASE_P(CUDA_Warping, WarpPerspectiveNPP, testing::Combine(
278     ALL_DEVICES,
279     testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
280     DIRECT_INVERSE,
281     testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC))));
282 
283 #endif // HAVE_CUDA
284