1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11
// For Open Source Computer Vision Library
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.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
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.
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.
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.
43
#include "test_precomp.hpp"
47
using namespace cvtest;
51
cv::Mat createTransfomMatrix(cv::Size srcSize, double angle)
53
cv::Mat M(3, 3, CV_64FC1);
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;
63
///////////////////////////////////////////////////////////////////
64
// Test buildWarpPerspectiveMaps
66
PARAM_TEST_CASE(BuildWarpPerspectiveMaps, cv::cuda::DeviceInfo, cv::Size, Inverse)
68
cv::cuda::DeviceInfo devInfo;
74
devInfo = GET_PARAM(0);
76
inverse = GET_PARAM(2);
78
cv::cuda::setDevice(devInfo.deviceID());
82
CUDA_TEST_P(BuildWarpPerspectiveMaps, Accuracy)
84
cv::Mat M = createTransfomMatrix(size, CV_PI / 4);
86
cv::cuda::GpuMat xmap, ymap;
87
cv::cuda::buildWarpPerspectiveMaps(M, inverse, size, xmap, ymap);
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;
94
flags |= cv::WARP_INVERSE_MAP;
97
cv::remap(src, dst, cv::Mat(xmap), cv::Mat(ymap), interpolation, borderMode);
100
cv::warpPerspective(src, dst_gold, M, size, flags, borderMode);
102
EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
105
INSTANTIATE_TEST_CASE_P(CUDA_Warping, BuildWarpPerspectiveMaps, testing::Combine(
110
///////////////////////////////////////////////////////////////////
111
// Gold implementation
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)
117
const int cn = src.channels();
119
dst.create(dsize, src.type());
121
for (int y = 0; y < dsize.height; ++y)
123
for (int x = 0; x < dsize.width; ++x)
125
float coeff = static_cast<float>(M.at<double>(2, 0) * x + M.at<double>(2, 1) * y + M.at<double>(2, 2));
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);
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);
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)
138
typedef void (*func_t)(const cv::Mat& src, const cv::Mat& M, cv::Size dsize, cv::Mat& dst, int borderType, cv::Scalar borderVal);
140
static const func_t nearest_funcs[] =
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>
150
static const func_t linear_funcs[] =
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>
160
static const func_t cubic_funcs[] =
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>
170
static const func_t* funcs[] = {nearest_funcs, linear_funcs, cubic_funcs};
173
funcs[interpolation][src.depth()](src, M, dsize, dst, borderType, borderVal);
178
funcs[interpolation][src.depth()](src, iM, dsize, dst, borderType, borderVal);
183
///////////////////////////////////////////////////////////////////
186
PARAM_TEST_CASE(WarpPerspective, cv::cuda::DeviceInfo, cv::Size, MatType, Inverse, Interpolation, BorderType, UseRoi)
188
cv::cuda::DeviceInfo devInfo;
198
devInfo = GET_PARAM(0);
201
inverse = GET_PARAM(3);
202
interpolation = GET_PARAM(4);
203
borderType = GET_PARAM(5);
204
useRoi = GET_PARAM(6);
206
cv::cuda::setDevice(devInfo.deviceID());
210
CUDA_TEST_P(WarpPerspective, Accuracy)
212
cv::Mat src = randomMat(size, type);
213
cv::Mat M = createTransfomMatrix(size, CV_PI / 3);
214
int flags = interpolation;
216
flags |= cv::WARP_INVERSE_MAP;
217
cv::Scalar val = randomScalar(0.0, 255.0);
219
cv::cuda::GpuMat dst = createMat(size, type, useRoi);
220
cv::cuda::warpPerspective(loadMat(src, useRoi), dst, M, size, flags, borderType, val);
223
warpPerspectiveGold(src, M, inverse, size, dst_gold, interpolation, borderType, val);
225
EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-1 : 1.0);
228
INSTANTIATE_TEST_CASE_P(CUDA_Warping, WarpPerspective, testing::Combine(
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)),
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)),
237
///////////////////////////////////////////////////////////////////
240
PARAM_TEST_CASE(WarpPerspectiveNPP, cv::cuda::DeviceInfo, MatType, Inverse, Interpolation)
242
cv::cuda::DeviceInfo devInfo;
249
devInfo = GET_PARAM(0);
251
inverse = GET_PARAM(2);
252
interpolation = GET_PARAM(3);
254
cv::cuda::setDevice(devInfo.deviceID());
258
CUDA_TEST_P(WarpPerspectiveNPP, Accuracy)
260
cv::Mat src = readImageType("stereobp/aloe-L.png", type);
261
ASSERT_FALSE(src.empty());
263
cv::Mat M = createTransfomMatrix(src.size(), CV_PI / 4);
264
int flags = interpolation;
266
flags |= cv::WARP_INVERSE_MAP;
268
cv::cuda::GpuMat dst;
269
cv::cuda::warpPerspective(loadMat(src), dst, M, src.size(), flags);
272
warpPerspectiveGold(src, M, inverse, src.size(), dst_gold, interpolation, cv::BORDER_CONSTANT, cv::Scalar::all(0));
274
EXPECT_MAT_SIMILAR(dst_gold, dst, 2e-2);
277
INSTANTIATE_TEST_CASE_P(CUDA_Warping, WarpPerspectiveNPP, testing::Combine(
279
testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
281
testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC))));