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 // Copyright (C) 2013, OpenCV Foundation, all rights reserved. 16 // Third party copyrights are property of their respective owners. 17 // 18 // Redistribution and use in source and binary forms, with or without modification, 19 // are permitted provided that the following conditions are met: 20 // 21 // * Redistribution's of source code must retain the above copyright notice, 22 // this list of conditions and the following disclaimer. 23 // 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. 27 // 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. 30 // 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. 41 // 42 //M*/ 43 44 #include "test_precomp.hpp" 45 46 using namespace cv; 47 using namespace cv::cuda; 48 using namespace cv::cudev; 49 using namespace cvtest; 50 51 // remap 52 53 enum { HALF_SIZE=0, UPSIDE_DOWN, REFLECTION_X, REFLECTION_BOTH }; 54 55 static void generateMap(Mat& mapx, Mat& mapy, int remapMode) 56 { 57 for (int j = 0; j < mapx.rows; ++j) 58 { 59 for (int i = 0; i < mapx.cols; ++i) 60 { 61 switch (remapMode) 62 { 63 case HALF_SIZE: 64 if (i > mapx.cols*0.25 && i < mapx.cols*0.75 && j > mapx.rows*0.25 && j < mapx.rows*0.75) 65 { 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; 68 } 69 else 70 { 71 mapx.at<float>(j,i) = 0.f; 72 mapy.at<float>(j,i) = 0.f; 73 } 74 break; 75 case UPSIDE_DOWN: 76 mapx.at<float>(j,i) = static_cast<float>(i); 77 mapy.at<float>(j,i) = static_cast<float>(mapx.rows - j); 78 break; 79 case REFLECTION_X: 80 mapx.at<float>(j,i) = static_cast<float>(mapx.cols - i); 81 mapy.at<float>(j,i) = static_cast<float>(j); 82 break; 83 case REFLECTION_BOTH: 84 mapx.at<float>(j,i) = static_cast<float>(mapx.cols - i); 85 mapy.at<float>(j,i) = static_cast<float>(mapx.rows - j); 86 break; 87 } // end of switch 88 } 89 } 90 } 91 92 static void test_remap(int remapMode) 93 { 94 const Size size = randomSize(100, 400); 95 96 Mat src = randomMat(size, CV_32FC1, 0, 1); 97 98 Mat mapx(size, CV_32FC1); 99 Mat mapy(size, CV_32FC1); 100 generateMap(mapx, mapy, remapMode); 101 102 GpuMat_<float> d_src(src); 103 GpuMat_<float> d_mapx(mapx); 104 GpuMat_<float> d_mapy(mapy); 105 106 GpuMat_<float> dst = remap_(interNearest(brdReplicate(d_src)), d_mapx, d_mapy); 107 108 Mat dst_gold; 109 cv::remap(src, dst_gold, mapx, mapy, INTER_NEAREST, BORDER_REPLICATE); 110 111 EXPECT_MAT_NEAR(dst_gold, dst, 0.0); 112 } 113 114 TEST(Remap, HALF_SIZE) 115 { 116 test_remap(HALF_SIZE); 117 } 118 119 TEST(Remap, UPSIDE_DOWN) 120 { 121 test_remap(UPSIDE_DOWN); 122 } 123 124 TEST(Remap, REFLECTION_X) 125 { 126 test_remap(REFLECTION_X); 127 } 128 129 TEST(Remap, REFLECTION_BOTH) 130 { 131 test_remap(REFLECTION_BOTH); 132 } 133 134 // resize 135 136 TEST(Resize, Upscale) 137 { 138 const Size size = randomSize(100, 400); 139 140 Mat src = randomMat(size, CV_32FC1, 0, 1); 141 142 GpuMat_<float> d_src(src); 143 Texture<float> tex_src(d_src); 144 145 GpuMat_<float> dst1 = resize_(interCubic(tex_src), 2, 2); 146 147 Mat mapx(size.height * 2, size.width * 2, CV_32FC1); 148 Mat mapy(size.height * 2, size.width * 2, CV_32FC1); 149 150 for (int y = 0; y < mapx.rows; ++y) 151 { 152 for (int x = 0; x < mapx.cols; ++x) 153 { 154 mapx.at<float>(y, x) = static_cast<float>(x / 2); 155 mapy.at<float>(y, x) = static_cast<float>(y / 2); 156 } 157 } 158 159 GpuMat_<float> d_mapx(mapx); 160 GpuMat_<float> d_mapy(mapy); 161 162 GpuMat_<float> dst2 = remap_(interCubic(brdReplicate(d_src)), d_mapx, d_mapy); 163 164 EXPECT_MAT_NEAR(dst1, dst2, 0.0); 165 } 166 167 TEST(Resize, Downscale) 168 { 169 const Size size = randomSize(100, 400); 170 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; 174 175 GpuMat_<float> d_src(src); 176 Texture<float> tex_src(d_src); 177 178 GpuMat_<float> dst1 = resize_(interArea(tex_src, Size(3, 3)), fx, fy); 179 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); 182 183 for (int y = 0; y < mapx.rows; ++y) 184 { 185 for (int x = 0; x < mapx.cols; ++x) 186 { 187 mapx.at<float>(y, x) = x / fx; 188 mapy.at<float>(y, x) = y / fy; 189 } 190 } 191 192 GpuMat_<float> d_mapx(mapx); 193 GpuMat_<float> d_mapy(mapy); 194 195 GpuMat_<float> dst2 = remap_(interArea(brdReplicate(d_src), Size(3, 3)), d_mapx, d_mapy); 196 197 EXPECT_MAT_NEAR(dst1, dst2, 0.0); 198 } 199 200 // warpAffine & warpPerspective 201 202 Mat createAffineTransfomMatrix(Size srcSize, float angle, bool perspective) 203 { 204 cv::Mat M(perspective ? 3 : 2, 3, CV_32FC1); 205 206 { 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; 209 } 210 if (perspective) 211 { 212 M.at<float>(2, 0) = 0.0f ; M.at<float>(2, 1) = 0.0f ; M.at<float>(2, 2) = 1.0f; 213 } 214 215 return M; 216 } 217 218 TEST(WarpAffine, Rotation) 219 { 220 const Size size = randomSize(100, 400); 221 222 Mat src = randomMat(size, CV_32FC1, 0, 1); 223 Mat M = createAffineTransfomMatrix(size, static_cast<float>(CV_PI / 4), false); 224 225 GpuMat_<float> d_src(src); 226 GpuMat_<float> d_M; 227 createContinuous(M.size(), M.type(), d_M); 228 d_M.upload(M); 229 230 GpuMat_<float> dst = warpAffine_(interNearest(brdConstant(d_src)), size, d_M); 231 232 Mat dst_gold; 233 cv::warpAffine(src, dst_gold, M, size, INTER_NEAREST | WARP_INVERSE_MAP); 234 235 EXPECT_MAT_SIMILAR(dst_gold, dst, 1e-3); 236 } 237 238 TEST(WarpPerspective, Rotation) 239 { 240 const Size size = randomSize(100, 400); 241 242 Mat src = randomMat(size, CV_32FC1, 0, 1); 243 Mat M = createAffineTransfomMatrix(size, static_cast<float>(CV_PI / 4), true); 244 245 GpuMat_<float> d_src(src); 246 GpuMat_<float> d_M; 247 createContinuous(M.size(), M.type(), d_M); 248 d_M.upload(M); 249 250 GpuMat_<float> dst = warpPerspective_(interNearest(brdConstant(d_src)), size, d_M); 251 252 Mat dst_gold; 253 cv::warpPerspective(src, dst_gold, M, size, INTER_NEAREST | WARP_INVERSE_MAP); 254 255 EXPECT_MAT_SIMILAR(dst_gold, dst, 1e-3); 256 } 257