// 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) 2018 Intel Corporation #include "test_precomp.hpp" #include #include namespace opencv_test { namespace ThisTest { using GPointArray = cv::GArray; G_TYPED_KERNEL(GeneratePoints, , "test.array.out_const") { static GArrayDesc outMeta(const GMatDesc&) { return empty_array_desc(); } }; G_TYPED_KERNEL(FindCorners, , "test.array.out") { static GArrayDesc outMeta(const GMatDesc&) { return empty_array_desc(); } }; G_TYPED_KERNEL(CountCorners, , "test.array.in") { static GScalarDesc outMeta(const GArrayDesc &) { return empty_scalar_desc(); } }; } // namespace ThisTest namespace { GAPI_OCV_KERNEL(OCVGeneratePoints, ThisTest::GeneratePoints) { static void run(cv::Mat, std::vector &out) { for (int i = 0; i < 10; i++) out.emplace_back(i, i); } }; GAPI_OCV_KERNEL(OCVFindCorners, ThisTest::FindCorners) { static void run(cv::Mat in, std::vector &out) { cv::goodFeaturesToTrack(in, out, 1024, 0.01, 3); } }; GAPI_OCV_KERNEL(OCVCountCorners, ThisTest::CountCorners) { static void run(const std::vector &in, cv::Scalar &out) { out[0] = static_cast(in.size()); } }; cv::Mat cross(int w, int h) { cv::Mat mat = cv::Mat::eye(h, w, CV_8UC1)*255; cv::Mat yee; cv::flip(mat, yee, 0); // X-axis mat |= yee; // make an "X" matrix; return mat; } } // (anonymous namespace) TEST(GArray, TestReturnValue) { // FIXME: Make .apply() able to take compile arguments cv::GComputationT c(ThisTest::FindCorners::on); auto cc = c.compile(cv::GMatDesc{CV_8U,1,{32,32}}, cv::compile_args(cv::gapi::kernels())); // Prepare input matrix cv::Mat input = cross(32, 32); std::vector points; cc(input, points); // OCV goodFeaturesToTrack should find 5 points here (with these settings) EXPECT_EQ(5u, points.size()); EXPECT_TRUE(ade::util::find(points, cv::Point(16,16)) != points.end()); EXPECT_TRUE(ade::util::find(points, cv::Point(30,30)) != points.end()); EXPECT_TRUE(ade::util::find(points, cv::Point( 1,30)) != points.end()); EXPECT_TRUE(ade::util::find(points, cv::Point(30, 1)) != points.end()); EXPECT_TRUE(ade::util::find(points, cv::Point( 1, 1)) != points.end()); } TEST(GArray, TestInputArg) { cv::GComputationT c(ThisTest::CountCorners::on); auto cc = c.compile(cv::empty_array_desc(), cv::compile_args(cv::gapi::kernels())); const std::vector arr = {cv::Point(1,1), cv::Point(2,2)}; cv::Scalar out; cc(arr, out); EXPECT_EQ(2, out[0]); } TEST(GArray, TestPipeline) { cv::GComputationT c([](cv::GMat in) { return ThisTest::CountCorners::on(ThisTest::FindCorners::on(in)); }); auto cc = c.compile(cv::GMatDesc{CV_8U,1,{32,32}}, cv::compile_args(cv::gapi::kernels())); cv::Mat input = cross(32, 32); cv::Scalar out; cc(input, out); EXPECT_EQ(5, out[0]); } TEST(GArray, NoAggregationBetweenRuns) { cv::GComputationT c([](cv::GMat in) { return ThisTest::CountCorners::on(ThisTest::GeneratePoints::on(in)); }); auto cc = c.compile(cv::GMatDesc{CV_8U,1,{32,32}}, cv::compile_args(cv::gapi::kernels())); cv::Mat input = cv::Mat::eye(32, 32, CV_8UC1); cv::Scalar out; cc(input, out); EXPECT_EQ(10, out[0]); // Last kernel in the graph counts number of elements in array, returned by the previous kernel // (in this test, this variable is constant). // After 10 executions, this number MUST remain the same - 1st kernel is adding new values on every // run, but it is graph's responsibility to reset internal object state. cv::Scalar out2; for (int i = 0; i < 10; i++) { cc(input, out2); } EXPECT_EQ(10, out2[0]); } TEST(GArray, TestIntermediateOutput) { using Result = std::tuple; cv::GComputationT c([](cv::GMat in) { auto corners = ThisTest::GeneratePoints::on(in); return std::make_tuple(corners, ThisTest::CountCorners::on(corners)); }); cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1); std::vector out_points; cv::Scalar out_count; auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(cv::gapi::kernels())); cc(in_mat, out_points, out_count); EXPECT_EQ(10u, out_points.size()); EXPECT_EQ(10, out_count[0]); } TEST(GArray_VectorRef, TestMov) { // Warning: this test is testing some not-very-public APIs // Test how VectorRef's mov() (aka poor man's move()) is working. using I = int; using V = std::vector; const V vgold = { 1, 2, 3}; V vtest = vgold; const I* vptr = vtest.data(); cv::detail::VectorRef vref(vtest); cv::detail::VectorRef vmov; vmov.reset(); EXPECT_EQ(vgold, vref.rref()); vmov.mov(vref); EXPECT_EQ(vgold, vmov.rref()); EXPECT_EQ(vptr, vmov.rref().data()); EXPECT_EQ(V{}, vref.rref()); EXPECT_EQ(V{}, vtest); } TEST(GArray_VectorRef, Kind) { cv::detail::VectorRef v1(std::vector{}); EXPECT_EQ(cv::detail::OpaqueKind::CV_RECT, v1.getKind()); cv::detail::VectorRef v2(std::vector{}); EXPECT_EQ(cv::detail::OpaqueKind::CV_MAT, v2.getKind()); cv::detail::VectorRef v3(std::vector{}); EXPECT_EQ(cv::detail::OpaqueKind::CV_INT, v3.getKind()); cv::detail::VectorRef v4(std::vector{}); EXPECT_EQ(cv::detail::OpaqueKind::CV_DOUBLE, v4.getKind()); cv::detail::VectorRef v5(std::vector{}); EXPECT_EQ(cv::detail::OpaqueKind::CV_SCALAR, v5.getKind()); cv::detail::VectorRef v6(std::vector{}); EXPECT_EQ(cv::detail::OpaqueKind::CV_POINT, v6.getKind()); cv::detail::VectorRef v7(std::vector{}); EXPECT_EQ(cv::detail::OpaqueKind::CV_SIZE, v7.getKind()); cv::detail::VectorRef v8(std::vector{}); EXPECT_EQ(cv::detail::OpaqueKind::CV_UNKNOWN, v8.getKind()); } TEST(GArray_VectorRef, TestRvalue) { // Warning: this test is testing some not-very-public APIs cv::detail::VectorRef vref(std::vector{3, 5, -4}); auto v = std::vector{3, 5, -4}; EXPECT_EQ(vref.rref(), v); } } // namespace opencv_test