test_reduction.cu 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. TEST(Sum, GpuMat)
  49. {
  50. const Size size = randomSize(100, 400);
  51. Mat src = randomMat(size, CV_8UC1);
  52. GpuMat_<uchar> d_src(src);
  53. GpuMat_<float> dst = sum_(d_src);
  54. float res;
  55. dst.download(_OutputArray(&res, 1));
  56. Scalar dst_gold = cv::sum(src);
  57. ASSERT_FLOAT_EQ(static_cast<float>(dst_gold[0]), res);
  58. }
  59. TEST(Sum, Expr)
  60. {
  61. const Size size = randomSize(100, 400);
  62. Mat src1 = randomMat(size, CV_32FC1, 0, 1);
  63. Mat src2 = randomMat(size, CV_32FC1, 0, 1);
  64. GpuMat_<float> d_src1(src1), d_src2(src2);
  65. GpuMat_<float> dst = sum_(abs_(d_src1 - d_src2));
  66. float res;
  67. dst.download(_OutputArray(&res, 1));
  68. Scalar dst_gold = cv::norm(src1, src2, NORM_L1);
  69. ASSERT_FLOAT_EQ(static_cast<float>(dst_gold[0]), res);
  70. }
  71. TEST(MinVal, GpuMat)
  72. {
  73. const Size size = randomSize(100, 400);
  74. Mat src = randomMat(size, CV_8UC1);
  75. GpuMat_<uchar> d_src(src);
  76. GpuMat_<float> dst = minVal_(d_src);
  77. float res;
  78. dst.download(_OutputArray(&res, 1));
  79. double res_gold;
  80. cv::minMaxLoc(src, &res_gold, 0);
  81. ASSERT_FLOAT_EQ(static_cast<float>(res_gold), res);
  82. }
  83. TEST(MaxVal, Expr)
  84. {
  85. const Size size = randomSize(100, 400);
  86. Mat src1 = randomMat(size, CV_32SC1);
  87. Mat src2 = randomMat(size, CV_32SC1);
  88. GpuMat_<int> d_src1(src1), d_src2(src2);
  89. GpuMat_<float> dst = maxVal_(abs_(d_src1 - d_src2));
  90. float res;
  91. dst.download(_OutputArray(&res, 1));
  92. double res_gold = cv::norm(src1, src2, NORM_INF);
  93. ASSERT_FLOAT_EQ(static_cast<float>(res_gold), res);
  94. }
  95. TEST(MinMaxVal, GpuMat)
  96. {
  97. const Size size = randomSize(100, 400);
  98. Mat src = randomMat(size, CV_8UC1);
  99. GpuMat_<uchar> d_src(src);
  100. GpuMat_<float> dst = minMaxVal_(d_src);
  101. float res[2];
  102. dst.download(Mat(1, 2, CV_32FC1, res));
  103. double res_gold[2];
  104. cv::minMaxLoc(src, &res_gold[0], &res_gold[1]);
  105. ASSERT_FLOAT_EQ(static_cast<float>(res_gold[0]), res[0]);
  106. ASSERT_FLOAT_EQ(static_cast<float>(res_gold[1]), res[1]);
  107. }
  108. TEST(NonZeroCount, Accuracy)
  109. {
  110. const Size size = randomSize(100, 400);
  111. Mat src = randomMat(size, CV_8UC1, 0, 5);
  112. GpuMat_<uchar> d_src(src);
  113. GpuMat_<int> dst1 = countNonZero_(d_src);
  114. GpuMat_<int> dst2 = sum_(cvt_<int>(d_src) != 0);
  115. EXPECT_MAT_NEAR(dst1, dst2, 0.0);
  116. }
  117. TEST(ReduceToRow, Sum)
  118. {
  119. const Size size = randomSize(100, 400);
  120. Mat src = randomMat(size, CV_8UC1);
  121. GpuMat_<uchar> d_src(src);
  122. GpuMat_<int> dst = reduceToRow_<Sum<int> >(d_src);
  123. Mat dst_gold;
  124. cv::reduce(src, dst_gold, 0, REDUCE_SUM, CV_32S);
  125. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  126. }
  127. TEST(ReduceToRow, Avg)
  128. {
  129. const Size size = randomSize(100, 400);
  130. Mat src = randomMat(size, CV_8UC1);
  131. GpuMat_<uchar> d_src(src);
  132. GpuMat_<float> dst = reduceToRow_<Avg<float> >(d_src);
  133. Mat dst_gold;
  134. cv::reduce(src, dst_gold, 0, REDUCE_AVG, CV_32F);
  135. EXPECT_MAT_NEAR(dst_gold, dst, 1e-4);
  136. }
  137. TEST(ReduceToRow, Min)
  138. {
  139. const Size size = randomSize(100, 400);
  140. Mat src = randomMat(size, CV_8UC1);
  141. GpuMat_<uchar> d_src(src);
  142. GpuMat_<uchar> dst = reduceToRow_<Min<uchar> >(d_src);
  143. Mat dst_gold;
  144. cv::reduce(src, dst_gold, 0, REDUCE_MIN);
  145. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  146. }
  147. TEST(ReduceToRow, Max)
  148. {
  149. const Size size = randomSize(100, 400);
  150. Mat src = randomMat(size, CV_8UC1);
  151. GpuMat_<uchar> d_src(src);
  152. GpuMat_<uchar> dst = reduceToRow_<Max<uchar> >(d_src);
  153. Mat dst_gold;
  154. cv::reduce(src, dst_gold, 0, REDUCE_MAX);
  155. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  156. }
  157. TEST(ReduceToColumn, Sum)
  158. {
  159. const Size size = randomSize(100, 400);
  160. Mat src = randomMat(size, CV_8UC1);
  161. GpuMat_<uchar> d_src(src);
  162. GpuMat_<int> dst = reduceToColumn_<Sum<int> >(d_src);
  163. Mat dst_gold;
  164. cv::reduce(src, dst_gold, 1, REDUCE_SUM, CV_32S);
  165. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  166. }
  167. TEST(ReduceToColumn, Avg)
  168. {
  169. const Size size = randomSize(100, 400);
  170. Mat src = randomMat(size, CV_8UC1);
  171. GpuMat_<uchar> d_src(src);
  172. GpuMat_<float> dst = reduceToColumn_<Avg<float> >(d_src);
  173. Mat dst_gold;
  174. cv::reduce(src, dst_gold, 1, REDUCE_AVG, CV_32F);
  175. EXPECT_MAT_NEAR(dst_gold, dst, 1e-4);
  176. }
  177. TEST(ReduceToColumn, Min)
  178. {
  179. const Size size = randomSize(100, 400);
  180. Mat src = randomMat(size, CV_8UC1);
  181. GpuMat_<uchar> d_src(src);
  182. GpuMat_<uchar> dst = reduceToColumn_<Min<uchar> >(d_src);
  183. Mat dst_gold;
  184. cv::reduce(src, dst_gold, 1, REDUCE_MIN);
  185. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  186. }
  187. TEST(ReduceToColumn, Max)
  188. {
  189. const Size size = randomSize(100, 400);
  190. Mat src = randomMat(size, CV_8UC1);
  191. GpuMat_<uchar> d_src(src);
  192. GpuMat_<uchar> dst = reduceToColumn_<Max<uchar> >(d_src);
  193. Mat dst_gold;
  194. cv::reduce(src, dst_gold, 1, REDUCE_MAX);
  195. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  196. }
  197. static void calcHistGold(const cv::Mat& src, cv::Mat& hist)
  198. {
  199. hist.create(1, 256, CV_32SC1);
  200. hist.setTo(cv::Scalar::all(0));
  201. int* hist_row = hist.ptr<int>();
  202. for (int y = 0; y < src.rows; ++y)
  203. {
  204. const uchar* src_row = src.ptr(y);
  205. for (int x = 0; x < src.cols; ++x)
  206. ++hist_row[src_row[x]];
  207. }
  208. }
  209. TEST(Histogram, GpuMat)
  210. {
  211. const Size size = randomSize(100, 400);
  212. Mat src = randomMat(size, CV_8UC1);
  213. GpuMat_<uchar> d_src(src);
  214. GpuMat_<int> dst = histogram_<256>(d_src);
  215. Mat dst_gold;
  216. calcHistGold(src, dst_gold);
  217. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  218. }