test_arithm_op.cu 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #include "test_precomp.hpp"
  44. using namespace cv;
  45. using namespace cv::cuda;
  46. using namespace cv::cudev;
  47. using namespace cvtest;
  48. typedef ::testing::Types<uchar, ushort, short, int, float> AllTypes;
  49. typedef ::testing::Types<short, int, float> SignedTypes;
  50. ////////////////////////////////////////////////////////////////////////////////
  51. // UnaryMinusTest
  52. template <typename T>
  53. class UnaryMinusTest : public ::testing::Test
  54. {
  55. public:
  56. void test_gpumat()
  57. {
  58. const Size size = randomSize(100, 400);
  59. const int type = DataType<T>::type;
  60. Mat src = randomMat(size, type);
  61. GpuMat_<T> d_src(src);
  62. GpuMat_<T> dst = -d_src;
  63. Mat dst_gold;
  64. src.convertTo(dst_gold, src.depth(), -1);
  65. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  66. }
  67. void test_globptr()
  68. {
  69. const Size size = randomSize(100, 400);
  70. const int type = DataType<T>::type;
  71. Mat src = randomMat(size, type);
  72. GpuMat_<T> d_src(src);
  73. GlobPtrSz<T> d_src_ptr = d_src;
  74. GpuMat_<T> dst = -d_src_ptr;
  75. Mat dst_gold;
  76. src.convertTo(dst_gold, src.depth(), -1);
  77. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  78. }
  79. void test_texptr()
  80. {
  81. const Size size = randomSize(100, 400);
  82. const int type = DataType<T>::type;
  83. Mat src = randomMat(size, type);
  84. GpuMat_<T> d_src(src);
  85. Texture<T> tex_src(d_src);
  86. GpuMat_<T> dst = -tex_src;
  87. Mat dst_gold;
  88. src.convertTo(dst_gold, src.depth(), -1);
  89. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  90. }
  91. void test_expr()
  92. {
  93. const Size size = randomSize(100, 400);
  94. const int type = DataType<T>::type;
  95. Mat src1 = randomMat(size, type);
  96. Mat src2 = randomMat(size, type);
  97. GpuMat_<T> d_src1(src1), d_src2(src2);
  98. GpuMat_<T> dst = -(d_src1 + d_src2);
  99. Mat dst_gold;
  100. cv::add(src1, src2, dst_gold);
  101. dst_gold.convertTo(dst_gold, dst_gold.depth(), -1);
  102. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  103. }
  104. };
  105. TYPED_TEST_CASE(UnaryMinusTest, SignedTypes);
  106. TYPED_TEST(UnaryMinusTest, GpuMat)
  107. {
  108. UnaryMinusTest<TypeParam>::test_gpumat();
  109. }
  110. TYPED_TEST(UnaryMinusTest, GlobPtrSz)
  111. {
  112. UnaryMinusTest<TypeParam>::test_globptr();
  113. }
  114. TYPED_TEST(UnaryMinusTest, TexturePtr)
  115. {
  116. UnaryMinusTest<TypeParam>::test_texptr();
  117. }
  118. TYPED_TEST(UnaryMinusTest, Expr)
  119. {
  120. UnaryMinusTest<TypeParam>::test_expr();
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////
  123. // PlusTest
  124. template <typename T>
  125. class PlusTest : public ::testing::Test
  126. {
  127. public:
  128. void test_gpumat_gpumat()
  129. {
  130. const Size size = randomSize(100, 400);
  131. const int type = DataType<T>::type;
  132. Mat src1 = randomMat(size, type);
  133. Mat src2 = randomMat(size, type);
  134. GpuMat_<T> d_src1(src1), d_src2(src2);
  135. GpuMat_<T> dst = d_src1 + d_src2;
  136. Mat dst_gold;
  137. cv::add(src1, src2, dst_gold);
  138. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  139. }
  140. void test_texptr_scalar()
  141. {
  142. const Size size = randomSize(100, 400);
  143. const int type = DataType<T>::type;
  144. Mat src = randomMat(size, type);
  145. GpuMat_<T> d_src(src);
  146. Texture<T> tex_src(d_src);
  147. GpuMat_<T> dst = tex_src + static_cast<T>(5);
  148. Mat dst_gold;
  149. cv::add(src, 5, dst_gold);
  150. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  151. }
  152. void test_expr_gpumat()
  153. {
  154. const Size size = randomSize(100, 400);
  155. const int type = DataType<T>::type;
  156. Mat src1 = randomMat(size, type);
  157. Mat src2 = randomMat(size, type);
  158. Mat src3 = randomMat(size, type);
  159. GpuMat_<T> d_src1(src1), d_src2(src2), d_src3(src3);
  160. GpuMat_<T> dst = d_src1 + d_src2 + d_src3;
  161. Mat dst_gold;
  162. cv::add(src1, src2, dst_gold);
  163. cv::add(dst_gold, src3, dst_gold);
  164. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  165. }
  166. void test_scalar_expr()
  167. {
  168. const Size size = randomSize(100, 400);
  169. const int type = DataType<T>::type;
  170. Mat src1 = randomMat(size, type);
  171. Mat src2 = randomMat(size, type);
  172. GpuMat_<T> d_src1(src1), d_src2(src2);
  173. GpuMat_<T> dst = static_cast<T>(5) + (d_src1 + d_src2);
  174. Mat dst_gold;
  175. cv::add(src1, src2, dst_gold);
  176. cv::add(dst_gold, 5, dst_gold);
  177. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  178. }
  179. };
  180. TYPED_TEST_CASE(PlusTest, AllTypes);
  181. TYPED_TEST(PlusTest, GpuMat_GpuMat)
  182. {
  183. PlusTest<TypeParam>::test_gpumat_gpumat();
  184. }
  185. TYPED_TEST(PlusTest, TexturePtr_Scalar)
  186. {
  187. PlusTest<TypeParam>::test_texptr_scalar();
  188. }
  189. TYPED_TEST(PlusTest, Expr_GpuMat)
  190. {
  191. PlusTest<TypeParam>::test_expr_gpumat();
  192. }
  193. TYPED_TEST(PlusTest, Scalar_Expr)
  194. {
  195. PlusTest<TypeParam>::test_scalar_expr();
  196. }
  197. ////////////////////////////////////////////////////////////////////////////////
  198. // MinusTest
  199. template <typename T>
  200. class MinusTest : public ::testing::Test
  201. {
  202. public:
  203. void test_gpumat_gpumat()
  204. {
  205. const Size size = randomSize(100, 400);
  206. const int type = DataType<T>::type;
  207. Mat src1 = randomMat(size, type);
  208. Mat src2 = randomMat(size, type);
  209. GpuMat_<T> d_src1(src1), d_src2(src2);
  210. GpuMat_<T> dst = d_src1 - d_src2;
  211. Mat dst_gold;
  212. cv::subtract(src1, src2, dst_gold);
  213. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  214. }
  215. void test_texptr_scalar()
  216. {
  217. const Size size = randomSize(100, 400);
  218. const int type = DataType<T>::type;
  219. Mat src = randomMat(size, type);
  220. GpuMat_<T> d_src(src);
  221. Texture<T> tex_src(d_src);
  222. GpuMat_<T> dst = tex_src - static_cast<T>(5);
  223. Mat dst_gold;
  224. cv::subtract(src, 5, dst_gold);
  225. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  226. }
  227. void test_expr_gpumat()
  228. {
  229. const Size size = randomSize(100, 400);
  230. const int type = DataType<T>::type;
  231. Mat src1 = randomMat(size, type);
  232. Mat src2 = randomMat(size, type);
  233. Mat src3 = randomMat(size, type);
  234. GpuMat_<T> d_src1(src1), d_src2(src2), d_src3(src3);
  235. GpuMat_<T> dst = (d_src1 + d_src2) - d_src3;
  236. Mat dst_gold;
  237. cv::add(src1, src2, dst_gold);
  238. cv::subtract(dst_gold, src3, dst_gold);
  239. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  240. }
  241. void test_scalar_expr()
  242. {
  243. const Size size = randomSize(100, 400);
  244. const int type = DataType<T>::type;
  245. Mat src1 = randomMat(size, type);
  246. Mat src2 = randomMat(size, type);
  247. GpuMat_<T> d_src1(src1), d_src2(src2);
  248. GpuMat_<T> dst = static_cast<T>(5) - (d_src1 + d_src2);
  249. Mat dst_gold;
  250. cv::add(src1, src2, dst_gold);
  251. cv::subtract(5, dst_gold, dst_gold);
  252. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  253. }
  254. };
  255. TYPED_TEST_CASE(MinusTest, SignedTypes);
  256. TYPED_TEST(MinusTest, GpuMat_GpuMat)
  257. {
  258. MinusTest<TypeParam>::test_gpumat_gpumat();
  259. }
  260. TYPED_TEST(MinusTest, TexturePtr_Scalar)
  261. {
  262. MinusTest<TypeParam>::test_texptr_scalar();
  263. }
  264. TYPED_TEST(MinusTest, Expr_GpuMat)
  265. {
  266. MinusTest<TypeParam>::test_expr_gpumat();
  267. }
  268. TYPED_TEST(MinusTest, Scalar_Expr)
  269. {
  270. MinusTest<TypeParam>::test_scalar_expr();
  271. }
  272. ////////////////////////////////////////////////////////////////////////////////
  273. // AbsDiffTest
  274. template <typename T>
  275. class AbsDiffTest : public ::testing::Test
  276. {
  277. public:
  278. void test_accuracy()
  279. {
  280. const Size size = randomSize(100, 400);
  281. const int type = DataType<T>::type;
  282. Mat src1 = randomMat(size, type);
  283. Mat src2 = randomMat(size, type);
  284. GpuMat_<T> d_src1(src1), d_src2(src2);
  285. GpuMat_<T> dst1 = absdiff_(d_src1, d_src2);
  286. GpuMat_<T> dst2 = abs_(d_src1 - d_src2);
  287. EXPECT_MAT_NEAR(dst1, dst2, 0.0);
  288. }
  289. };
  290. TYPED_TEST_CASE(AbsDiffTest, SignedTypes);
  291. TYPED_TEST(AbsDiffTest, Accuracy)
  292. {
  293. AbsDiffTest<TypeParam>::test_accuracy();
  294. }