Path: blob/master/modules/gapi/test/gapi_fluid_roi_test.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 "gapi_fluid_test_kernels.hpp"1011namespace opencv_test12{1314using namespace cv::gapi_test_kernels;1516struct PartialComputation : public TestWithParam <std::tuple<cv::Rect>> {};17TEST_P(PartialComputation, Test)18{19cv::Rect roi;20std::tie(roi) = GetParam();2122int borderType = BORDER_REPLICATE;23int kernelSize = 3;24cv::Point anchor = {-1, -1};2526cv::GMat in;27cv::GMat out = TBlur3x3::on(in, borderType, {});28cv::GComputation c(cv::GIn(in), cv::GOut(out));2930const auto sz = cv::Size(8, 10);31cv::Mat in_mat(sz, CV_8UC1);32cv::Scalar mean = cv::Scalar(127.0f);33cv::Scalar stddev = cv::Scalar(40.f);34cv::randn(in_mat, mean, stddev);3536cv::Mat out_mat_gapi = cv::Mat::zeros(sz, CV_8UC1);37cv::Mat out_mat_ocv = cv::Mat::zeros(sz, CV_8UC1);3839// Run G-API40auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{to_own(roi)}}));41cc(cv::gin(in_mat), cv::gout(out_mat_gapi));4243// Check with OpenCV44if (roi == cv::Rect{}) roi = cv::Rect{0,0,sz.width,sz.height};45cv::blur(in_mat(roi), out_mat_ocv(roi), {kernelSize, kernelSize}, anchor, borderType);4647EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));48}4950INSTANTIATE_TEST_CASE_P(Fluid, PartialComputation,51Values(cv::Rect{}, cv::Rect{0,0,8,6}, cv::Rect{0,1,8,3},52cv::Rect{0,2,8,3}, cv::Rect{0,3,8,5}, cv::Rect{0,4,8,6}));5354struct PartialComputationAddC : public TestWithParam <std::tuple<cv::Rect>> {};55TEST_P(PartialComputationAddC, Test)56{57cv::Rect roi;58std::tie(roi) = GetParam();5960cv::GMat in;61cv::GMat out = TAddCSimple::on(in, 1);62cv::GComputation c(cv::GIn(in), cv::GOut(out));6364const auto sz = cv::Size(8, 10);65cv::Mat in_mat(sz, CV_8UC1);66cv::Scalar mean = cv::Scalar(127.0f);67cv::Scalar stddev = cv::Scalar(40.f);68cv::randn(in_mat, mean, stddev);6970cv::Mat out_mat_gapi = cv::Mat::zeros(sz, CV_8UC1);71cv::Mat out_mat_ocv = cv::Mat::zeros(sz, CV_8UC1);7273// Run G-API74auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{to_own(roi)}}));75cc(cv::gin(in_mat), cv::gout(out_mat_gapi));7677// Check with OpenCV78if (roi == cv::Rect{}) roi = cv::Rect{0,0,sz.width,sz.height};79out_mat_ocv(roi) = in_mat(roi) + 1;8081EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));82}8384INSTANTIATE_TEST_CASE_P(FluidRoi, PartialComputationAddC,85Values(cv::Rect{}, cv::Rect{0,0,8,6}, cv::Rect{0,1,8,3},86cv::Rect{0,2,8,3}, cv::Rect{0,3,8,5}, cv::Rect{0,4,8,6}));8788struct SequenceOfBlursRoiTest : public TestWithParam <std::tuple<int, cv::Rect>> {};89TEST_P(SequenceOfBlursRoiTest, Test)90{91cv::Size sz_in = { 320, 240 };9293int borderType = 0;94cv::Rect roi;95std::tie(borderType, roi) = GetParam();96cv::Mat in_mat(sz_in, CV_8UC1);97cv::Scalar mean = cv::Scalar(127.0f);98cv::Scalar stddev = cv::Scalar(40.f);99100cv::randn(in_mat, mean, stddev);101102cv::Point anchor = {-1, -1};103cv::Scalar borderValue(0);104105GMat in;106auto mid = TBlur3x3::on(in, borderType, borderValue);107auto out = TBlur5x5::on(mid, borderType, borderValue);108109Mat out_mat_gapi = Mat::zeros(sz_in, CV_8UC1);110111GComputation c(GIn(in), GOut(out));112auto cc = c.compile(descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{to_own(roi)}}));113cc(gin(in_mat), gout(out_mat_gapi));114115cv::Mat mid_mat_ocv = Mat::zeros(sz_in, CV_8UC1);116cv::Mat out_mat_ocv = Mat::zeros(sz_in, CV_8UC1);117118cv::blur(in_mat, mid_mat_ocv, {3,3}, anchor, borderType);119120if (roi == cv::Rect{})121{122roi = cv::Rect{0, 0, sz_in.width, sz_in.height};123}124125cv::blur(mid_mat_ocv(roi), out_mat_ocv(roi), {5,5}, anchor, borderType);126127EXPECT_EQ(0, countNonZero(out_mat_ocv != out_mat_gapi));128}129130INSTANTIATE_TEST_CASE_P(FluidRoi, SequenceOfBlursRoiTest,131Combine(Values(BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT_101),132Values(cv::Rect{0,0,320,240}, cv::Rect{0,64,320,128}, cv::Rect{0,128,320,112})));133134struct TwoBlursRoiTest : public TestWithParam <std::tuple<int, int, int, int, int, int, bool, cv::Rect>> {};135TEST_P(TwoBlursRoiTest, Test)136{137cv::Size sz_in = { 320, 240 };138139int kernelSize1 = 0, kernelSize2 = 0;140int borderType1 = -1, borderType2 = -1;141cv::Scalar borderValue1{}, borderValue2{};142bool readFromInput = false;143cv::Rect outRoi;144std::tie(kernelSize1, borderType1, borderValue1, kernelSize2, borderType2, borderValue2, readFromInput, outRoi) = GetParam();145cv::Mat in_mat(sz_in, CV_8UC1);146cv::Scalar mean = cv::Scalar(127.0f);147cv::Scalar stddev = cv::Scalar(40.f);148149cv::randn(in_mat, mean, stddev);150151cv::Point anchor = {-1, -1};152153auto blur1 = kernelSize1 == 3 ? &TBlur3x3::on : TBlur5x5::on;154auto blur2 = kernelSize2 == 3 ? &TBlur3x3::on : TBlur5x5::on;155156GMat in, out1, out2;157if (readFromInput)158{159out1 = blur1(in, borderType1, borderValue1);160out2 = blur2(in, borderType2, borderValue2);161}162else163{164auto mid = TAddCSimple::on(in, 0);165out1 = blur1(mid, borderType1, borderValue1);166out2 = blur2(mid, borderType2, borderValue2);167}168169Mat out_mat_gapi1 = Mat::zeros(sz_in, CV_8UC1);170Mat out_mat_gapi2 = Mat::zeros(sz_in, CV_8UC1);171172GComputation c(GIn(in), GOut(out1, out2));173auto cc = c.compile(descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{outRoi, outRoi}}));174cc(gin(in_mat), gout(out_mat_gapi1, out_mat_gapi2));175176cv::Mat out_mat_ocv1 = Mat::zeros(sz_in, CV_8UC1);177cv::Mat out_mat_ocv2 = Mat::zeros(sz_in, CV_8UC1);178179cv::blur(in_mat(outRoi), out_mat_ocv1(outRoi), {kernelSize1, kernelSize1}, anchor, borderType1);180cv::blur(in_mat(outRoi), out_mat_ocv2(outRoi), {kernelSize2, kernelSize2}, anchor, borderType2);181182EXPECT_EQ(0, countNonZero(out_mat_ocv1 != out_mat_gapi1));183EXPECT_EQ(0, countNonZero(out_mat_ocv2 != out_mat_gapi2));184}185186INSTANTIATE_TEST_CASE_P(FluidRoi, TwoBlursRoiTest,187Combine(Values(3, 5),188Values(cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT_101),189Values(0),190Values(3, 5),191Values(cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT_101),192Values(0),193testing::Bool(), // Read from input directly or place a copy node at start194Values(cv::Rect{0,0,320,240}, cv::Rect{0,64,320,128}, cv::Rect{0,128,320,112})));195196} // namespace opencv_test197198199