Path: blob/master/modules/gapi/test/common/gapi_tests_common.hpp
16339 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3//4// Copyright (C) 2018 Intel Corporation567#include <iostream>89#include "opencv2/ts.hpp"10#include "opencv2/gapi.hpp"1112namespace13{14inline std::ostream& operator<<(std::ostream& o, const cv::GCompileArg& arg)15{16return o << (arg.tag.empty() ? "empty" : arg.tag);17}18}1920namespace opencv_test21{2223class TestFunctional24{25public:26cv::Mat in_mat1;27cv::Mat in_mat2;28cv::Mat out_mat_gapi;29cv::Mat out_mat_ocv;3031cv::Scalar sc;3233cv::Scalar initScalarRandU(unsigned upper)34{35auto& rng = cv::theRNG();36double s1 = rng(upper);37double s2 = rng(upper);38double s3 = rng(upper);39double s4 = rng(upper);40return cv::Scalar(s1, s2, s3, s4);41}4243void initMatsRandU(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)44{45in_mat1 = cv::Mat(sz_in, type);46in_mat2 = cv::Mat(sz_in, type);4748sc = initScalarRandU(100);49cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));50cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));5152if (createOutputMatrices && dtype != -1)53{54out_mat_gapi = cv::Mat (sz_in, dtype);55out_mat_ocv = cv::Mat (sz_in, dtype);56}57}5859void initMatrixRandU(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)60{61in_mat1 = cv::Mat(sz_in, type);6263sc = initScalarRandU(100);6465cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));6667if (createOutputMatrices && dtype != -1)68{69out_mat_gapi = cv::Mat (sz_in, dtype);70out_mat_ocv = cv::Mat (sz_in, dtype);71}72}7374void initMatsRandN(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)75{76in_mat1 = cv::Mat(sz_in, type);77cv::randn(in_mat1, cv::Scalar::all(127), cv::Scalar::all(40.f));7879if (createOutputMatrices && dtype != -1)80{81out_mat_gapi = cv::Mat(sz_in, dtype);82out_mat_ocv = cv::Mat(sz_in, dtype);83}84}8586static cv::Mat nonZeroPixels(const cv::Mat& mat)87{88int channels = mat.channels();89std::vector<cv::Mat> split(channels);90cv::split(mat, split);91cv::Mat result;92for (int c=0; c < channels; c++)93{94if (c == 0)95result = split[c] != 0;96else97result = result | (split[c] != 0);98}99return result;100}101102static int countNonZeroPixels(const cv::Mat& mat)103{104return cv::countNonZero( nonZeroPixels(mat) );105}106107};108109template<class T>110class TestParams: public TestFunctional, public TestWithParam<T>{};111112template<class T>113class TestPerfParams: public TestFunctional, public perf::TestBaseWithParam<T>{};114115using compare_f = std::function<bool(const cv::Mat &a, const cv::Mat &b)>;116117template<typename T>118struct Wrappable119{120compare_f to_compare_f()121{122T t = *static_cast<T*const>(this);123return [t](const cv::Mat &a, const cv::Mat &b)124{125return t(a, b);126};127}128};129130}131132133