// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // // Copyright (C) 2020 Intel Corporation #ifndef OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP #define OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP #include "gapi_tests_common.hpp" #include "../../include/opencv2/gapi/imgproc.hpp" #include namespace opencv_test { // Draw random ellipses on given cv::Mat of given size and type static void initMatForFindingContours(cv::Mat& mat, const cv::Size& sz, const int type) { cv::RNG& rng = theRNG(); mat = cv::Mat(sz, type, cv::Scalar::all(0)); const size_t numEllipses = rng.uniform(1, 10); for( size_t i = 0; i < numEllipses; i++ ) { cv::Point center; cv::Size axes; center.x = rng.uniform(0, sz.width); center.y = rng.uniform(0, sz.height); axes.width = rng.uniform(2, sz.width); axes.height = rng.uniform(2, sz.height); const int color = rng.uniform(1, 256); const double angle = rng.uniform(0., 180.); cv::ellipse(mat, center, axes, angle, 0., 360., color, 1, FILLED); } } enum OptionalFindContoursOutput {NONE, HIERARCHY}; template cv::GComputation findContoursTestGAPI(const cv::Mat& in, const cv::RetrievalModes mode, const cv::ContourApproximationModes method, cv::GCompileArgs&& args, std::vector>& out_cnts_gapi, std::vector& /*out_hier_gapi*/, const cv::Point& offset = cv::Point()) { cv::GMat g_in; cv::GOpaque gOffset; cv::GArray> outCts; outCts = cv::gapi::findContours(g_in, mode, method, gOffset); cv::GComputation c(GIn(g_in, gOffset), GOut(outCts)); c.apply(gin(in, offset), gout(out_cnts_gapi), std::move(args)); return c; } template<> cv::GComputation findContoursTestGAPI ( const cv::Mat& in, const cv::RetrievalModes mode, const cv::ContourApproximationModes method, cv::GCompileArgs&& args, std::vector>& out_cnts_gapi, std::vector& out_hier_gapi, const cv::Point& offset) { cv::GMat g_in; cv::GOpaque gOffset; cv::GArray> outCts; cv::GArray outHier; std::tie(outCts, outHier) = cv::gapi::findContoursH(g_in, mode, method, gOffset); cv::GComputation c(GIn(g_in, gOffset), GOut(outCts, outHier)); c.apply(gin(in, offset), gout(out_cnts_gapi, out_hier_gapi), std::move(args)); return c; } template void findContoursTestOpenCVCompare(const cv::Mat& in, const cv::RetrievalModes mode, const cv::ContourApproximationModes method, const std::vector>& out_cnts_gapi, const std::vector& out_hier_gapi, const CompareMats& cmpF, const cv::Point& offset = cv::Point()) { // OpenCV code ///////////////////////////////////////////////////////////// std::vector> out_cnts_ocv; std::vector out_hier_ocv; cv::findContours(in, out_cnts_ocv, out_hier_ocv, mode, method, offset); // Comparison ////////////////////////////////////////////////////////////// EXPECT_TRUE(out_cnts_gapi.size() == out_cnts_ocv.size()); cv::Mat out_mat_ocv = cv::Mat(cv::Size{ in.cols, in.rows }, in.type(), cv::Scalar::all(0)); cv::Mat out_mat_gapi = cv::Mat(cv::Size{ in.cols, in.rows }, in.type(), cv::Scalar::all(0)); cv::fillPoly(out_mat_ocv, out_cnts_ocv, cv::Scalar::all(1)); cv::fillPoly(out_mat_gapi, out_cnts_gapi, cv::Scalar::all(1)); EXPECT_TRUE(cmpF(out_mat_ocv, out_mat_gapi)); if (optional == HIERARCHY) { EXPECT_TRUE(out_hier_ocv.size() == out_hier_gapi.size()); EXPECT_TRUE(AbsExactVector().to_compare_f()(out_hier_ocv, out_hier_gapi)); } } template void findContoursTestBody(const cv::Size& sz, const MatType2& type, const cv::RetrievalModes mode, const cv::ContourApproximationModes method, const CompareMats& cmpF, cv::GCompileArgs&& args, const cv::Point& offset = cv::Point()) { cv::Mat in; initMatForFindingContours(in, sz, type); std::vector> out_cnts_gapi; std::vector out_hier_gapi; findContoursTestGAPI(in, mode, method, std::move(args), out_cnts_gapi, out_hier_gapi, offset); findContoursTestOpenCVCompare(in, mode, method, out_cnts_gapi, out_hier_gapi, cmpF, offset); } //------------------------------------------------------------------------------------------------- template static cv::GComputation boundingRectTestGAPI(const In& in, cv::GCompileArgs&& args, cv::Rect& out_rect_gapi) { cv::detail::g_type_of_t g_in; auto out = cv::gapi::boundingRect(g_in); cv::GComputation c(cv::GIn(g_in), cv::GOut(out)); c.apply(cv::gin(in), cv::gout(out_rect_gapi), std::move(args)); return c; } template static void boundingRectTestOpenCVCompare(const In& in, const cv::Rect& out_rect_gapi, const CompareRects& cmpF) { // OpenCV code ///////////////////////////////////////////////////////////// cv::Rect out_rect_ocv = cv::boundingRect(in); // Comparison ////////////////////////////////////////////////////////////// EXPECT_TRUE(cmpF(out_rect_gapi, out_rect_ocv)); } template static void boundingRectTestBody(const In& in, const CompareRects& cmpF, cv::GCompileArgs&& args) { cv::Rect out_rect_gapi; boundingRectTestGAPI(in, std::move(args), out_rect_gapi); boundingRectTestOpenCVCompare(in, out_rect_gapi, cmpF); } //------------------------------------------------------------------------------------------------- template static cv::GComputation fitLineTestGAPI(const In& in, const cv::DistanceTypes distType, cv::GCompileArgs&& args, cv::Vec4f& out_vec_gapi) { const double paramDefault = 0., repsDefault = 0., aepsDefault = 0.; cv::detail::g_type_of_t g_in; auto out = cv::gapi::fitLine2D(g_in, distType, paramDefault, repsDefault, aepsDefault); cv::GComputation c(cv::GIn(g_in), cv::GOut(out)); c.apply(cv::gin(in), cv::gout(out_vec_gapi), std::move(args)); return c; } template static cv::GComputation fitLineTestGAPI(const In& in, const cv::DistanceTypes distType, cv::GCompileArgs&& args, cv::Vec6f& out_vec_gapi) { const double paramDefault = 0., repsDefault = 0., aepsDefault = 0.; cv::detail::g_type_of_t g_in; auto out = cv::gapi::fitLine3D(g_in, distType, paramDefault, repsDefault, aepsDefault); cv::GComputation c(cv::GIn(g_in), cv::GOut(out)); c.apply(cv::gin(in), cv::gout(out_vec_gapi), std::move(args)); return c; } template static void fitLineTestOpenCVCompare(const In& in, const cv::DistanceTypes distType, const cv::Vec& out_vec_gapi, const CompareVecs& cmpF) { const double paramDefault = 0., repsDefault = 0., aepsDefault = 0.; // OpenCV code ///////////////////////////////////////////////////////////// cv::Vec out_vec_ocv; cv::fitLine(in, out_vec_ocv, distType, paramDefault, repsDefault, aepsDefault); // Comparison ////////////////////////////////////////////////////////////// EXPECT_TRUE(cmpF(out_vec_gapi, out_vec_ocv)); } template static void fitLineTestBody(const In& in, const cv::DistanceTypes distType, const CompareVecs& cmpF, cv::GCompileArgs&& args) { cv::Vec out_vec_gapi; fitLineTestGAPI(in, distType, std::move(args), out_vec_gapi); fitLineTestOpenCVCompare(in, distType, out_vec_gapi, cmpF); } } // namespace opencv_test #endif // OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP