perf_integral.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "perf_precomp.hpp"
  5. namespace opencv_test {
  6. enum PerfSqMatDepth{
  7. DEPTH_32S_32S = 0,
  8. DEPTH_32S_32F,
  9. DEPTH_32S_64F,
  10. DEPTH_32F_32F,
  11. DEPTH_32F_64F,
  12. DEPTH_64F_64F};
  13. CV_ENUM(IntegralOutputDepths, DEPTH_32S_32S, DEPTH_32S_32F, DEPTH_32S_64F, DEPTH_32F_32F, DEPTH_32F_64F, DEPTH_64F_64F)
  14. static int extraOutputDepths[6][2] = {{CV_32S, CV_32S}, {CV_32S, CV_32F}, {CV_32S, CV_64F}, {CV_32F, CV_32F}, {CV_32F, CV_64F}, {CV_64F, CV_64F}};
  15. typedef tuple<Size, MatType, MatDepth> Size_MatType_OutMatDepth_t;
  16. typedef perf::TestBaseWithParam<Size_MatType_OutMatDepth_t> Size_MatType_OutMatDepth;
  17. typedef tuple<Size, std::tuple<MatType, IntegralOutputDepths>> Size_MatType_OutMatDepthArray_t;
  18. typedef perf::TestBaseWithParam<Size_MatType_OutMatDepthArray_t> Size_MatType_OutMatDepthArray;
  19. PERF_TEST_P(Size_MatType_OutMatDepth, integral,
  20. testing::Combine(
  21. testing::Values(TYPICAL_MAT_SIZES),
  22. testing::Values(CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4),
  23. testing::Values(CV_32S, CV_32F, CV_64F)
  24. )
  25. )
  26. {
  27. Size sz = get<0>(GetParam());
  28. int matType = get<1>(GetParam());
  29. int sdepth = get<2>(GetParam());
  30. Mat src(sz, matType);
  31. Mat sum(sz, sdepth);
  32. declare.in(src, WARMUP_RNG).out(sum);
  33. if (sdepth == CV_32F)
  34. src *= (1 << 23) / (double)(sz.area() * 256); // FP32 calculations are not accurate (mantissa is 23-bit)
  35. TEST_CYCLE() integral(src, sum, sdepth);
  36. Mat src_roi; src(Rect(src.cols - 4, src.rows - 4, 4, 4)).convertTo(src_roi, sdepth);
  37. Mat restored_src_roi =
  38. sum(Rect(sum.cols - 4, sum.rows - 4, 4, 4)) + sum(Rect(sum.cols - 5, sum.rows - 5, 4, 4)) -
  39. sum(Rect(sum.cols - 4, sum.rows - 5, 4, 4)) - sum(Rect(sum.cols - 5, sum.rows - 4, 4, 4));
  40. EXPECT_EQ(0, cvtest::norm(restored_src_roi, src_roi, NORM_INF))
  41. << src_roi << endl << restored_src_roi << endl
  42. << sum(Rect(sum.cols - 4, sum.rows - 4, 4, 4));
  43. if (sdepth == CV_32F)
  44. SANITY_CHECK_NOTHING();
  45. else
  46. SANITY_CHECK(sum, 1e-6);
  47. }
  48. PERF_TEST_P(Size_MatType_OutMatDepth, integral_sqsum,
  49. testing::Combine(
  50. testing::Values(TYPICAL_MAT_SIZES),
  51. testing::Values(CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4),
  52. testing::Values(CV_32S, CV_32F, CV_64F)
  53. )
  54. )
  55. {
  56. Size sz = get<0>(GetParam());
  57. int matType = get<1>(GetParam());
  58. int sdepth = get<2>(GetParam());
  59. Mat src(sz, matType);
  60. Mat sum(sz, sdepth);
  61. Mat sqsum(sz, sdepth);
  62. declare.in(src, WARMUP_RNG).out(sum, sqsum);
  63. declare.time(100);
  64. TEST_CYCLE() integral(src, sum, sqsum, sdepth);
  65. SANITY_CHECK(sum, 1e-6);
  66. SANITY_CHECK(sqsum, 1e-6);
  67. }
  68. static std::vector<std::tuple<MatType, IntegralOutputDepths>> GetFullSqsumDepthPairs() {
  69. static int extraDepths[12][2] = {
  70. {CV_8U, DEPTH_32S_64F},
  71. {CV_8U, DEPTH_32S_32F},
  72. {CV_8U, DEPTH_32S_32S},
  73. {CV_8U, DEPTH_32F_64F},
  74. {CV_8U, DEPTH_32F_32F},
  75. {CV_8U, DEPTH_64F_64F},
  76. {CV_16U, DEPTH_64F_64F},
  77. {CV_16S, DEPTH_64F_64F},
  78. {CV_32F, DEPTH_32F_64F},
  79. {CV_32F, DEPTH_32F_32F},
  80. {CV_32F, DEPTH_64F_64F},
  81. {CV_64F, DEPTH_64F_64F}
  82. };
  83. std::vector<std::tuple<MatType, IntegralOutputDepths>> valid_pairs;
  84. for (size_t i = 0; i < 12; i++) {
  85. for (int cn = 1; cn <= 4; cn++) {
  86. valid_pairs.emplace_back(CV_MAKETYPE(extraDepths[i][0], cn), extraDepths[i][1]);
  87. }
  88. }
  89. return valid_pairs;
  90. }
  91. PERF_TEST_P(Size_MatType_OutMatDepthArray, DISABLED_integral_sqsum_full,
  92. testing::Combine(
  93. testing::Values(TYPICAL_MAT_SIZES),
  94. testing::ValuesIn(GetFullSqsumDepthPairs())
  95. )
  96. )
  97. {
  98. Size sz = get<0>(GetParam());
  99. auto depths = get<1>(GetParam());
  100. int matType = get<0>(depths);
  101. int sdepth = extraOutputDepths[get<1>(depths)][0];
  102. int sqdepth = extraOutputDepths[get<1>(depths)][1];
  103. Mat src(sz, matType);
  104. Mat sum(sz, sdepth);
  105. Mat sqsum(sz, sqdepth);
  106. declare.in(src, WARMUP_RNG).out(sum, sqsum);
  107. declare.time(100);
  108. TEST_CYCLE() integral(src, sum, sqsum, sdepth, sqdepth);
  109. SANITY_CHECK_NOTHING();
  110. }
  111. PERF_TEST_P( Size_MatType_OutMatDepth, integral_sqsum_tilted,
  112. testing::Combine(
  113. testing::Values(TYPICAL_MAT_SIZES),
  114. testing::Values( CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4 ),
  115. testing::Values( CV_32S, CV_32F, CV_64F )
  116. )
  117. )
  118. {
  119. Size sz = get<0>(GetParam());
  120. int matType = get<1>(GetParam());
  121. int sdepth = get<2>(GetParam());
  122. Mat src(sz, matType);
  123. Mat sum(sz, sdepth);
  124. Mat sqsum(sz, sdepth);
  125. Mat tilted(sz, sdepth);
  126. declare.in(src, WARMUP_RNG).out(sum, sqsum, tilted);
  127. declare.time(100);
  128. TEST_CYCLE() integral(src, sum, sqsum, tilted, sdepth);
  129. SANITY_CHECK(sum, 1e-6);
  130. SANITY_CHECK(sqsum, 1e-6);
  131. SANITY_CHECK(tilted, 1e-6, tilted.depth() > CV_32S ? ERROR_RELATIVE : ERROR_ABSOLUTE);
  132. }
  133. } // namespace