Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / opencv_test_gapi / common / gapi_tests_common.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <iostream>
8
9 #include "opencv2/core.hpp"
10 #include "opencv2/gapi/cpu/core.hpp"
11
12 #include <gtest/gtest.h>
13
14 namespace
15 {
16     std::ostream& operator<<(std::ostream& o, const cv::GCompileArg& arg)
17     {
18         return o << (arg.tag.empty() ? "empty" : arg.tag);
19     }
20 }
21
22 namespace opencv_test
23 {
24
25 class TestFunctional
26 {
27 public:
28     cv::Mat in_mat1;
29     cv::Mat in_mat2;
30     cv::Mat out_mat_gapi;
31     cv::Mat out_mat_ocv;
32
33     cv::Scalar sc;
34
35     void initMatsRandU(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)
36     {
37         in_mat1 = cv::Mat(sz_in, type);
38         in_mat2 = cv::Mat(sz_in, type);
39
40         auto& rng = cv::theRNG();
41         sc = cv::Scalar(rng(100),rng(100),rng(100),rng(100));
42         cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
43         cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
44
45         if (createOutputMatrices && dtype != -1)
46         {
47             out_mat_gapi = cv::Mat (sz_in, dtype);
48             out_mat_ocv = cv::Mat (sz_in, dtype);
49         }
50     }
51
52     void initMatrixRandU(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)
53     {
54         in_mat1 = cv::Mat(sz_in, type);
55
56         auto& rng = cv::theRNG();
57         sc = cv::Scalar(rng(100),rng(100),rng(100),rng(100));
58
59         cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
60
61         if (createOutputMatrices && dtype != -1)
62         {
63             out_mat_gapi = cv::Mat (sz_in, dtype);
64             out_mat_ocv = cv::Mat (sz_in, dtype);
65         }
66     }
67
68     void initMatsRandN(int type, cv::Size sz_in, int dtype, bool createOutputMatrices = true)
69     {
70         in_mat1  = cv::Mat(sz_in, type);
71         cv::randn(in_mat1, cv::Scalar::all(127), cv::Scalar::all(40.f));
72
73         if (createOutputMatrices  && dtype != -1)
74         {
75             out_mat_gapi = cv::Mat(sz_in, dtype);
76             out_mat_ocv = cv::Mat(sz_in, dtype);
77         }
78     }
79
80     static cv::Mat nonZeroPixels(const cv::Mat& mat)
81     {
82         int channels = mat.channels();
83         std::vector<cv::Mat> split(channels);
84         cv::split(mat, split);
85         cv::Mat result;
86         for (int c=0; c < channels; c++)
87         {
88             if (c == 0)
89                 result = split[c] != 0;
90             else
91                 result = result | (split[c] != 0);
92         }
93         return result;
94     }
95
96     static int countNonZeroPixels(const cv::Mat& mat)
97     {
98         return cv::countNonZero( nonZeroPixels(mat) );
99     }
100
101 };
102
103 template<class T>
104 class TestParams: public TestFunctional, public testing::TestWithParam<T>{};
105
106 }