Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / test / gapi_fluid_roi_test.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 "gapi_fluid_test_kernels.hpp"
11
12 namespace opencv_test
13 {
14
15 using namespace cv::gapi_test_kernels;
16
17 struct PartialComputation : public TestWithParam <std::tuple<cv::Rect>> {};
18 TEST_P(PartialComputation, Test)
19 {
20     cv::Rect roi;
21     std::tie(roi) = GetParam();
22
23     int borderType = BORDER_REPLICATE;
24     int kernelSize = 3;
25     cv::Point anchor = {-1, -1};
26
27     cv::GMat in;
28     cv::GMat out = TBlur3x3::on(in, borderType, {});
29     cv::GComputation c(cv::GIn(in), cv::GOut(out));
30
31     const auto sz = cv::Size(8, 10);
32     cv::Mat in_mat(sz, CV_8UC1);
33     cv::Scalar mean   = cv::Scalar(127.0f);
34     cv::Scalar stddev = cv::Scalar(40.f);
35     cv::randn(in_mat, mean, stddev);
36
37     cv::Mat out_mat_gapi = cv::Mat::zeros(sz, CV_8UC1);
38     cv::Mat out_mat_ocv = cv::Mat::zeros(sz, CV_8UC1);
39
40     // Run G-API
41     auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{to_own(roi)}}));
42     cc(cv::gin(in_mat), cv::gout(out_mat_gapi));
43
44     // Check with OpenCV
45     if (roi == cv::Rect{}) roi = cv::Rect{0,0,sz.width,sz.height};
46     cv::blur(in_mat(roi), out_mat_ocv(roi), {kernelSize, kernelSize}, anchor, borderType);
47
48     EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
49 }
50
51 INSTANTIATE_TEST_CASE_P(Fluid, PartialComputation,
52                         Values(cv::Rect{},        cv::Rect{0,0,8,6}, cv::Rect{0,1,8,3},
53                                cv::Rect{0,2,8,3}, cv::Rect{0,3,8,5}, cv::Rect{0,4,8,6}));
54
55 struct PartialComputationAddC : public TestWithParam <std::tuple<cv::Rect>> {};
56 TEST_P(PartialComputationAddC, Test)
57 {
58     cv::Rect roi;
59     std::tie(roi) = GetParam();
60
61     cv::GMat in;
62     cv::GMat out = TAddCSimple::on(in, 1);
63     cv::GComputation c(cv::GIn(in), cv::GOut(out));
64
65     const auto sz = cv::Size(8, 10);
66     cv::Mat in_mat(sz, CV_8UC1);
67     cv::Scalar mean   = cv::Scalar(127.0f);
68     cv::Scalar stddev = cv::Scalar(40.f);
69     cv::randn(in_mat, mean, stddev);
70
71     cv::Mat out_mat_gapi = cv::Mat::zeros(sz, CV_8UC1);
72     cv::Mat out_mat_ocv = cv::Mat::zeros(sz, CV_8UC1);
73
74     // Run G-API
75     auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{to_own(roi)}}));
76     cc(cv::gin(in_mat), cv::gout(out_mat_gapi));
77
78     // Check with OpenCV
79     if (roi == cv::Rect{}) roi = cv::Rect{0,0,sz.width,sz.height};
80     out_mat_ocv(roi) = in_mat(roi) + 1;
81
82     EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
83 }
84
85 INSTANTIATE_TEST_CASE_P(FluidRoi, PartialComputationAddC,
86                         Values(cv::Rect{},        cv::Rect{0,0,8,6}, cv::Rect{0,1,8,3},
87                                cv::Rect{0,2,8,3}, cv::Rect{0,3,8,5}, cv::Rect{0,4,8,6}));
88
89 struct SequenceOfBlursRoiTest : public TestWithParam <std::tuple<int, cv::Rect>> {};
90 TEST_P(SequenceOfBlursRoiTest, Test)
91 {
92     cv::Size sz_in = { 320, 240 };
93
94     int borderType = 0;
95     cv::Rect roi;
96     std::tie(borderType, roi) = GetParam();
97     cv::Mat in_mat(sz_in, CV_8UC1);
98     cv::Scalar mean   = cv::Scalar(127.0f);
99     cv::Scalar stddev = cv::Scalar(40.f);
100
101     cv::randn(in_mat, mean, stddev);
102
103     cv::Point anchor = {-1, -1};
104     cv::Scalar borderValue(0);
105
106     GMat in;
107     auto mid = TBlur3x3::on(in,  borderType, borderValue);
108     auto out = TBlur5x5::on(mid, borderType, borderValue);
109
110     Mat out_mat_gapi = Mat::zeros(sz_in, CV_8UC1);
111
112     GComputation c(GIn(in), GOut(out));
113     auto cc = c.compile(descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{to_own(roi)}}));
114     cc(gin(in_mat), gout(out_mat_gapi));
115
116     cv::Mat mid_mat_ocv = Mat::zeros(sz_in, CV_8UC1);
117     cv::Mat out_mat_ocv = Mat::zeros(sz_in, CV_8UC1);
118
119     cv::blur(in_mat, mid_mat_ocv, {3,3}, anchor, borderType);
120
121     if (roi == cv::Rect{})
122     {
123         roi = cv::Rect{0, 0, sz_in.width, sz_in.height};
124     }
125
126     cv::blur(mid_mat_ocv(roi), out_mat_ocv(roi), {5,5}, anchor, borderType);
127
128     EXPECT_EQ(0, countNonZero(out_mat_ocv != out_mat_gapi));
129 }
130
131 INSTANTIATE_TEST_CASE_P(FluidRoi, SequenceOfBlursRoiTest,
132                         Combine(Values(BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT_101),
133                                 Values(cv::Rect{0,0,320,240}, cv::Rect{0,64,320,128}, cv::Rect{0,128,320,112})));
134
135 struct TwoBlursRoiTest : public TestWithParam <std::tuple<int, int, int, int, int, int, bool, cv::Rect>> {};
136 TEST_P(TwoBlursRoiTest, Test)
137 {
138     cv::Size sz_in = { 320, 240 };
139
140     int kernelSize1 = 0, kernelSize2 = 0;
141     int borderType1 = -1, borderType2 = -1;
142     cv::Scalar borderValue1{}, borderValue2{};
143     bool readFromInput = false;
144     cv::Rect outRoi;
145     std::tie(kernelSize1, borderType1, borderValue1, kernelSize2, borderType2, borderValue2, readFromInput, outRoi) = GetParam();
146     cv::Mat in_mat(sz_in, CV_8UC1);
147     cv::Scalar mean   = cv::Scalar(127.0f);
148     cv::Scalar stddev = cv::Scalar(40.f);
149
150     cv::randn(in_mat, mean, stddev);
151
152     cv::Point anchor = {-1, -1};
153
154     auto blur1 = kernelSize1 == 3 ? &TBlur3x3::on : TBlur5x5::on;
155     auto blur2 = kernelSize2 == 3 ? &TBlur3x3::on : TBlur5x5::on;
156
157     GMat in, out1, out2;
158     if (readFromInput)
159     {
160         out1 = blur1(in, borderType1, borderValue1);
161         out2 = blur2(in, borderType2, borderValue2);
162     }
163     else
164     {
165         auto mid = TAddCSimple::on(in, 0);
166         out1 = blur1(mid, borderType1, borderValue1);
167         out2 = blur2(mid, borderType2, borderValue2);
168     }
169
170     Mat out_mat_gapi1 = Mat::zeros(sz_in, CV_8UC1);
171     Mat out_mat_gapi2 = Mat::zeros(sz_in, CV_8UC1);
172
173     GComputation c(GIn(in), GOut(out1, out2));
174     auto cc = c.compile(descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{outRoi, outRoi}}));
175     cc(gin(in_mat), gout(out_mat_gapi1, out_mat_gapi2));
176
177     cv::Mat out_mat_ocv1 = Mat::zeros(sz_in, CV_8UC1);
178     cv::Mat out_mat_ocv2 = Mat::zeros(sz_in, CV_8UC1);
179
180     cv::blur(in_mat(outRoi), out_mat_ocv1(outRoi), {kernelSize1, kernelSize1}, anchor, borderType1);
181     cv::blur(in_mat(outRoi), out_mat_ocv2(outRoi), {kernelSize2, kernelSize2}, anchor, borderType2);
182
183     EXPECT_EQ(0, countNonZero(out_mat_ocv1 != out_mat_gapi1));
184     EXPECT_EQ(0, countNonZero(out_mat_ocv2 != out_mat_gapi2));
185 }
186
187 INSTANTIATE_TEST_CASE_P(FluidRoi, TwoBlursRoiTest,
188                         Combine(Values(3, 5),
189                                 Values(cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT_101),
190                                 Values(0),
191                                 Values(3, 5),
192                                 Values(cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT_101),
193                                 Values(0),
194                                 testing::Bool(), // Read from input directly or place a copy node at start
195                                 Values(cv::Rect{0,0,320,240}, cv::Rect{0,64,320,128}, cv::Rect{0,128,320,112})));
196
197 } // namespace opencv_test