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 {
remapImpl(const cv::Mat & src,const cv::Mat & xmap,const cv::Mat & ymap,cv::Mat & dst,int borderType,cv::Scalar borderVal)54     template <typename T, template <typename> class Interpolator> void remapImpl(const cv::Mat& src, const cv::Mat& xmap, const cv::Mat& ymap, cv::Mat& dst, int borderType, cv::Scalar borderVal)
55     {
56         const int cn = src.channels();
57 
58         cv::Size dsize = xmap.size();
59 
60         dst.create(dsize, src.type());
61 
62         for (int y = 0; y < dsize.height; ++y)
63         {
64             for (int x = 0; x < dsize.width; ++x)
65             {
66                 for (int c = 0; c < cn; ++c)
67                     dst.at<T>(y, x * cn + c) = Interpolator<T>::getValue(src, ymap.at<float>(y, x), xmap.at<float>(y, x), c, borderType, borderVal);
68             }
69         }
70     }
71 
remapGold(const cv::Mat & src,const cv::Mat & xmap,const cv::Mat & ymap,cv::Mat & dst,int interpolation,int borderType,cv::Scalar borderVal)72     void remapGold(const cv::Mat& src, const cv::Mat& xmap, const cv::Mat& ymap, cv::Mat& dst, int interpolation, int borderType, cv::Scalar borderVal)
73     {
74         typedef void (*func_t)(const cv::Mat& src, const cv::Mat& xmap, const cv::Mat& ymap, cv::Mat& dst, int borderType, cv::Scalar borderVal);
75 
76         static const func_t nearest_funcs[] =
77         {
78             remapImpl<unsigned char, NearestInterpolator>,
79             remapImpl<signed char, NearestInterpolator>,
80             remapImpl<unsigned short, NearestInterpolator>,
81             remapImpl<short, NearestInterpolator>,
82             remapImpl<int, NearestInterpolator>,
83             remapImpl<float, NearestInterpolator>
84         };
85 
86         static const func_t linear_funcs[] =
87         {
88             remapImpl<unsigned char, LinearInterpolator>,
89             remapImpl<signed char, LinearInterpolator>,
90             remapImpl<unsigned short, LinearInterpolator>,
91             remapImpl<short, LinearInterpolator>,
92             remapImpl<int, LinearInterpolator>,
93             remapImpl<float, LinearInterpolator>
94         };
95 
96         static const func_t cubic_funcs[] =
97         {
98             remapImpl<unsigned char, CubicInterpolator>,
99             remapImpl<signed char, CubicInterpolator>,
100             remapImpl<unsigned short, CubicInterpolator>,
101             remapImpl<short, CubicInterpolator>,
102             remapImpl<int, CubicInterpolator>,
103             remapImpl<float, CubicInterpolator>
104         };
105 
106         static const func_t* funcs[] = {nearest_funcs, linear_funcs, cubic_funcs};
107 
108         funcs[interpolation][src.depth()](src, xmap, ymap, dst, borderType, borderVal);
109     }
110 }
111 
112 ///////////////////////////////////////////////////////////////////
113 // Test
114 
PARAM_TEST_CASE(Remap,cv::cuda::DeviceInfo,cv::Size,MatType,Interpolation,BorderType,UseRoi)115 PARAM_TEST_CASE(Remap, cv::cuda::DeviceInfo, cv::Size, MatType, Interpolation, BorderType, UseRoi)
116 {
117     cv::cuda::DeviceInfo devInfo;
118     cv::Size size;
119     int type;
120     int interpolation;
121     int borderType;
122     bool useRoi;
123 
124     cv::Mat xmap;
125     cv::Mat ymap;
126 
127     virtual void SetUp()
128     {
129         devInfo = GET_PARAM(0);
130         size = GET_PARAM(1);
131         type = GET_PARAM(2);
132         interpolation = GET_PARAM(3);
133         borderType = GET_PARAM(4);
134         useRoi = GET_PARAM(5);
135 
136         cv::cuda::setDevice(devInfo.deviceID());
137 
138         // rotation matrix
139 
140         const double aplha = CV_PI / 4;
141         static double M[2][3] = { {std::cos(aplha), -std::sin(aplha), size.width / 2.0},
142                                   {std::sin(aplha),  std::cos(aplha), 0.0}};
143 
144         xmap.create(size, CV_32FC1);
145         ymap.create(size, CV_32FC1);
146 
147         for (int y = 0; y < size.height; ++y)
148         {
149             for (int x = 0; x < size.width; ++x)
150             {
151                 xmap.at<float>(y, x) = static_cast<float>(M[0][0] * x + M[0][1] * y + M[0][2]);
152                 ymap.at<float>(y, x) = static_cast<float>(M[1][0] * x + M[1][1] * y + M[1][2]);
153             }
154         }
155     }
156 };
157 
CUDA_TEST_P(Remap,Accuracy)158 CUDA_TEST_P(Remap, Accuracy)
159 {
160     cv::Mat src = randomMat(size, type);
161     cv::Scalar val = randomScalar(0.0, 255.0);
162 
163     cv::cuda::GpuMat dst = createMat(xmap.size(), type, useRoi);
164     cv::cuda::remap(loadMat(src, useRoi), dst, loadMat(xmap, useRoi), loadMat(ymap, useRoi), interpolation, borderType, val);
165 
166     cv::Mat dst_gold;
167     remapGold(src, xmap, ymap, dst_gold, interpolation, borderType, val);
168 
169     EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-3 : 1.0);
170 }
171 
172 INSTANTIATE_TEST_CASE_P(CUDA_Warping, Remap, testing::Combine(
173     ALL_DEVICES,
174     DIFFERENT_SIZES,
175     testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
176     testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC)),
177     testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_CONSTANT), BorderType(cv::BORDER_REFLECT), BorderType(cv::BORDER_WRAP)),
178     WHOLE_SUBMAT));
179 
180 #endif // HAVE_CUDA
181