test_hasnonzero.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. typedef testing::TestWithParam<std::tuple<int, Size> > HasNonZeroAllZeros;
  45. TEST_P(HasNonZeroAllZeros, hasNonZeroAllZeros)
  46. {
  47. const int type = std::get<0>(GetParam());
  48. const Size size = std::get<1>(GetParam());
  49. Mat m = Mat::zeros(size, type);
  50. EXPECT_FALSE(hasNonZero(m));
  51. }
  52. INSTANTIATE_TEST_CASE_P(Core, HasNonZeroAllZeros,
  53. testing::Combine(
  54. testing::Values(CV_8UC1, CV_8SC1, CV_16UC1, CV_16SC1, CV_32SC1, CV_32FC1, CV_64FC1),
  55. testing::Values(Size(1, 1), Size(320, 240), Size(127, 113), Size(1, 113))
  56. )
  57. );
  58. typedef testing::TestWithParam<std::tuple<int, Size> > HasNonZeroNegZeros;
  59. TEST_P(HasNonZeroNegZeros, hasNonZeroNegZeros)
  60. {
  61. const int type = std::get<0>(GetParam());
  62. const Size size = std::get<1>(GetParam());
  63. Mat m = Mat(size, type);
  64. m.setTo(Scalar::all(-0.));
  65. EXPECT_FALSE(hasNonZero(m));
  66. }
  67. INSTANTIATE_TEST_CASE_P(Core, HasNonZeroNegZeros,
  68. testing::Combine(
  69. testing::Values(CV_32FC1, CV_64FC1),
  70. testing::Values(Size(1, 1), Size(320, 240), Size(127, 113), Size(1, 113))
  71. )
  72. );
  73. typedef testing::TestWithParam<std::tuple<int, Size> > HasNonZeroLimitValues;
  74. TEST_P(HasNonZeroLimitValues, hasNonZeroLimitValues)
  75. {
  76. const int type = std::get<0>(GetParam());
  77. const Size size = std::get<1>(GetParam());
  78. Mat m = Mat(size, type);
  79. m.setTo(Scalar::all(std::numeric_limits<double>::infinity()));
  80. EXPECT_TRUE(hasNonZero(m));
  81. m.setTo(Scalar::all(-std::numeric_limits<double>::infinity()));
  82. EXPECT_TRUE(hasNonZero(m));
  83. m.setTo(Scalar::all(std::numeric_limits<double>::quiet_NaN()));
  84. EXPECT_TRUE(hasNonZero(m));
  85. m.setTo((CV_MAT_DEPTH(type) == CV_64F) ? Scalar::all(std::numeric_limits<double>::epsilon()) : Scalar::all(std::numeric_limits<float>::epsilon()));
  86. EXPECT_TRUE(hasNonZero(m));
  87. m.setTo((CV_MAT_DEPTH(type) == CV_64F) ? Scalar::all(std::numeric_limits<double>::min()) : Scalar::all(std::numeric_limits<float>::min()));
  88. EXPECT_TRUE(hasNonZero(m));
  89. m.setTo((CV_MAT_DEPTH(type) == CV_64F) ? Scalar::all(std::numeric_limits<double>::denorm_min()) : Scalar::all(std::numeric_limits<float>::denorm_min()));
  90. EXPECT_TRUE(hasNonZero(m));
  91. }
  92. INSTANTIATE_TEST_CASE_P(Core, HasNonZeroLimitValues,
  93. testing::Combine(
  94. testing::Values(CV_32FC1, CV_64FC1),
  95. testing::Values(Size(1, 1), Size(320, 240), Size(127, 113), Size(1, 113))
  96. )
  97. );
  98. typedef testing::TestWithParam<std::tuple<int, Size> > HasNonZeroRandom;
  99. TEST_P(HasNonZeroRandom, hasNonZeroRandom)
  100. {
  101. const int type = std::get<0>(GetParam());
  102. const Size size = std::get<1>(GetParam());
  103. RNG& rng = theRNG();
  104. const size_t N = std::min(100, size.area());
  105. for(size_t i = 0 ; i<N ; ++i)
  106. {
  107. const int nz_pos_x = rng.uniform(0, size.width);
  108. const int nz_pos_y = rng.uniform(0, size.height);
  109. Mat m = Mat::zeros(size, type);
  110. Mat nzROI = Mat(m, Rect(nz_pos_x, nz_pos_y, 1, 1));
  111. nzROI.setTo(Scalar::all(1));
  112. EXPECT_TRUE(hasNonZero(m));
  113. }
  114. }
  115. INSTANTIATE_TEST_CASE_P(Core, HasNonZeroRandom,
  116. testing::Combine(
  117. testing::Values(CV_8UC1, CV_8SC1, CV_16UC1, CV_16SC1, CV_32SC1, CV_32FC1, CV_64FC1),
  118. testing::Values(Size(1, 1), Size(320, 240), Size(127, 113), Size(1, 113))
  119. )
  120. );
  121. typedef testing::TestWithParam<tuple<int, int, bool> > HasNonZeroNd;
  122. TEST_P(HasNonZeroNd, hasNonZeroNd)
  123. {
  124. const int type = get<0>(GetParam());
  125. const int ndims = get<1>(GetParam());
  126. const bool continuous = get<2>(GetParam());
  127. RNG& rng = theRNG();
  128. const size_t N = 10;
  129. for(size_t i = 0 ; i<N ; ++i)
  130. {
  131. std::vector<size_t> steps(ndims);
  132. std::vector<int> sizes(ndims);
  133. size_t totalBytes = 1;
  134. for(int dim = 0 ; dim<ndims ; ++dim)
  135. {
  136. const bool isFirstDim = (dim == 0);
  137. const bool isLastDim = (dim+1 == ndims);
  138. const int length = rng.uniform(1, 64);
  139. steps[dim] = (isLastDim ? 1 : static_cast<size_t>(length))*CV_ELEM_SIZE(type);
  140. sizes[dim] = (isFirstDim || continuous) ? length : rng.uniform(1, length);
  141. totalBytes *= steps[dim]*static_cast<size_t>(sizes[dim]);
  142. }
  143. std::vector<unsigned char> buffer(totalBytes);
  144. void* data = buffer.data();
  145. Mat m = Mat(ndims, sizes.data(), type, data, steps.data());
  146. std::vector<Range> nzRange(ndims);
  147. for(int dim = 0 ; dim<ndims ; ++dim)
  148. {
  149. const int pos = rng.uniform(0, sizes[dim]);
  150. nzRange[dim] = Range(pos, pos+1);
  151. }
  152. Mat nzROI = Mat(m, nzRange.data());
  153. nzROI.setTo(Scalar::all(1));
  154. const int nzCount = countNonZero(m);
  155. EXPECT_EQ((nzCount>0), hasNonZero(m));
  156. }
  157. }
  158. INSTANTIATE_TEST_CASE_P(Core, HasNonZeroNd,
  159. testing::Combine(
  160. testing::Values(CV_8UC1),
  161. testing::Values(2, 3),
  162. testing::Values(true, false)
  163. )
  164. );
  165. }} // namespace