Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / test / gapi_array_tests.cpp
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) 2018-2019 Intel Corporation
6
7
8 #include "test_precomp.hpp"
9
10 #include <vector>
11 #include <ade/util/algorithm.hpp>
12
13 namespace opencv_test
14 {
15
16 namespace ThisTest
17 {
18 using GPointArray = cv::GArray<cv::Point>;
19 G_TYPED_KERNEL(GeneratePoints, <GPointArray(GMat)>, "test.array.out_const")
20 {
21     static GArrayDesc outMeta(const GMatDesc&) { return empty_array_desc(); }
22 };
23 G_TYPED_KERNEL(FindCorners,    <GPointArray(GMat)>, "test.array.out")
24 {
25     static GArrayDesc outMeta(const GMatDesc&) { return empty_array_desc(); }
26 };
27 G_TYPED_KERNEL(CountCorners,   <GScalar(GPointArray)>,  "test.array.in")
28 {
29     static GScalarDesc outMeta(const GArrayDesc &) { return empty_scalar_desc(); }
30 };
31 } // namespace ThisTest
32
33 namespace
34 {
35 GAPI_OCV_KERNEL(OCVGeneratePoints, ThisTest::GeneratePoints)
36 {
37     static void run(cv::Mat, std::vector<cv::Point> &out)
38     {
39         for (int i = 0; i < 10; i++)
40             out.emplace_back(i, i);
41     }
42 };
43
44 GAPI_OCV_KERNEL(OCVFindCorners, ThisTest::FindCorners)
45 {
46     static void run(cv::Mat in, std::vector<cv::Point> &out)
47     {
48         cv::goodFeaturesToTrack(in, out, 1024, 0.01, 3);
49     }
50 };
51
52 GAPI_OCV_KERNEL(OCVCountCorners, ThisTest::CountCorners)
53 {
54     static void run(const std::vector<cv::Point> &in, cv::Scalar &out)
55     {
56         out[0] = static_cast<double>(in.size());
57     }
58 };
59
60 cv::Mat cross(int w, int h)
61 {
62     cv::Mat mat = cv::Mat::eye(h, w, CV_8UC1)*255;
63     cv::Mat yee;
64     cv::flip(mat, yee, 0); // X-axis
65     mat |= yee;            // make an "X" matrix;
66     return mat;
67 }
68 } // (anonymous namespace)
69
70 TEST(GArray, TestReturnValue)
71 {
72     // FIXME: Make .apply() able to take compile arguments
73     cv::GComputationT<ThisTest::GPointArray(cv::GMat)> c(ThisTest::FindCorners::on);
74     auto cc = c.compile(cv::GMatDesc{CV_8U,1,{32,32}},
75                         cv::compile_args(cv::gapi::kernels<OCVFindCorners>()));
76
77     // Prepare input matrix
78     cv::Mat input = cross(32, 32);
79
80     std::vector<cv::Point> points;
81     cc(input, points);
82
83     // OCV goodFeaturesToTrack should find 5 points here (with these settings)
84     EXPECT_EQ(5u, points.size());
85     EXPECT_TRUE(ade::util::find(points, cv::Point(16,16)) != points.end());
86     EXPECT_TRUE(ade::util::find(points, cv::Point(30,30)) != points.end());
87     EXPECT_TRUE(ade::util::find(points, cv::Point( 1,30)) != points.end());
88     EXPECT_TRUE(ade::util::find(points, cv::Point(30, 1)) != points.end());
89     EXPECT_TRUE(ade::util::find(points, cv::Point( 1, 1)) != points.end());
90 }
91
92 TEST(GArray, TestInputArg)
93 {
94     cv::GComputationT<cv::GScalar(ThisTest::GPointArray)> c(ThisTest::CountCorners::on);
95     auto cc = c.compile(cv::empty_array_desc(),
96                         cv::compile_args(cv::gapi::kernels<OCVCountCorners>()));
97
98     const std::vector<cv::Point> arr = {cv::Point(1,1), cv::Point(2,2)};
99     cv::Scalar out;
100     cc(arr, out);
101     EXPECT_EQ(2, out[0]);
102 }
103
104 TEST(GArray, TestPipeline)
105 {
106     cv::GComputationT<cv::GScalar(cv::GMat)> c([](cv::GMat in)
107     {
108         return ThisTest::CountCorners::on(ThisTest::FindCorners::on(in));
109     });
110     auto cc = c.compile(cv::GMatDesc{CV_8U,1,{32,32}},
111                         cv::compile_args(cv::gapi::kernels<OCVFindCorners, OCVCountCorners>()));
112
113     cv::Mat input = cross(32, 32);
114     cv::Scalar out;
115     cc(input, out);
116     EXPECT_EQ(5, out[0]);
117 }
118
119 TEST(GArray, NoAggregationBetweenRuns)
120 {
121     cv::GComputationT<cv::GScalar(cv::GMat)> c([](cv::GMat in)
122     {
123         return ThisTest::CountCorners::on(ThisTest::GeneratePoints::on(in));
124     });
125     auto cc = c.compile(cv::GMatDesc{CV_8U,1,{32,32}},
126                         cv::compile_args(cv::gapi::kernels<OCVGeneratePoints, OCVCountCorners>()));
127
128     cv::Mat input = cv::Mat::eye(32, 32, CV_8UC1);
129     cv::Scalar out;
130
131     cc(input, out);
132     EXPECT_EQ(10, out[0]);
133
134     // Last kernel in the graph counts number of elements in array, returned by the previous kernel
135     // (in this test, this variable is constant).
136     // After 10 executions, this number MUST remain the same - 1st kernel is adding new values on every
137     // run, but it is graph's responsibility to reset internal object state.
138     cv::Scalar out2;
139     for (int i = 0; i < 10; i++)
140     {
141         cc(input, out2);
142     }
143     EXPECT_EQ(10, out2[0]);
144 }
145
146 TEST(GArray, TestIntermediateOutput)
147 {
148     using Result = std::tuple<ThisTest::GPointArray, cv::GScalar>;
149     cv::GComputationT<Result(cv::GMat)> c([](cv::GMat in)
150     {
151         auto corners = ThisTest::GeneratePoints::on(in);
152         return std::make_tuple(corners, ThisTest::CountCorners::on(corners));
153     });
154
155     cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
156     std::vector<cv::Point> out_points;
157     cv::Scalar out_count;
158
159     auto cc = c.compile(cv::descr_of(in_mat),
160                         cv::compile_args(cv::gapi::kernels<OCVGeneratePoints, OCVCountCorners>()));
161     cc(in_mat, out_points, out_count);
162
163     EXPECT_EQ(10u, out_points.size());
164     EXPECT_EQ(10,  out_count[0]);
165 }
166 } // namespace opencv_test