Path: blob/master/modules/gapi/test/gapi_sample_pipelines.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 <stdexcept>10#include <ade/util/iota_range.hpp>11#include "logger.hpp"1213namespace opencv_test14{1516namespace17{18G_TYPED_KERNEL(GInvalidResize, <GMat(GMat,Size,double,double,int)>, "org.opencv.test.invalid_resize")19{20static GMatDesc outMeta(GMatDesc in, Size, double, double, int) { return in; }21};2223GAPI_OCV_KERNEL(GOCVInvalidResize, GInvalidResize)24{25static void run(const cv::Mat& in, cv::Size sz, double fx, double fy, int interp, cv::Mat &out)26{27cv::resize(in, out, sz, fx, fy, interp);28}29};3031G_TYPED_KERNEL(GReallocatingCopy, <GMat(GMat)>, "org.opencv.test.reallocating_copy")32{33static GMatDesc outMeta(GMatDesc in) { return in; }34};3536GAPI_OCV_KERNEL(GOCVReallocatingCopy, GReallocatingCopy)37{38static void run(const cv::Mat& in, cv::Mat &out)39{40out = in.clone();41}42};43}4445TEST(GAPI_Pipeline, OverloadUnary_MatMat)46{47cv::GMat in;48cv::GComputation comp(in, cv::gapi::bitwise_not(in));4950cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);51cv::Mat ref_mat = ~in_mat;5253cv::Mat out_mat;54comp.apply(in_mat, out_mat);55EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));5657out_mat = cv::Mat();58auto cc = comp.compile(cv::descr_of(in_mat));59cc(in_mat, out_mat);60EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));61}6263TEST(GAPI_Pipeline, OverloadUnary_MatScalar)64{65cv::GMat in;66cv::GComputation comp(in, cv::gapi::sum(in));6768cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);69cv::Scalar ref_scl = cv::sum(in_mat);7071cv::Scalar out_scl;72comp.apply(in_mat, out_scl);73EXPECT_EQ(out_scl, ref_scl);7475out_scl = cv::Scalar();76auto cc = comp.compile(cv::descr_of(in_mat));77cc(in_mat, out_scl);78EXPECT_EQ(out_scl, ref_scl);79}8081TEST(GAPI_Pipeline, OverloadBinary_Mat)82{83cv::GMat a, b;84cv::GComputation comp(a, b, cv::gapi::add(a, b));8586cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);87cv::Mat ref_mat = (in_mat+in_mat);8889cv::Mat out_mat;90comp.apply(in_mat, in_mat, out_mat);91EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));9293out_mat = cv::Mat();94auto cc = comp.compile(cv::descr_of(in_mat), cv::descr_of(in_mat));95cc(in_mat, in_mat, out_mat);96EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));97}9899TEST(GAPI_Pipeline, OverloadBinary_Scalar)100{101cv::GMat a, b;102cv::GComputation comp(a, b, cv::gapi::sum(a + b));103104cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);105cv::Scalar ref_scl = cv::sum(in_mat+in_mat);106107cv::Scalar out_scl;108comp.apply(in_mat, in_mat, out_scl);109EXPECT_EQ(out_scl, ref_scl);110111out_scl = cv::Scalar();112auto cc = comp.compile(cv::descr_of(in_mat), cv::descr_of(in_mat));113cc(in_mat, in_mat, out_scl);114EXPECT_EQ(out_scl, ref_scl);115}116117TEST(GAPI_Pipeline, Sharpen)118{119const cv::Size sz_in (1280, 720);120const cv::Size sz_out( 640, 480);121cv::Mat in_mat (sz_in, CV_8UC3);122in_mat = cv::Scalar(128, 33, 53);123124cv::Mat out_mat(sz_out, CV_8UC3);125cv::Mat out_mat_y;126cv::Mat out_mat_ocv(sz_out, CV_8UC3);127128float sharpen_coeffs[] = {1290.0f, -1.f, 0.0f,130-1.0f, 5.f, -1.0f,1310.0f, -1.f, 0.0f132};133cv::Mat sharpen_kernel(3, 3, CV_32F, sharpen_coeffs);134135// G-API code //////////////////////////////////////////////////////////////136137cv::GMat in;138auto vga = cv::gapi::resize(in, sz_out);139auto yuv = cv::gapi::RGB2YUV(vga);140auto yuv_p = cv::gapi::split3(yuv);141auto y_sharp = cv::gapi::filter2D(std::get<0>(yuv_p), -1, sharpen_kernel);142auto yuv_new = cv::gapi::merge3(y_sharp, std::get<1>(yuv_p), std::get<2>(yuv_p));143auto out = cv::gapi::YUV2RGB(yuv_new);144145cv::GComputation c(cv::GIn(in), cv::GOut(y_sharp, out));146c.apply(cv::gin(in_mat), cv::gout(out_mat_y, out_mat));147148// OpenCV code /////////////////////////////////////////////////////////////149{150cv::Mat smaller;151cv::resize(in_mat, smaller, sz_out);152153cv::Mat yuv_mat;154cv::cvtColor(smaller, yuv_mat, cv::COLOR_RGB2YUV);155std::vector<cv::Mat> yuv_planar(3);156cv::split(yuv_mat, yuv_planar);157cv::filter2D(yuv_planar[0], yuv_planar[0], -1, sharpen_kernel);158cv::merge(yuv_planar, yuv_mat);159cv::cvtColor(yuv_mat, out_mat_ocv, cv::COLOR_YUV2RGB);160}161162// Comparison //////////////////////////////////////////////////////////////163{164cv::Mat diff = out_mat_ocv != out_mat;165std::vector<cv::Mat> diffBGR(3);166cv::split(diff, diffBGR);167EXPECT_EQ(0, cv::countNonZero(diffBGR[0]));168EXPECT_EQ(0, cv::countNonZero(diffBGR[1]));169EXPECT_EQ(0, cv::countNonZero(diffBGR[2]));170}171172// Metadata check /////////////////////////////////////////////////////////173{174auto cc = c.compile(cv::descr_of(in_mat));175auto metas = cc.outMetas();176ASSERT_EQ(2u, metas.size());177178auto out_y_meta = cv::util::get<cv::GMatDesc>(metas[0]);179auto out_meta = cv::util::get<cv::GMatDesc>(metas[1]);180181// Y-output182EXPECT_EQ(CV_8U, out_y_meta.depth);183EXPECT_EQ(1, out_y_meta.chan);184EXPECT_EQ(640, out_y_meta.size.width);185EXPECT_EQ(480, out_y_meta.size.height);186187// Final output188EXPECT_EQ(CV_8U, out_meta.depth);189EXPECT_EQ(3, out_meta.chan);190EXPECT_EQ(640, out_meta.size.width);191EXPECT_EQ(480, out_meta.size.height);192}193}194195TEST(GAPI_Pipeline, CustomRGB2YUV)196{197const cv::Size sz(1280, 720);198199// BEWARE:200//201// std::vector<cv::Mat> out_mats_cv(3, cv::Mat(sz, CV_8U))202//203// creates a vector of 3 elements pointing to the same Mat!204// FIXME: Make a G-API check for that205const int INS = 3;206std::vector<cv::Mat> in_mats(INS);207for (auto i : ade::util::iota(INS))208{209in_mats[i].create(sz, CV_8U);210cv::randu(in_mats[i], cv::Scalar::all(0), cv::Scalar::all(255));211}212213const int OUTS = 3;214std::vector<cv::Mat> out_mats_cv(OUTS);215std::vector<cv::Mat> out_mats_gapi(OUTS);216for (auto i : ade::util::iota(OUTS))217{218out_mats_cv [i].create(sz, CV_8U);219out_mats_gapi[i].create(sz, CV_8U);220}221222// G-API code //////////////////////////////////////////////////////////////223{224cv::GMat r, g, b;225cv::GMat y = 0.299f*r + 0.587f*g + 0.114f*b;226cv::GMat u = 0.492f*(b - y);227cv::GMat v = 0.877f*(r - y);228229cv::GComputation customCvt({r, g, b}, {y, u, v});230customCvt.apply(in_mats, out_mats_gapi);231}232233// OpenCV code /////////////////////////////////////////////////////////////234{235cv::Mat r = in_mats[0], g = in_mats[1], b = in_mats[2];236cv::Mat y = 0.299f*r + 0.587f*g + 0.114f*b;237cv::Mat u = 0.492f*(b - y);238cv::Mat v = 0.877f*(r - y);239240out_mats_cv[0] = y;241out_mats_cv[1] = u;242out_mats_cv[2] = v;243}244245// Comparison //////////////////////////////////////////////////////////////246{247const auto diff = [](cv::Mat m1, cv::Mat m2, int t) {248return cv::abs(m1-m2) > t;249};250251// FIXME: Not bit-accurate even now!252cv::Mat253diff_y = diff(out_mats_cv[0], out_mats_gapi[0], 2),254diff_u = diff(out_mats_cv[1], out_mats_gapi[1], 2),255diff_v = diff(out_mats_cv[2], out_mats_gapi[2], 2);256257EXPECT_EQ(0, cv::countNonZero(diff_y));258EXPECT_EQ(0, cv::countNonZero(diff_u));259EXPECT_EQ(0, cv::countNonZero(diff_v));260}261}262263TEST(GAPI_Pipeline, PipelineWithInvalidKernel)264{265cv::GMat in, out;266cv::Mat in_mat(500, 500, CV_8UC1), out_mat;267out = GInvalidResize::on(in, cv::Size(300, 300), 0.0, 0.0, cv::INTER_LINEAR);268269const auto pkg = cv::gapi::kernels<GOCVInvalidResize>();270cv::GComputation comp(cv::GIn(in), cv::GOut(out));271272EXPECT_THROW(comp.apply(in_mat, out_mat, cv::compile_args(pkg)), std::logic_error);273}274275TEST(GAPI_Pipeline, InvalidOutputComputation)276{277cv::GMat in1, out1, out2, out3;278279std::tie(out1, out2, out2) = cv::gapi::split3(in1);280cv::GComputation c({in1}, {out1, out2, out3});281cv::Mat in_mat;282cv::Mat out_mat1, out_mat2, out_mat3, out_mat4;283std::vector<cv::Mat> u_outs = {out_mat1, out_mat2, out_mat3, out_mat4};284std::vector<cv::Mat> u_ins = {in_mat};285286EXPECT_THROW(c.apply(u_ins, u_outs), std::logic_error);287}288289TEST(GAPI_Pipeline, PipelineAllocatingKernel)290{291cv::GMat in, out;292cv::Mat in_mat(500, 500, CV_8UC1), out_mat;293out = GReallocatingCopy::on(in);294295const auto pkg = cv::gapi::kernels<GOCVReallocatingCopy>();296cv::GComputation comp(cv::GIn(in), cv::GOut(out));297298EXPECT_THROW(comp.apply(in_mat, out_mat, cv::compile_args(pkg)), std::logic_error);299}300} // namespace opencv_test301302303