Path: blob/master/modules/gapi/test/gapi_scalar_tests.cpp
16337 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 "test_precomp.hpp"89#include <iostream>1011namespace opencv_test12{1314TEST(GAPI_Scalar, Argument)15{16cv::Size sz(2, 2);17cv::Mat in_mat(sz, CV_8U);1819cv::GComputationT<cv::GMat (cv::GMat, cv::GScalar)> mulS([](cv::GMat in, cv::GScalar c)20{21return in*c;22});2324cv::Mat out_mat(sz, CV_8U);25mulS.apply(in_mat, cv::Scalar(2), out_mat);2627cv::Mat reference = in_mat*2;28EXPECT_EQ(0, cv::countNonZero(cv::abs(out_mat - reference)));29}3031TEST(GAPI_Scalar, ReturnValue)32{33const cv::Size sz(2, 2);34cv::Mat in_mat(sz, CV_8U, cv::Scalar(1));3536cv::GComputationT<cv::GScalar (cv::GMat)> sum_of_sum([](cv::GMat in)37{38return cv::gapi::sum(in + in);39});4041cv::Scalar out;42sum_of_sum.apply(in_mat, out);4344EXPECT_EQ(8, out[0]);45}4647TEST(GAPI_Scalar, TmpScalar)48{49const cv::Size sz(2, 2);50cv::Mat in_mat(sz, CV_8U, cv::Scalar(1));5152cv::GComputationT<cv::GMat (cv::GMat)> mul_by_sum([](cv::GMat in)53{54return in * cv::gapi::sum(in);55});5657cv::Mat out_mat(sz, CV_8U);58mul_by_sum.apply(in_mat, out_mat);5960cv::Mat reference = cv::Mat(sz, CV_8U, cv::Scalar(4));61EXPECT_EQ(0, cv::countNonZero(cv::abs(out_mat - reference)));62}6364TEST(GAPI_ScalarWithValue, Simple_Arithmetic_Pipeline)65{66GMat in;67GMat out = (in + 1) * 2;68cv::GComputation comp(in, out);6970cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1);71cv::Mat ref_mat, out_mat;7273ref_mat = (in_mat + 1) * 2;74comp.apply(in_mat, out_mat);7576EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));77}7879TEST(GAPI_ScalarWithValue, GScalar_Initilization)80{81cv::Scalar sc(2);82cv::GMat in;83cv::GScalar s(sc);84cv::GComputation comp(in, cv::gapi::mulC(in, s));8586cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1);87cv::Mat ref_mat, out_mat;88cv::multiply(in_mat, sc, ref_mat, 1, CV_8UC1);89comp.apply(cv::gin(in_mat), cv::gout(out_mat));9091EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));92}9394TEST(GAPI_ScalarWithValue, Constant_GScalar_In_Middle_Graph)95{96cv::Scalar sc(5);97cv::GMat in1;98cv::GScalar in2;99cv::GScalar s(sc);100101auto add_out = cv::gapi::addC(in1, in2);102cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(cv::gapi::mulC(add_out, s)));103104cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1);105cv::Scalar in_scalar(3);106107cv::Mat ref_mat, out_mat, add_mat;108cv::add(in_mat, in_scalar, add_mat);109cv::multiply(add_mat, sc, ref_mat, 1, CV_8UC1);110comp.apply(cv::gin(in_mat, in_scalar), cv::gout(out_mat));111112EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));113}114115} // namespace opencv_test116117118