test_arithm_func.cu 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. ////////////////////////////////////////////////////////////////////////////////
  49. // SqrtTest
  50. template <typename T>
  51. class SqrtTest : public ::testing::Test
  52. {
  53. public:
  54. void test_gpumat()
  55. {
  56. const Size size = randomSize(100, 400);
  57. const int type = DataType<T>::type;
  58. Mat src = randomMat(size, type);
  59. GpuMat_<T> d_src(src);
  60. GpuMat_<T> dst = sqrt_(d_src);
  61. Mat dst_gold;
  62. cv::sqrt(src, dst_gold);
  63. EXPECT_MAT_NEAR(dst_gold, dst, 1e-4);
  64. }
  65. void test_expr()
  66. {
  67. const Size size = randomSize(100, 400);
  68. const int type = DataType<T>::type;
  69. Mat src1 = randomMat(size, type);
  70. Mat src2 = randomMat(size, type);
  71. GpuMat_<T> d_src1(src1), d_src2(src2);
  72. GpuMat_<T> dst = sqrt_(d_src1 * d_src2);
  73. Mat dst_gold;
  74. cv::multiply(src1, src2, dst_gold);
  75. cv::sqrt(dst_gold, dst_gold);
  76. EXPECT_MAT_NEAR(dst_gold, dst, 1e-4);
  77. }
  78. };
  79. TYPED_TEST_CASE(SqrtTest, float);
  80. TYPED_TEST(SqrtTest, GpuMat)
  81. {
  82. SqrtTest<TypeParam>::test_gpumat();
  83. }
  84. TYPED_TEST(SqrtTest, Expr)
  85. {
  86. SqrtTest<TypeParam>::test_expr();
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////
  89. // MagnitudeTest
  90. template <typename T>
  91. class MagnitudeTest : public ::testing::Test
  92. {
  93. public:
  94. void test_accuracy()
  95. {
  96. const Size size = randomSize(100, 400);
  97. const int type = DataType<T>::type;
  98. Mat src1 = randomMat(size, type);
  99. Mat src2 = randomMat(size, type);
  100. GpuMat_<T> d_src1(src1), d_src2(src2);
  101. GpuMat_<T> dst1 = hypot_(d_src1, d_src2);
  102. GpuMat_<T> dst2 = magnitude_(d_src1, d_src2);
  103. GpuMat_<T> dst3 = sqrt_(sqr_(d_src1) + sqr_(d_src2));
  104. EXPECT_MAT_NEAR(dst1, dst2, 1e-4);
  105. EXPECT_MAT_NEAR(dst2, dst3, 0.0);
  106. }
  107. };
  108. TYPED_TEST_CASE(MagnitudeTest, float);
  109. TYPED_TEST(MagnitudeTest, Accuracy)
  110. {
  111. MagnitudeTest<TypeParam>::test_accuracy();
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////
  114. // PowTest
  115. template <typename T>
  116. class PowTest : public ::testing::Test
  117. {
  118. public:
  119. void test_accuracy()
  120. {
  121. const Size size = randomSize(100, 400);
  122. const int type = DataType<T>::type;
  123. Mat src = randomMat(size, type);
  124. GpuMat_<T> d_src(src);
  125. GpuMat_<T> dst1 = pow_(d_src, 0.5);
  126. GpuMat_<T> dst2 = sqrt_(d_src);
  127. EXPECT_MAT_NEAR(dst1, dst2, 1e-5);
  128. }
  129. };
  130. TYPED_TEST_CASE(PowTest, float);
  131. TYPED_TEST(PowTest, Accuracy)
  132. {
  133. PowTest<TypeParam>::test_accuracy();
  134. }