rmat_view_tests.cpp
8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// 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
#include "../test_precomp.hpp"
#include <opencv2/gapi/rmat.hpp>
#include <opencv2/gapi/util/compiler_hints.hpp>
#include "../src/backends/common/gbackend.hpp"
namespace opencv_test
{
using cv::GMatDesc;
using View = cv::RMat::View;
using cv::Mat;
using cv::gimpl::asMat;
using cv::gimpl::asView;
using namespace ::testing;
static void expect_eq_desc(const GMatDesc& desc, const View& view) {
EXPECT_EQ(desc.size, view.size());
EXPECT_EQ(desc.dims, view.dims());
EXPECT_EQ(desc.size.width, view.cols());
EXPECT_EQ(desc.size.height, view.rows());
EXPECT_EQ(desc.depth, view.depth());
EXPECT_EQ(desc.chan, view.chan());
EXPECT_EQ(desc.depth, view.depth());
EXPECT_EQ(desc.chan, view.chan());
}
TEST(RMatView, TestDefaultConstruction) {
View view;
GMatDesc desc{};
expect_eq_desc(desc, view);
EXPECT_EQ(nullptr, view.ptr());
EXPECT_EQ(0u, view.step());
}
struct RMatViewTest : public TestWithParam<int /*dataType*/>{};
TEST_P(RMatViewTest, ConstructionFromMat) {
auto type = GetParam();
Mat mat(8,8,type);
const auto desc = cv::descr_of(mat);
View view = asView(mat);
expect_eq_desc(desc, view);
EXPECT_EQ(mat.ptr(), view.ptr());
EXPECT_EQ(mat.step, view.step());
}
TEST(RMatView, TestConstructionFromMatND) {
std::vector<int> dims(4, 8);
Mat mat(dims, CV_8UC1);
const auto desc = cv::descr_of(mat);
View view(cv::descr_of(mat), mat.ptr());
expect_eq_desc(desc, view);
EXPECT_EQ(mat.ptr(), view.ptr());
}
TEST_P(RMatViewTest, DefaultStep) {
auto type = GetParam();
GMatDesc desc;
desc.chan = CV_MAT_CN(type);
desc.depth = CV_MAT_DEPTH(type);
desc.size = {8,8};
std::vector<unsigned char> data(desc.size.width*desc.size.height*CV_ELEM_SIZE(type));
View view(desc, data.data());
EXPECT_EQ(static_cast<size_t>(desc.size.width)*CV_ELEM_SIZE(type), view.step());
}
struct RMatViewNDTest : public TestWithParam<
std::tuple<int /*depth*/, int /*ndims*/>>{};
TEST_P(RMatViewNDTest, DefaultStep) {
int depth = 0, ndims = 0;
std::tie(depth, ndims) = GetParam();
std::vector<int> dims(ndims, 12);
GMatDesc desc;
desc.dims = dims;
desc.depth = depth;
GAPI_Assert(desc.chan == -1);
auto elemSize = CV_ELEM_SIZE(depth);
auto total = std::accumulate(dims.begin(), dims.end(), elemSize, std::multiplies<int>());
std::vector<unsigned char> data(total);
View view(desc, data.data());
auto step = static_cast<size_t>(total/dims[0]);
EXPECT_EQ(step, view.step(0));
for (int i = 1; i < ndims; i++) {
step /= dims[i];
EXPECT_EQ(step, view.step(i));
}
}
TEST_P(RMatViewNDTest, StepFromMat) {
int depth = 0, ndims = 0;
std::tie(depth, ndims) = GetParam();
std::vector<int> dims(ndims, 12);
cv::Mat mat(dims, depth);
auto view = asView(mat);
EXPECT_EQ(mat.ptr(), view.ptr());
for (int i = 0; i < ndims; i++) {
EXPECT_EQ(mat.step[i], view.step(i));
}
}
TEST_P(RMatViewNDTest, StepFromView) {
int depth = 0, ndims = 0;
std::tie(depth, ndims) = GetParam();
std::vector<int> dims(ndims, 12);
std::vector<int> aligned(ndims, 16);
GMatDesc desc;
desc.dims = dims;
desc.depth = depth;
GAPI_Assert(desc.chan == -1);
auto elemSize = CV_ELEM_SIZE(depth);
auto total = std::accumulate(aligned.begin(), aligned.end(), elemSize, std::multiplies<int>());
std::vector<unsigned char> data(total);
View::stepsT steps(ndims);
auto step = static_cast<size_t>(total/aligned[0]);
steps[0] = step;
for (int i = 1; i < ndims; i++) {
step /= aligned[i];
steps[i] = step;
}
View view(desc, data.data(), steps);
auto mat = asMat(view);
EXPECT_EQ(mat.ptr(), view.ptr());
for (int i = 0; i < ndims; i++) {
EXPECT_EQ(mat.step[i], view.step(i));
}
}
INSTANTIATE_TEST_CASE_P(Test, RMatViewNDTest,
Combine(Values(CV_8U, CV_32F), // depth
Values(1,2,3,4,7))); // ndims
struct RMatViewNDTestNegative : public TestWithParam<
std::tuple<int /*depth*/, int /*chan*/, int /*ndims*/>>{};
TEST_P(RMatViewNDTestNegative, DefaultStep) {
int depth = 0, chan = 0, ndims = 0;
std::tie(depth, chan, ndims) = GetParam();
std::vector<int> dims(ndims, 12);
GMatDesc desc;
desc.dims = dims;
desc.depth = depth;
desc.chan = chan;
auto elemSize = CV_ELEM_SIZE(depth);
auto total = std::accumulate(dims.begin(), dims.end(), elemSize, std::multiplies<int>());
std::vector<unsigned char> data(total);
EXPECT_ANY_THROW(View view(desc, data.data()));
}
INSTANTIATE_TEST_CASE_P(Test, RMatViewNDTestNegative,
Combine(Values(CV_8U, CV_32F), // depth
Values(1,2,3,4), // chan
Values(2,4,7))); // ndims
TEST_P(RMatViewTest, NonDefaultStepInput) {
auto type = GetParam();
Mat bigMat(16,16,type);
cv::randn(bigMat, cv::Scalar::all(127), cv::Scalar::all(40));
Mat mat = bigMat(cv::Rect{4,4,8,8});
View view = asView(mat);
const auto viewMat = asMat(view);
Mat ref, out;
cv::Size ksize{1,1};
cv::blur(viewMat, out, ksize);
cv::blur( mat, ref, ksize);
EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
}
TEST_P(RMatViewTest, NonDefaultStepOutput) {
auto type = GetParam();
Mat mat(8,8,type);
cv::randn(mat, cv::Scalar::all(127), cv::Scalar::all(40));
Mat bigMat = Mat::zeros(16,16,type);
Mat out = bigMat(cv::Rect{4,4,8,8});
View view = asView(out);
auto viewMat = asMat(view);
Mat ref;
cv::Size ksize{1,1};
cv::blur(mat, viewMat, ksize);
cv::blur(mat, ref, ksize);
EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
}
TEST_P(RMatViewTest, NonDefaultStep2DInput) {
auto type = GetParam();
Mat bigMat(16,16,type);
cv::randn(bigMat, cv::Scalar::all(127), cv::Scalar::all(40));
Mat mat = bigMat(cv::Rect{4,4,8,8});
View view(cv::descr_of(mat), mat.data, mat.step);
const auto viewMat = asMat(view);
Mat ref, out;
cv::Size ksize{1,1};
cv::blur(viewMat, out, ksize);
cv::blur( mat, ref, ksize);
EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
}
TEST_P(RMatViewTest, NonDefaultStep2DOutput) {
auto type = GetParam();
Mat mat(8,8,type);
cv::randn(mat, cv::Scalar::all(127), cv::Scalar::all(40));
Mat bigMat = Mat::zeros(16,16,type);
Mat out = bigMat(cv::Rect{4,4,8,8});
View view(cv::descr_of(out), out.data, out.step);
auto viewMat = asMat(view);
Mat ref;
cv::Size ksize{1,1};
cv::blur(mat, viewMat, ksize);
cv::blur(mat, ref, ksize);
EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
}
INSTANTIATE_TEST_CASE_P(Test, RMatViewTest,
Values(CV_8UC1, CV_8UC3, CV_32FC1));
struct RMatViewCallbackTest : public ::testing::Test {
RMatViewCallbackTest()
: mat(8,8,CV_8UC1) {
cv::randn(mat, cv::Scalar::all(127), cv::Scalar::all(40));
}
View getView() { return asView(mat, [this](){ callbackCalls++; }); }
int callbackCalls = 0;
Mat mat;
};
TEST_F(RMatViewCallbackTest, MoveCtor) {
{
View copy(getView());
cv::util::suppress_unused_warning(copy);
EXPECT_EQ(0, callbackCalls);
}
EXPECT_EQ(1, callbackCalls);
}
TEST_F(RMatViewCallbackTest, MoveCopy) {
{
View copy;
copy = getView();
cv::util::suppress_unused_warning(copy);
EXPECT_EQ(0, callbackCalls);
}
EXPECT_EQ(1, callbackCalls);
}
static int firstElement(const View& view) { return *view.ptr(); }
static void setFirstElement(View& view, uchar value) { *view.ptr() = value; }
TEST_F(RMatViewCallbackTest, MagazineInteraction) {
cv::gimpl::magazine::Class<View> mag;
constexpr int rc = 1;
constexpr uchar value = 11;
mag.slot<View>()[rc] = getView();
{
auto& mag_view = mag.slot<View>()[rc];
setFirstElement(mag_view, value);
auto mag_el = firstElement(mag_view);
EXPECT_EQ(value, mag_el);
}
{
const auto& mag_view = mag.slot<View>()[rc];
auto mag_el = firstElement(mag_view);
EXPECT_EQ(value, mag_el);
}
EXPECT_EQ(0, callbackCalls);
mag.slot<View>().erase(rc);
EXPECT_EQ(1, callbackCalls);
}
} // namespace opencv_test