gapi_imgproc_tests_common.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. //
  5. // Copyright (C) 2020 Intel Corporation
  6. #ifndef OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP
  7. #define OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP
  8. #include "gapi_tests_common.hpp"
  9. #include "../../include/opencv2/gapi/imgproc.hpp"
  10. #include <opencv2/imgproc.hpp>
  11. namespace opencv_test
  12. {
  13. // Draw random ellipses on given cv::Mat of given size and type
  14. static void initMatForFindingContours(cv::Mat& mat, const cv::Size& sz, const int type)
  15. {
  16. cv::RNG& rng = theRNG();
  17. mat = cv::Mat(sz, type, cv::Scalar::all(0));
  18. const size_t numEllipses = rng.uniform(1, 10);
  19. for( size_t i = 0; i < numEllipses; i++ )
  20. {
  21. cv::Point center;
  22. cv::Size axes;
  23. center.x = rng.uniform(0, sz.width);
  24. center.y = rng.uniform(0, sz.height);
  25. axes.width = rng.uniform(2, sz.width);
  26. axes.height = rng.uniform(2, sz.height);
  27. const int color = rng.uniform(1, 256);
  28. const double angle = rng.uniform(0., 180.);
  29. cv::ellipse(mat, center, axes, angle, 0., 360., color, 1, FILLED);
  30. }
  31. }
  32. enum OptionalFindContoursOutput {NONE, HIERARCHY};
  33. template<OptionalFindContoursOutput optional = NONE>
  34. cv::GComputation findContoursTestGAPI(const cv::Mat& in, const cv::RetrievalModes mode,
  35. const cv::ContourApproximationModes method,
  36. cv::GCompileArgs&& args,
  37. std::vector<std::vector<cv::Point>>& out_cnts_gapi,
  38. std::vector<cv::Vec4i>& /*out_hier_gapi*/,
  39. const cv::Point& offset = cv::Point())
  40. {
  41. cv::GMat g_in;
  42. cv::GOpaque<cv::Point> gOffset;
  43. cv::GArray<cv::GArray<cv::Point>> outCts;
  44. outCts = cv::gapi::findContours(g_in, mode, method, gOffset);
  45. cv::GComputation c(GIn(g_in, gOffset), GOut(outCts));
  46. c.apply(gin(in, offset), gout(out_cnts_gapi), std::move(args));
  47. return c;
  48. }
  49. template<> cv::GComputation findContoursTestGAPI<HIERARCHY> (
  50. const cv::Mat& in, const cv::RetrievalModes mode, const cv::ContourApproximationModes method,
  51. cv::GCompileArgs&& args, std::vector<std::vector<cv::Point>>& out_cnts_gapi,
  52. std::vector<cv::Vec4i>& out_hier_gapi, const cv::Point& offset)
  53. {
  54. cv::GMat g_in;
  55. cv::GOpaque<cv::Point> gOffset;
  56. cv::GArray<cv::GArray<cv::Point>> outCts;
  57. cv::GArray<cv::Vec4i> outHier;
  58. std::tie(outCts, outHier) = cv::gapi::findContoursH(g_in, mode, method, gOffset);
  59. cv::GComputation c(GIn(g_in, gOffset), GOut(outCts, outHier));
  60. c.apply(gin(in, offset), gout(out_cnts_gapi, out_hier_gapi), std::move(args));
  61. return c;
  62. }
  63. template<OptionalFindContoursOutput optional = NONE>
  64. void findContoursTestOpenCVCompare(const cv::Mat& in, const cv::RetrievalModes mode,
  65. const cv::ContourApproximationModes method,
  66. const std::vector<std::vector<cv::Point>>& out_cnts_gapi,
  67. const std::vector<cv::Vec4i>& out_hier_gapi,
  68. const CompareMats& cmpF, const cv::Point& offset = cv::Point())
  69. {
  70. // OpenCV code /////////////////////////////////////////////////////////////
  71. std::vector<std::vector<cv::Point>> out_cnts_ocv;
  72. std::vector<cv::Vec4i> out_hier_ocv;
  73. cv::findContours(in, out_cnts_ocv, out_hier_ocv, mode, method, offset);
  74. // Comparison //////////////////////////////////////////////////////////////
  75. EXPECT_TRUE(out_cnts_gapi.size() == out_cnts_ocv.size());
  76. cv::Mat out_mat_ocv = cv::Mat(cv::Size{ in.cols, in.rows }, in.type(), cv::Scalar::all(0));
  77. cv::Mat out_mat_gapi = cv::Mat(cv::Size{ in.cols, in.rows }, in.type(), cv::Scalar::all(0));
  78. cv::fillPoly(out_mat_ocv, out_cnts_ocv, cv::Scalar::all(1));
  79. cv::fillPoly(out_mat_gapi, out_cnts_gapi, cv::Scalar::all(1));
  80. EXPECT_TRUE(cmpF(out_mat_ocv, out_mat_gapi));
  81. if (optional == HIERARCHY)
  82. {
  83. EXPECT_TRUE(out_hier_ocv.size() == out_hier_gapi.size());
  84. EXPECT_TRUE(AbsExactVector<cv::Vec4i>().to_compare_f()(out_hier_ocv, out_hier_gapi));
  85. }
  86. }
  87. template<OptionalFindContoursOutput optional = NONE>
  88. void findContoursTestBody(const cv::Size& sz, const MatType2& type, const cv::RetrievalModes mode,
  89. const cv::ContourApproximationModes method, const CompareMats& cmpF,
  90. cv::GCompileArgs&& args, const cv::Point& offset = cv::Point())
  91. {
  92. cv::Mat in;
  93. initMatForFindingContours(in, sz, type);
  94. std::vector<std::vector<cv::Point>> out_cnts_gapi;
  95. std::vector<cv::Vec4i> out_hier_gapi;
  96. findContoursTestGAPI<optional>(in, mode, method, std::move(args), out_cnts_gapi, out_hier_gapi,
  97. offset);
  98. findContoursTestOpenCVCompare<optional>(in, mode, method, out_cnts_gapi, out_hier_gapi, cmpF,
  99. offset);
  100. }
  101. //-------------------------------------------------------------------------------------------------
  102. template<typename In>
  103. static cv::GComputation boundingRectTestGAPI(const In& in, cv::GCompileArgs&& args,
  104. cv::Rect& out_rect_gapi)
  105. {
  106. cv::detail::g_type_of_t<In> g_in;
  107. auto out = cv::gapi::boundingRect(g_in);
  108. cv::GComputation c(cv::GIn(g_in), cv::GOut(out));
  109. c.apply(cv::gin(in), cv::gout(out_rect_gapi), std::move(args));
  110. return c;
  111. }
  112. template<typename In>
  113. static void boundingRectTestOpenCVCompare(const In& in, const cv::Rect& out_rect_gapi,
  114. const CompareRects& cmpF)
  115. {
  116. // OpenCV code /////////////////////////////////////////////////////////////
  117. cv::Rect out_rect_ocv = cv::boundingRect(in);
  118. // Comparison //////////////////////////////////////////////////////////////
  119. EXPECT_TRUE(cmpF(out_rect_gapi, out_rect_ocv));
  120. }
  121. template<typename In>
  122. static void boundingRectTestBody(const In& in, const CompareRects& cmpF, cv::GCompileArgs&& args)
  123. {
  124. cv::Rect out_rect_gapi;
  125. boundingRectTestGAPI(in, std::move(args), out_rect_gapi);
  126. boundingRectTestOpenCVCompare(in, out_rect_gapi, cmpF);
  127. }
  128. //-------------------------------------------------------------------------------------------------
  129. template<typename In>
  130. static cv::GComputation fitLineTestGAPI(const In& in, const cv::DistanceTypes distType,
  131. cv::GCompileArgs&& args, cv::Vec4f& out_vec_gapi)
  132. {
  133. const double paramDefault = 0., repsDefault = 0., aepsDefault = 0.;
  134. cv::detail::g_type_of_t<In> g_in;
  135. auto out = cv::gapi::fitLine2D(g_in, distType, paramDefault, repsDefault, aepsDefault);
  136. cv::GComputation c(cv::GIn(g_in), cv::GOut(out));
  137. c.apply(cv::gin(in), cv::gout(out_vec_gapi), std::move(args));
  138. return c;
  139. }
  140. template<typename In>
  141. static cv::GComputation fitLineTestGAPI(const In& in, const cv::DistanceTypes distType,
  142. cv::GCompileArgs&& args, cv::Vec6f& out_vec_gapi)
  143. {
  144. const double paramDefault = 0., repsDefault = 0., aepsDefault = 0.;
  145. cv::detail::g_type_of_t<In> g_in;
  146. auto out = cv::gapi::fitLine3D(g_in, distType, paramDefault, repsDefault, aepsDefault);
  147. cv::GComputation c(cv::GIn(g_in), cv::GOut(out));
  148. c.apply(cv::gin(in), cv::gout(out_vec_gapi), std::move(args));
  149. return c;
  150. }
  151. template<typename In, int dim>
  152. static void fitLineTestOpenCVCompare(const In& in, const cv::DistanceTypes distType,
  153. const cv::Vec<float, dim>& out_vec_gapi,
  154. const CompareVecs<float, dim>& cmpF)
  155. {
  156. const double paramDefault = 0., repsDefault = 0., aepsDefault = 0.;
  157. // OpenCV code /////////////////////////////////////////////////////////////
  158. cv::Vec<float, dim> out_vec_ocv;
  159. cv::fitLine(in, out_vec_ocv, distType, paramDefault, repsDefault, aepsDefault);
  160. // Comparison //////////////////////////////////////////////////////////////
  161. EXPECT_TRUE(cmpF(out_vec_gapi, out_vec_ocv));
  162. }
  163. template<typename In, int dim>
  164. static void fitLineTestBody(const In& in, const cv::DistanceTypes distType,
  165. const CompareVecs<float, dim>& cmpF, cv::GCompileArgs&& args)
  166. {
  167. cv::Vec<float, dim> out_vec_gapi;
  168. fitLineTestGAPI(in, distType, std::move(args), out_vec_gapi);
  169. fitLineTestOpenCVCompare(in, distType, out_vec_gapi, cmpF);
  170. }
  171. } // namespace opencv_test
  172. #endif // OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP