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
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16
// Third party copyrights are property of their respective owners.
18
// Redistribution and use in source and binary forms, with or without modification,
19
// are permitted provided that the following conditions are met:
21
// * Redistribution's of source code must retain the above copyright notice,
22
// this list of conditions and the following disclaimer.
24
// * Redistribution's in binary form must reproduce the above copyright notice,
25
// this list of conditions and the following disclaimer in the documentation
26
// and/or other materials provided with the distribution.
28
// * The name of the copyright holders may not be used to endorse or promote products
29
// derived from this software without specific prior written permission.
31
// This software is provided by the copyright holders and contributors "as is" and
32
// any express or implied warranties, including, but not limited to, the implied
33
// warranties of merchantability and fitness for a particular purpose are disclaimed.
34
// In no event shall the Intel Corporation or contributors be liable for any direct,
35
// indirect, incidental, special, exemplary, or consequential damages
36
// (including, but not limited to, procurement of substitute goods or services;
37
// loss of use, data, or profits; or business interruption) however caused
38
// and on any theory of liability, whether in contract, strict liability,
39
// or tort (including negligence or otherwise) arising in any way out of
40
// the use of this software, even if advised of the possibility of such damage.
44
#include "test_precomp.hpp"
47
using namespace cv::cuda;
48
using namespace cv::cudev;
49
using namespace cvtest;
53
enum { HALF_SIZE=0, UPSIDE_DOWN, REFLECTION_X, REFLECTION_BOTH };
55
static void generateMap(Mat& mapx, Mat& mapy, int remapMode)
57
for (int j = 0; j < mapx.rows; ++j)
59
for (int i = 0; i < mapx.cols; ++i)
64
if (i > mapx.cols*0.25 && i < mapx.cols*0.75 && j > mapx.rows*0.25 && j < mapx.rows*0.75)
66
mapx.at<float>(j,i) = 2.f * (i - mapx.cols * 0.25f) + 0.5f;
67
mapy.at<float>(j,i) = 2.f * (j - mapx.rows * 0.25f) + 0.5f;
71
mapx.at<float>(j,i) = 0.f;
72
mapy.at<float>(j,i) = 0.f;
76
mapx.at<float>(j,i) = static_cast<float>(i);
77
mapy.at<float>(j,i) = static_cast<float>(mapx.rows - j);
80
mapx.at<float>(j,i) = static_cast<float>(mapx.cols - i);
81
mapy.at<float>(j,i) = static_cast<float>(j);
84
mapx.at<float>(j,i) = static_cast<float>(mapx.cols - i);
85
mapy.at<float>(j,i) = static_cast<float>(mapx.rows - j);
92
static void test_remap(int remapMode)
94
const Size size = randomSize(100, 400);
96
Mat src = randomMat(size, CV_32FC1, 0, 1);
98
Mat mapx(size, CV_32FC1);
99
Mat mapy(size, CV_32FC1);
100
generateMap(mapx, mapy, remapMode);
102
GpuMat_<float> d_src(src);
103
GpuMat_<float> d_mapx(mapx);
104
GpuMat_<float> d_mapy(mapy);
106
GpuMat_<float> dst = remap_(interNearest(brdReplicate(d_src)), d_mapx, d_mapy);
109
cv::remap(src, dst_gold, mapx, mapy, INTER_NEAREST, BORDER_REPLICATE);
111
EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
114
TEST(Remap, HALF_SIZE)
116
test_remap(HALF_SIZE);
119
TEST(Remap, UPSIDE_DOWN)
121
test_remap(UPSIDE_DOWN);
124
TEST(Remap, REFLECTION_X)
126
test_remap(REFLECTION_X);
129
TEST(Remap, REFLECTION_BOTH)
131
test_remap(REFLECTION_BOTH);
136
TEST(Resize, Upscale)
138
const Size size = randomSize(100, 400);
140
Mat src = randomMat(size, CV_32FC1, 0, 1);
142
GpuMat_<float> d_src(src);
143
Texture<float> tex_src(d_src);
145
GpuMat_<float> dst1 = resize_(interCubic(tex_src), 2, 2);
147
Mat mapx(size.height * 2, size.width * 2, CV_32FC1);
148
Mat mapy(size.height * 2, size.width * 2, CV_32FC1);
150
for (int y = 0; y < mapx.rows; ++y)
152
for (int x = 0; x < mapx.cols; ++x)
154
mapx.at<float>(y, x) = static_cast<float>(x / 2);
155
mapy.at<float>(y, x) = static_cast<float>(y / 2);
159
GpuMat_<float> d_mapx(mapx);
160
GpuMat_<float> d_mapy(mapy);
162
GpuMat_<float> dst2 = remap_(interCubic(brdReplicate(d_src)), d_mapx, d_mapy);
164
EXPECT_MAT_NEAR(dst1, dst2, 0.0);
167
TEST(Resize, Downscale)
169
const Size size = randomSize(100, 400);
171
Mat src = randomMat(size, CV_32FC1, 0, 1);
172
const float fx = 1.0f / 3.0f;
173
const float fy = 1.0f / 3.0f;
175
GpuMat_<float> d_src(src);
176
Texture<float> tex_src(d_src);
178
GpuMat_<float> dst1 = resize_(interArea(tex_src, Size(3, 3)), fx, fy);
180
Mat mapx(cv::saturate_cast<int>(size.height * fy), cv::saturate_cast<int>(size.width * fx), CV_32FC1);
181
Mat mapy(cv::saturate_cast<int>(size.height * fy), cv::saturate_cast<int>(size.width * fx), CV_32FC1);
183
for (int y = 0; y < mapx.rows; ++y)
185
for (int x = 0; x < mapx.cols; ++x)
187
mapx.at<float>(y, x) = x / fx;
188
mapy.at<float>(y, x) = y / fy;
192
GpuMat_<float> d_mapx(mapx);
193
GpuMat_<float> d_mapy(mapy);
195
GpuMat_<float> dst2 = remap_(interArea(brdReplicate(d_src), Size(3, 3)), d_mapx, d_mapy);
197
EXPECT_MAT_NEAR(dst1, dst2, 0.0);
200
// warpAffine & warpPerspective
202
Mat createAffineTransfomMatrix(Size srcSize, float angle, bool perspective)
204
cv::Mat M(perspective ? 3 : 2, 3, CV_32FC1);
207
M.at<float>(0, 0) = std::cos(angle); M.at<float>(0, 1) = -std::sin(angle); M.at<float>(0, 2) = static_cast<float>(srcSize.width / 2);
208
M.at<float>(1, 0) = std::sin(angle); M.at<float>(1, 1) = std::cos(angle); M.at<float>(1, 2) = 0.0f;
212
M.at<float>(2, 0) = 0.0f ; M.at<float>(2, 1) = 0.0f ; M.at<float>(2, 2) = 1.0f;
218
TEST(WarpAffine, Rotation)
220
const Size size = randomSize(100, 400);
222
Mat src = randomMat(size, CV_32FC1, 0, 1);
223
Mat M = createAffineTransfomMatrix(size, static_cast<float>(CV_PI / 4), false);
225
GpuMat_<float> d_src(src);
227
createContinuous(M.size(), M.type(), d_M);
230
GpuMat_<float> dst = warpAffine_(interNearest(brdConstant(d_src)), size, d_M);
233
cv::warpAffine(src, dst_gold, M, size, INTER_NEAREST | WARP_INVERSE_MAP);
235
EXPECT_MAT_SIMILAR(dst_gold, dst, 1e-3);
238
TEST(WarpPerspective, Rotation)
240
const Size size = randomSize(100, 400);
242
Mat src = randomMat(size, CV_32FC1, 0, 1);
243
Mat M = createAffineTransfomMatrix(size, static_cast<float>(CV_PI / 4), true);
245
GpuMat_<float> d_src(src);
247
createContinuous(M.size(), M.type(), d_M);
250
GpuMat_<float> dst = warpPerspective_(interNearest(brdConstant(d_src)), size, d_M);
253
cv::warpPerspective(src, dst_gold, M, size, INTER_NEAREST | WARP_INVERSE_MAP);
255
EXPECT_MAT_SIMILAR(dst_gold, dst, 1e-3);