test_countnonzero.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. #define CORE_COUNTNONZERO_ERROR_COUNT 1
  45. #define MESSAGE_ERROR_COUNT "Count non zero elements returned by OpenCV function is incorrect."
  46. #define sign(a) a > 0 ? 1 : a == 0 ? 0 : -1
  47. #define MAX_WIDTH 100
  48. #define MAX_HEIGHT 100
  49. class CV_CountNonZeroTest: public cvtest::BaseTest
  50. {
  51. public:
  52. CV_CountNonZeroTest();
  53. ~CV_CountNonZeroTest();
  54. protected:
  55. void run (int);
  56. private:
  57. float eps_32;
  58. double eps_64;
  59. Mat src;
  60. int current_type;
  61. void generate_src_data(cv::Size size, int type);
  62. void generate_src_data(cv::Size size, int type, int count_non_zero);
  63. void generate_src_stat_data(cv::Size size, int type, int distribution);
  64. int get_count_non_zero();
  65. void print_information(int right, int result);
  66. };
  67. CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(std::numeric_limits<float>::min()), eps_64(std::numeric_limits<double>::min()), src(Mat()), current_type(-1) {}
  68. CV_CountNonZeroTest::~CV_CountNonZeroTest() {}
  69. void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type)
  70. {
  71. src.create(size, CV_MAKETYPE(type, 1));
  72. for (int j = 0; j < size.width; ++j)
  73. for (int i = 0; i < size.height; ++i)
  74. switch (type)
  75. {
  76. case CV_8U: { src.at<uchar>(i, j) = cv::randu<uchar>(); break; }
  77. case CV_8S: { src.at<char>(i, j) = cv::randu<uchar>() - 128; break; }
  78. case CV_16U: { src.at<ushort>(i, j) = cv::randu<ushort>(); break; }
  79. case CV_16S: { src.at<short>(i, j) = cv::randu<short>(); break; }
  80. case CV_32S: { src.at<int>(i, j) = cv::randu<int>(); break; }
  81. case CV_32F: { src.at<float>(i, j) = cv::randu<float>(); break; }
  82. case CV_64F: { src.at<double>(i, j) = cv::randu<double>(); break; }
  83. default: break;
  84. }
  85. }
  86. void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int count_non_zero)
  87. {
  88. src = Mat::zeros(size, CV_MAKETYPE(type, 1));
  89. int n = 0; RNG& rng = ts->get_rng();
  90. while (n < count_non_zero)
  91. {
  92. int i = rng.next()%size.height, j = rng.next()%size.width;
  93. switch (type)
  94. {
  95. case CV_8U: { if (!src.at<uchar>(i, j)) {src.at<uchar>(i, j) = cv::randu<uchar>(); n += (src.at<uchar>(i, j) > 0);} break; }
  96. case CV_8S: { if (!src.at<char>(i, j)) {src.at<char>(i, j) = cv::randu<uchar>() - 128; n += abs(sign(src.at<char>(i, j)));} break; }
  97. case CV_16U: { if (!src.at<ushort>(i, j)) {src.at<ushort>(i, j) = cv::randu<ushort>(); n += (src.at<ushort>(i, j) > 0);} break; }
  98. case CV_16S: { if (!src.at<short>(i, j)) {src.at<short>(i, j) = cv::randu<short>(); n += abs(sign(src.at<short>(i, j)));} break; }
  99. case CV_32S: { if (!src.at<int>(i, j)) {src.at<int>(i, j) = cv::randu<int>(); n += abs(sign(src.at<int>(i, j)));} break; }
  100. case CV_32F: { if (fabs(src.at<float>(i, j)) <= eps_32) {src.at<float>(i, j) = cv::randu<float>(); n += (fabs(src.at<float>(i, j)) > eps_32);} break; }
  101. case CV_64F: { if (fabs(src.at<double>(i, j)) <= eps_64) {src.at<double>(i, j) = cv::randu<double>(); n += (fabs(src.at<double>(i, j)) > eps_64);} break; }
  102. default: break;
  103. }
  104. }
  105. }
  106. void CV_CountNonZeroTest::generate_src_stat_data(cv::Size size, int type, int distribution)
  107. {
  108. src.create(size, CV_MAKETYPE(type, 1));
  109. double mean = 0.0, sigma = 1.0;
  110. double left = -1.0, right = 1.0;
  111. RNG& rng = ts->get_rng();
  112. if (distribution == RNG::NORMAL)
  113. rng.fill(src, RNG::NORMAL, Scalar::all(mean), Scalar::all(sigma));
  114. else if (distribution == RNG::UNIFORM)
  115. rng.fill(src, RNG::UNIFORM, Scalar::all(left), Scalar::all(right));
  116. }
  117. int CV_CountNonZeroTest::get_count_non_zero()
  118. {
  119. int result = 0;
  120. for (int i = 0; i < src.rows; ++i)
  121. for (int j = 0; j < src.cols; ++j)
  122. {
  123. if (current_type == CV_8U) result += (src.at<uchar>(i, j) > 0);
  124. else if (current_type == CV_8S) result += abs(sign(src.at<char>(i, j)));
  125. else if (current_type == CV_16U) result += (src.at<ushort>(i, j) > 0);
  126. else if (current_type == CV_16S) result += abs(sign(src.at<short>(i, j)));
  127. else if (current_type == CV_32S) result += abs(sign(src.at<int>(i, j)));
  128. else if (current_type == CV_32F) result += (fabs(src.at<float>(i, j)) > eps_32);
  129. else result += (fabs(src.at<double>(i, j)) > eps_64);
  130. }
  131. return result;
  132. }
  133. void CV_CountNonZeroTest::print_information(int right, int result)
  134. {
  135. cout << endl; cout << "Checking for the work of countNonZero function..." << endl; cout << endl;
  136. cout << "Type of Mat: ";
  137. switch (current_type)
  138. {
  139. case 0: {cout << "CV_8U"; break;}
  140. case 1: {cout << "CV_8S"; break;}
  141. case 2: {cout << "CV_16U"; break;}
  142. case 3: {cout << "CV_16S"; break;}
  143. case 4: {cout << "CV_32S"; break;}
  144. case 5: {cout << "CV_32F"; break;}
  145. case 6: {cout << "CV_64F"; break;}
  146. default: break;
  147. }
  148. cout << endl;
  149. cout << "Number of rows: " << src.rows << " Number of cols: " << src.cols << endl;
  150. cout << "True count non zero elements: " << right << " Result: " << result << endl;
  151. cout << endl;
  152. }
  153. void CV_CountNonZeroTest::run(int)
  154. {
  155. const size_t N = 1500;
  156. for (int k = 1; k <= 3; ++k)
  157. for (size_t i = 0; i < N; ++i)
  158. {
  159. RNG& rng = ts->get_rng();
  160. int w = rng.next()%MAX_WIDTH + 1, h = rng.next()%MAX_HEIGHT + 1;
  161. current_type = rng.next()%7;
  162. switch (k)
  163. {
  164. case 1: {
  165. generate_src_data(Size(w, h), current_type);
  166. int right = get_count_non_zero(), result = countNonZero(src);
  167. if (result != right)
  168. {
  169. cout << "Number of experiment: " << i << endl;
  170. cout << "Method of data generation: RANDOM" << endl;
  171. print_information(right, result);
  172. CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
  173. return;
  174. }
  175. break;
  176. }
  177. case 2: {
  178. int count_non_zero = rng.next()%(w*h);
  179. generate_src_data(Size(w, h), current_type, count_non_zero);
  180. int result = countNonZero(src);
  181. if (result != count_non_zero)
  182. {
  183. cout << "Number of experiment: " << i << endl;
  184. cout << "Method of data generation: HALF-RANDOM" << endl;
  185. print_information(count_non_zero, result);
  186. CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
  187. return;
  188. }
  189. break;
  190. }
  191. case 3: {
  192. int distribution = cv::randu<uchar>()%2;
  193. generate_src_stat_data(Size(w, h), current_type, distribution);
  194. int right = get_count_non_zero(), result = countNonZero(src);
  195. if (right != result)
  196. {
  197. cout << "Number of experiment: " << i << endl;
  198. cout << "Method of data generation: STATISTIC" << endl;
  199. print_information(right, result);
  200. CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
  201. return;
  202. }
  203. break;
  204. }
  205. default: break;
  206. }
  207. }
  208. }
  209. TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); }
  210. typedef testing::TestWithParam<tuple<int, int> > CountNonZeroND;
  211. TEST_P (CountNonZeroND, ndim)
  212. {
  213. const int dims = get<0>(GetParam());
  214. const int type = get<1>(GetParam());
  215. const int ONE_SIZE = 5;
  216. vector<int> sizes(dims);
  217. fill(sizes.begin(), sizes.end(), ONE_SIZE);
  218. Mat data(sizes, CV_MAKETYPE(type, 1));
  219. data = 0;
  220. EXPECT_EQ(0, cv::countNonZero(data));
  221. data = Scalar::all(1);
  222. int expected = static_cast<int>(pow(static_cast<float>(ONE_SIZE), dims));
  223. EXPECT_EQ(expected, cv::countNonZero(data));
  224. }
  225. INSTANTIATE_TEST_CASE_P(Core, CountNonZeroND,
  226. testing::Combine(
  227. testing::Range(2, 9),
  228. testing::Values(CV_8U, CV_8S, CV_32F)
  229. )
  230. );
  231. typedef testing::TestWithParam<tuple<int, cv::Size> > CountNonZeroBig;
  232. TEST_P(CountNonZeroBig, /**/)
  233. {
  234. const int type = get<0>(GetParam());
  235. const Size sz = get<1>(GetParam());
  236. EXPECT_EQ(0, cv::countNonZero(cv::Mat::zeros(sz, type)));
  237. EXPECT_EQ(sz.area(), cv::countNonZero(cv::Mat::ones(sz, type)));
  238. }
  239. INSTANTIATE_TEST_CASE_P(Core, CountNonZeroBig,
  240. testing::Combine(
  241. testing::Values(CV_8UC1, CV_32FC1),
  242. testing::Values(Size(1, 524190), Size(524190, 1), Size(3840, 2160))
  243. )
  244. );
  245. }} // namespace