Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/gapi/test/common/gapi_tests_common.hpp
16339 views
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 Intel Corporation
6
7
8
#include <iostream>
9
10
#include "opencv2/ts.hpp"
11
#include "opencv2/gapi.hpp"
12
13
namespace
14
{
15
inline std::ostream& operator<<(std::ostream& o, const cv::GCompileArg& arg)
16
{
17
return o << (arg.tag.empty() ? "empty" : arg.tag);
18
}
19
}
20
21
namespace opencv_test
22
{
23
24
class TestFunctional
25
{
26
public:
27
cv::Mat in_mat1;
28
cv::Mat in_mat2;
29
cv::Mat out_mat_gapi;
30
cv::Mat out_mat_ocv;
31
32
cv::Scalar sc;
33
34
cv::Scalar initScalarRandU(unsigned upper)
35
{
36
auto& rng = cv::theRNG();
37
double s1 = rng(upper);
38
double s2 = rng(upper);
39
double s3 = rng(upper);
40
double s4 = rng(upper);
41
return cv::Scalar(s1, s2, s3, s4);
42
}
43
44
void initMatsRandU(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)
45
{
46
in_mat1 = cv::Mat(sz_in, type);
47
in_mat2 = cv::Mat(sz_in, type);
48
49
sc = initScalarRandU(100);
50
cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
51
cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
52
53
if (createOutputMatrices && dtype != -1)
54
{
55
out_mat_gapi = cv::Mat (sz_in, dtype);
56
out_mat_ocv = cv::Mat (sz_in, dtype);
57
}
58
}
59
60
void initMatrixRandU(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)
61
{
62
in_mat1 = cv::Mat(sz_in, type);
63
64
sc = initScalarRandU(100);
65
66
cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
67
68
if (createOutputMatrices && dtype != -1)
69
{
70
out_mat_gapi = cv::Mat (sz_in, dtype);
71
out_mat_ocv = cv::Mat (sz_in, dtype);
72
}
73
}
74
75
void initMatsRandN(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)
76
{
77
in_mat1 = cv::Mat(sz_in, type);
78
cv::randn(in_mat1, cv::Scalar::all(127), cv::Scalar::all(40.f));
79
80
if (createOutputMatrices && dtype != -1)
81
{
82
out_mat_gapi = cv::Mat(sz_in, dtype);
83
out_mat_ocv = cv::Mat(sz_in, dtype);
84
}
85
}
86
87
static cv::Mat nonZeroPixels(const cv::Mat& mat)
88
{
89
int channels = mat.channels();
90
std::vector<cv::Mat> split(channels);
91
cv::split(mat, split);
92
cv::Mat result;
93
for (int c=0; c < channels; c++)
94
{
95
if (c == 0)
96
result = split[c] != 0;
97
else
98
result = result | (split[c] != 0);
99
}
100
return result;
101
}
102
103
static int countNonZeroPixels(const cv::Mat& mat)
104
{
105
return cv::countNonZero( nonZeroPixels(mat) );
106
}
107
108
};
109
110
template<class T>
111
class TestParams: public TestFunctional, public TestWithParam<T>{};
112
113
template<class T>
114
class TestPerfParams: public TestFunctional, public perf::TestBaseWithParam<T>{};
115
116
using compare_f = std::function<bool(const cv::Mat &a, const cv::Mat &b)>;
117
118
template<typename T>
119
struct Wrappable
120
{
121
compare_f to_compare_f()
122
{
123
T t = *static_cast<T*const>(this);
124
return [t](const cv::Mat &a, const cv::Mat &b)
125
{
126
return t(a, b);
127
};
128
}
129
};
130
131
}
132
133