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.
5 // Copyright (C) 2018-2019 Intel Corporation
10 #include "opencv2/ts.hpp"
11 #include "opencv2/gapi.hpp"
15 inline std::ostream& operator<<(std::ostream& o, const cv::GCompileArg& arg)
17 return o << (arg.tag.empty() ? "empty" : arg.tag);
34 cv::Scalar initScalarRandU(unsigned upper)
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);
44 void initMatsRandU(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)
46 in_mat1 = cv::Mat(sz_in, type);
47 in_mat2 = cv::Mat(sz_in, type);
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));
53 if (createOutputMatrices && dtype != -1)
55 out_mat_gapi = cv::Mat (sz_in, dtype);
56 out_mat_ocv = cv::Mat (sz_in, dtype);
60 void initMatrixRandU(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)
62 in_mat1 = cv::Mat(sz_in, type);
64 sc = initScalarRandU(100);
66 cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
68 if (createOutputMatrices && dtype != -1)
70 out_mat_gapi = cv::Mat (sz_in, dtype);
71 out_mat_ocv = cv::Mat (sz_in, dtype);
75 void initMatsRandN(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)
77 in_mat1 = cv::Mat(sz_in, type);
78 cv::randn(in_mat1, cv::Scalar::all(127), cv::Scalar::all(40.f));
80 if (createOutputMatrices && dtype != -1)
82 out_mat_gapi = cv::Mat(sz_in, dtype);
83 out_mat_ocv = cv::Mat(sz_in, dtype);
87 static cv::Mat nonZeroPixels(const cv::Mat& mat)
89 int channels = mat.channels();
90 std::vector<cv::Mat> split(channels);
91 cv::split(mat, split);
93 for (int c=0; c < channels; c++)
96 result = split[c] != 0;
98 result = result | (split[c] != 0);
103 static int countNonZeroPixels(const cv::Mat& mat)
105 return cv::countNonZero( nonZeroPixels(mat) );
111 class TestParams: public TestFunctional, public TestWithParam<T>{};
114 class TestPerfParams: public TestFunctional, public perf::TestBaseWithParam<T>{};
116 using compare_f = std::function<bool(const cv::Mat &a, const cv::Mat &b)>;
118 using compare_scalar_f = std::function<bool(const cv::Scalar &a, const cv::Scalar &b)>;
124 compare_f to_compare_f()
126 T t = *static_cast<T*const>(this);
127 return [t](const cv::Mat &a, const cv::Mat &b)
135 struct WrappableScalar
137 compare_scalar_f to_compare_f()
139 T t = *static_cast<T*const>(this);
140 return [t](const cv::Scalar &a, const cv::Scalar &b)
148 class AbsExact : public Wrappable<AbsExact>
152 bool operator() (const cv::Mat& in1, const cv::Mat& in2) const
154 if (cv::norm(in1, in2, NORM_INF) != 0)
156 std::cout << "AbsExact error: G-API output and reference output matrixes are not bitexact equal." << std::endl;
167 class AbsTolerance : public Wrappable<AbsTolerance>
170 AbsTolerance(double tol) : _tol(tol) {}
171 bool operator() (const cv::Mat& in1, const cv::Mat& in2) const
173 if (cv::norm(in1, in2, NORM_INF) > _tol)
175 std::cout << "AbsTolerance error: Number of different pixels in " << std::endl;
176 std::cout << "G-API output and reference output matrixes exceeds " << _tol << " pixels threshold." << std::endl;
188 class Tolerance_FloatRel_IntAbs : public Wrappable<Tolerance_FloatRel_IntAbs>
191 Tolerance_FloatRel_IntAbs(double tol, double tol8u) : _tol(tol), _tol8u(tol8u) {}
192 bool operator() (const cv::Mat& in1, const cv::Mat& in2) const
194 int depth = CV_MAT_DEPTH(in1.type());
196 double err = depth >= CV_32F ? cv::norm(in1, in2, NORM_L1 | NORM_RELATIVE)
197 : cv::norm(in1, in2, NORM_INF);
198 double tolerance = depth >= CV_32F ? _tol : _tol8u;
201 std::cout << "Tolerance_FloatRel_IntAbs error: err=" << err
202 << " tolerance=" << tolerance
203 << " depth=" << cv::typeToString(depth) << std::endl;
218 class AbsSimilarPoints : public Wrappable<AbsSimilarPoints>
221 AbsSimilarPoints(double tol, double percent) : _tol(tol), _percent(percent) {}
222 bool operator() (const cv::Mat& in1, const cv::Mat& in2) const
225 cv::absdiff(in1, in2, diff);
226 Mat err_mask = diff > _tol;
227 int err_points = cv::countNonZero(err_mask.reshape(1));
228 double max_err_points = _percent * std::max((size_t)1000, in1.total());
229 if (err_points > max_err_points)
231 std::cout << "AbsSimilarPoints error: err_points=" << err_points
232 << " max_err_points=" << max_err_points << " (total=" << in1.total() << ")"
233 << " diff_tolerance=" << _tol << std::endl;
247 class ToleranceFilter : public Wrappable<ToleranceFilter>
250 ToleranceFilter(double tol, double tol8u, double inf_tol = 2.0) : _tol(tol), _tol8u(tol8u), _inf_tol(inf_tol) {}
251 bool operator() (const cv::Mat& in1, const cv::Mat& in2) const
253 int depth = CV_MAT_DEPTH(in1.type());
255 double err_Inf = cv::norm(in1, in2, NORM_INF);
256 if (err_Inf > _inf_tol)
258 std::cout << "ToleranceFilter error: err_Inf=" << err_Inf << " tolerance=" << _inf_tol << std::endl;
261 double err = cv::norm(in1, in2, NORM_L2 | NORM_RELATIVE);
262 double tolerance = depth >= CV_32F ? _tol : _tol8u;
265 std::cout << "ToleranceFilter error: err=" << err << " tolerance=" << tolerance
266 << " depth=" << cv::depthToString(depth)
279 class ToleranceColor : public Wrappable<ToleranceColor>
282 ToleranceColor(double tol, double inf_tol = 2.0) : _tol(tol), _inf_tol(inf_tol) {}
283 bool operator() (const cv::Mat& in1, const cv::Mat& in2) const
286 double err_Inf = cv::norm(in1, in2, NORM_INF);
287 if (err_Inf > _inf_tol)
289 std::cout << "ToleranceColor error: err_Inf=" << err_Inf << " tolerance=" << _inf_tol << std::endl;;
292 double err = cv::norm(in1, in2, NORM_L1 | NORM_RELATIVE);
295 std::cout << "ToleranceColor error: err=" << err << " tolerance=" << _tol << std::endl;;
306 class AbsToleranceScalar : public WrappableScalar<AbsToleranceScalar>
309 AbsToleranceScalar(double tol) : _tol(tol) {}
310 bool operator() (const cv::Scalar& in1, const cv::Scalar& in2) const
312 double abs_err = std::abs(in1[0] - in2[0]) / std::max(1.0, std::abs(in2[0]));
315 std::cout << "AbsToleranceScalar error: abs_err=" << abs_err << " tolerance=" << _tol << " in1[0]" << in1[0] << " in2[0]" << in2[0] << std::endl;;
327 } // namespace opencv_test
331 inline std::ostream& operator<<(std::ostream& os, const opencv_test::compare_f&)
333 return os << "compare_f";
339 inline std::ostream& operator<<(std::ostream& os, const opencv_test::compare_scalar_f&)
341 return os << "compare_scalar_f";