test_accum.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. #include "opencv2/imgproc/imgproc_c.h"
  43. namespace opencv_test { namespace {
  44. class CV_AccumBaseTest : public cvtest::ArrayTest
  45. {
  46. public:
  47. CV_AccumBaseTest();
  48. protected:
  49. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  50. double get_success_error_level( int test_case_idx, int i, int j );
  51. double alpha;
  52. };
  53. CV_AccumBaseTest::CV_AccumBaseTest()
  54. {
  55. test_array[INPUT].push_back(NULL);
  56. test_array[INPUT_OUTPUT].push_back(NULL);
  57. test_array[REF_INPUT_OUTPUT].push_back(NULL);
  58. test_array[MASK].push_back(NULL);
  59. optional_mask = true;
  60. element_wise_relative_error = false;
  61. } // ctor
  62. void CV_AccumBaseTest::get_test_array_types_and_sizes( int test_case_idx,
  63. vector<vector<Size> >& sizes, vector<vector<int> >& types )
  64. {
  65. RNG& rng = ts->get_rng();
  66. int depth = cvtest::randInt(rng) % 4, cn = cvtest::randInt(rng) & 1 ? 3 : 1;
  67. int accdepth = (int)(cvtest::randInt(rng) % 2 + 1);
  68. int i, input_count = (int)test_array[INPUT].size();
  69. cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  70. depth = depth == 0 ? CV_8U : depth == 1 ? CV_16U : depth == 2 ? CV_32F : CV_64F;
  71. accdepth = accdepth == 1 ? CV_32F : CV_64F;
  72. accdepth = MAX(accdepth, depth);
  73. for( i = 0; i < input_count; i++ )
  74. types[INPUT][i] = CV_MAKETYPE(depth,cn);
  75. types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(accdepth,cn);
  76. alpha = cvtest::randReal(rng);
  77. }
  78. double CV_AccumBaseTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  79. {
  80. return test_mat[INPUT_OUTPUT][0].depth() < CV_64F ||
  81. test_mat[INPUT][0].depth() == CV_32F ? FLT_EPSILON*100 : DBL_EPSILON*1000;
  82. }
  83. /// acc
  84. class CV_AccTest : public CV_AccumBaseTest
  85. {
  86. public:
  87. CV_AccTest() { }
  88. protected:
  89. void run_func();
  90. void prepare_to_validation( int );
  91. };
  92. void CV_AccTest::run_func(void)
  93. {
  94. cvAcc( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], test_array[MASK][0] );
  95. }
  96. void CV_AccTest::prepare_to_validation( int )
  97. {
  98. const Mat& src = test_mat[INPUT][0];
  99. Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
  100. const Mat& mask = test_array[MASK][0] ? test_mat[MASK][0] : Mat();
  101. Mat temp;
  102. cvtest::add( src, 1, dst, 1, cvScalarAll(0.), temp, dst.type() );
  103. cvtest::copy( temp, dst, mask );
  104. }
  105. /// square acc
  106. class CV_SquareAccTest : public CV_AccumBaseTest
  107. {
  108. public:
  109. CV_SquareAccTest();
  110. protected:
  111. void run_func();
  112. void prepare_to_validation( int );
  113. };
  114. CV_SquareAccTest::CV_SquareAccTest()
  115. {
  116. }
  117. void CV_SquareAccTest::run_func()
  118. {
  119. cvSquareAcc( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], test_array[MASK][0] );
  120. }
  121. void CV_SquareAccTest::prepare_to_validation( int )
  122. {
  123. const Mat& src = test_mat[INPUT][0];
  124. Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
  125. const Mat& mask = test_array[MASK][0] ? test_mat[MASK][0] : Mat();
  126. Mat temp;
  127. cvtest::convert( src, temp, dst.type() );
  128. cvtest::multiply( temp, temp, temp, 1 );
  129. cvtest::add( temp, 1, dst, 1, cvScalarAll(0.), temp, dst.depth() );
  130. cvtest::copy( temp, dst, mask );
  131. }
  132. /// multiply acc
  133. class CV_MultiplyAccTest : public CV_AccumBaseTest
  134. {
  135. public:
  136. CV_MultiplyAccTest();
  137. protected:
  138. void run_func();
  139. void prepare_to_validation( int );
  140. };
  141. CV_MultiplyAccTest::CV_MultiplyAccTest()
  142. {
  143. test_array[INPUT].push_back(NULL);
  144. }
  145. void CV_MultiplyAccTest::run_func()
  146. {
  147. cvMultiplyAcc( test_array[INPUT][0], test_array[INPUT][1],
  148. test_array[INPUT_OUTPUT][0], test_array[MASK][0] );
  149. }
  150. void CV_MultiplyAccTest::prepare_to_validation( int )
  151. {
  152. const Mat& src1 = test_mat[INPUT][0];
  153. const Mat& src2 = test_mat[INPUT][1];
  154. Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
  155. const Mat& mask = test_array[MASK][0] ? test_mat[MASK][0] : Mat();
  156. Mat temp1, temp2;
  157. cvtest::convert( src1, temp1, dst.type() );
  158. cvtest::convert( src2, temp2, dst.type() );
  159. cvtest::multiply( temp1, temp2, temp1, 1 );
  160. cvtest::add( temp1, 1, dst, 1, cvScalarAll(0.), temp1, dst.depth() );
  161. cvtest::copy( temp1, dst, mask );
  162. }
  163. /// running average
  164. class CV_RunningAvgTest : public CV_AccumBaseTest
  165. {
  166. public:
  167. CV_RunningAvgTest();
  168. protected:
  169. void run_func();
  170. void prepare_to_validation( int );
  171. };
  172. CV_RunningAvgTest::CV_RunningAvgTest()
  173. {
  174. }
  175. void CV_RunningAvgTest::run_func()
  176. {
  177. cvRunningAvg( test_array[INPUT][0], test_array[INPUT_OUTPUT][0],
  178. alpha, test_array[MASK][0] );
  179. }
  180. void CV_RunningAvgTest::prepare_to_validation( int )
  181. {
  182. const Mat& src = test_mat[INPUT][0];
  183. Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
  184. Mat temp;
  185. const Mat& mask = test_array[MASK][0] ? test_mat[MASK][0] : Mat();
  186. double a[1], b[1];
  187. int accdepth = test_mat[INPUT_OUTPUT][0].depth();
  188. CvMat A = cvMat(1,1,accdepth,a), B = cvMat(1,1,accdepth,b);
  189. cvSetReal1D( &A, 0, alpha);
  190. cvSetReal1D( &B, 0, 1 - cvGetReal1D(&A, 0));
  191. cvtest::convert( src, temp, dst.type() );
  192. cvtest::add( src, cvGetReal1D(&A, 0), dst, cvGetReal1D(&B, 0), cvScalarAll(0.), temp, temp.depth() );
  193. cvtest::copy( temp, dst, mask );
  194. }
  195. TEST(Video_Acc, accuracy) { CV_AccTest test; test.safe_run(); }
  196. TEST(Video_AccSquared, accuracy) { CV_SquareAccTest test; test.safe_run(); }
  197. TEST(Video_AccProduct, accuracy) { CV_MultiplyAccTest test; test.safe_run(); }
  198. TEST(Video_RunningAvg, accuracy) { CV_RunningAvgTest test; test.safe_run(); }
  199. }} // namespace