From 8dd596b7ba68e70287f4a2590ab4721bd91de834 Mon Sep 17 00:00:00 2001 From: atalaman Date: Fri, 28 Jun 2019 17:11:03 +0300 Subject: [PATCH] Merge pull request #14892 from TolyaTalamanov:at/parameterized-render-tests G-API: Parameterized render tests (#14892) * Init commit * Add mat size as test parameter * Add test for text render * Add test for rect render * Add tests for line and circle * Remove old render tests * Init output mats * Remove methods input arguments * Add comment about data loss in BGR2NV12 conversion * Add edge test cases * Replace default color for out mats black -> white --- modules/gapi/src/api/render.cpp | 2 +- .../gapi/test/common/gapi_render_tests.cpp | 9 + .../gapi/test/common/gapi_render_tests.hpp | 73 ++++++ .../test/common/gapi_render_tests_inl.hpp | 96 ++++++++ .../gapi/test/cpu/gapi_render_tests_cpu.cpp | 66 +++++ modules/gapi/test/gapi_render_test.cpp | 227 ------------------ 6 files changed, 245 insertions(+), 228 deletions(-) create mode 100644 modules/gapi/test/common/gapi_render_tests.cpp create mode 100644 modules/gapi/test/common/gapi_render_tests.hpp create mode 100644 modules/gapi/test/common/gapi_render_tests_inl.hpp create mode 100644 modules/gapi/test/cpu/gapi_render_tests_cpu.cpp delete mode 100644 modules/gapi/test/gapi_render_test.cpp diff --git a/modules/gapi/src/api/render.cpp b/modules/gapi/src/api/render.cpp index dfc7d1a46d..b087c40cea 100644 --- a/modules/gapi/src/api/render.cpp +++ b/modules/gapi/src/api/render.cpp @@ -24,7 +24,7 @@ void cv::gapi::wip::draw::render(cv::Mat& bgr, const Prims& prims) { const auto& t_p = cv::util::get(p); cv::putText(bgr, t_p.text, t_p.org, t_p.ff, t_p.fs, - t_p.color, t_p.thick, t_p.bottom_left_origin); + t_p.color, t_p.thick, t_p.lt, t_p.bottom_left_origin); break; } diff --git a/modules/gapi/test/common/gapi_render_tests.cpp b/modules/gapi/test/common/gapi_render_tests.cpp new file mode 100644 index 0000000000..8e845e6f5a --- /dev/null +++ b/modules/gapi/test/common/gapi_render_tests.cpp @@ -0,0 +1,9 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. +// +// Copyright (C) 2018 Intel Corporation + + +#include "../test_precomp.hpp" +#include "gapi_render_tests_inl.hpp" diff --git a/modules/gapi/test/common/gapi_render_tests.hpp b/modules/gapi/test/common/gapi_render_tests.hpp new file mode 100644 index 0000000000..84df8c11f7 --- /dev/null +++ b/modules/gapi/test/common/gapi_render_tests.hpp @@ -0,0 +1,73 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. +// +// Copyright (C) 2018 Intel Corporation + + +#ifndef OPENCV_GAPI_RENDER_TESTS_HPP +#define OPENCV_GAPI_RENDER_TESTS_HPP + +#include "gapi_tests_common.hpp" +#include "api/render_priv.hpp" + +namespace opencv_test +{ + +using Points = std::vector; +using Rects = std::vector; +using PairOfPoints = std::pair; +using VecOfPairOfPoints = std::vector; + +template +class RenderWithParam : public TestWithParam +{ +protected: + void Init() + { + MatType type = CV_8UC3; + out_mat_ocv = cv::Mat(sz, type, cv::Scalar(255)); + out_mat_gapi = cv::Mat(sz, type, cv::Scalar(255)); + + if (isNV12Format) { + /* NB: When converting data from BGR to NV12, data loss occurs, + * so the reference data is subjected to the same transformation + * for correct comparison of the test results */ + cv::gapi::wip::draw::BGR2NV12(out_mat_ocv, y, uv); + cv::cvtColorTwoPlane(y, uv, out_mat_ocv, cv::COLOR_YUV2BGR_NV12); + } + } + + void Run() + { + if (isNV12Format) { + cv::gapi::wip::draw::BGR2NV12(out_mat_gapi, y, uv); + cv::gapi::wip::draw::render(y, uv, prims); + cv::cvtColorTwoPlane(y, uv, out_mat_gapi, cv::COLOR_YUV2BGR_NV12); + + // NB: Also due to data loss + cv::gapi::wip::draw::BGR2NV12(out_mat_ocv, y, uv); + cv::cvtColorTwoPlane(y, uv, out_mat_ocv, cv::COLOR_YUV2BGR_NV12); + } else { + cv::gapi::wip::draw::render(out_mat_gapi, prims); + } + } + + cv::Size sz; + cv::Scalar color; + int thick; + int lt; + bool isNV12Format; + std::vector prims; + cv::Mat y, uv; + cv::Mat out_mat_ocv, out_mat_gapi; +}; + +struct RenderTextTest : public RenderWithParam > {}; +struct RenderRectTest : public RenderWithParam > {}; +struct RenderCircleTest : public RenderWithParam > {}; +struct RenderLineTest : public RenderWithParam > {}; + +} // opencv_test + +#endif //OPENCV_GAPI_RENDER_TESTS_HPP diff --git a/modules/gapi/test/common/gapi_render_tests_inl.hpp b/modules/gapi/test/common/gapi_render_tests_inl.hpp new file mode 100644 index 0000000000..aded1ad4cb --- /dev/null +++ b/modules/gapi/test/common/gapi_render_tests_inl.hpp @@ -0,0 +1,96 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. +// +// Copyright (C) 2018 Intel Corporation + + +#ifndef OPENCV_GAPI_RENDER_TESTS_INL_HPP +#define OPENCV_GAPI_RENDER_TESTS_INL_HPP + +#include "gapi_render_tests.hpp" + +#include + +namespace opencv_test +{ + +TEST_P(RenderTextTest, AccuracyTest) +{ + std::vector points; + std::string text; + int ff; + double fs; + bool blo; + + std::tie(sz, text, points, ff, fs, color, thick, lt, blo, isNV12Format) = GetParam(); + Init(); + + for (const auto& p : points) { + cv::putText(out_mat_ocv, text, p, ff, fs, color, thick, lt, blo); + prims.emplace_back(cv::gapi::wip::draw::Text{text, p, ff, fs, color, thick, lt, blo}); + } + + Run(); + + EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv)); +} + +TEST_P(RenderRectTest, AccuracyTest) +{ + std::vector rects; + int shift; + + std::tie(sz, rects, color, thick, lt, shift, isNV12Format) = GetParam(); + Init(); + + for (const auto& r : rects) { + cv::rectangle(out_mat_ocv, r, color, thick, lt, shift); + prims.emplace_back(cv::gapi::wip::draw::Rect{r, color, thick, lt, shift}); + } + + Run(); + + EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv)); +} + +TEST_P(RenderCircleTest, AccuracyTest) +{ + std::vector points; + int radius; + int shift; + + std::tie(sz, points, radius, color, thick, lt, shift, isNV12Format) = GetParam(); + Init(); + + for (const auto& p : points) { + cv::circle(out_mat_ocv, p, radius, color, thick, lt, shift); + prims.emplace_back(cv::gapi::wip::draw::Circle{p, radius, color, thick, lt, shift}); + } + + Run(); + + EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv)); +} + +TEST_P(RenderLineTest, AccuracyTest) +{ + std::vector> points; + int shift; + + std::tie(sz, points, color, thick, lt, shift, isNV12Format) = GetParam(); + Init(); + + for (const auto& p : points) { + cv::line(out_mat_ocv, p.first, p.second, color, thick, lt, shift); + prims.emplace_back(cv::gapi::wip::draw::Line{p.first, p.second, color, thick, lt, shift}); + } + + Run(); + + EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv)); +} + +} // opencv_test + +#endif //OPENCV_GAPI_RENDER_TESTS_INL_HPP diff --git a/modules/gapi/test/cpu/gapi_render_tests_cpu.cpp b/modules/gapi/test/cpu/gapi_render_tests_cpu.cpp new file mode 100644 index 0000000000..334a9e5e13 --- /dev/null +++ b/modules/gapi/test/cpu/gapi_render_tests_cpu.cpp @@ -0,0 +1,66 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. +// +// Copyright (C) 2018 Intel Corporation + + +#include "../test_precomp.hpp" +#include "../common/gapi_render_tests.hpp" + +namespace opencv_test +{ + +INSTANTIATE_TEST_CASE_P(RenderTextTestCPU, RenderTextTest, + Combine(Values(cv::Size(1280, 720), + cv::Size(640, 480), + cv::Size(128, 128)), + Values("text"), + Values(Points{Point(5, 30), Point(40, 70), Point(-1, -1)}), +/* Font face */ Values(FONT_HERSHEY_SIMPLEX), +/* Font scale */ Values(2), +/* Color */ Values(cv::Scalar(255, 0, 0)), +/* Thickness */ Values(1), +/* Line type */ Values(LINE_8), +/* Bottom left origin */ testing::Bool(), +/* NV12 format or not */ testing::Bool())); + +INSTANTIATE_TEST_CASE_P(RenderRectTestCPU, RenderRectTest, + Combine(Values(cv::Size(1280, 720), + cv::Size(640, 480), + cv::Size(128, 128)), + Values(Rects{Rect(5, 30, 40, 50), + Rect(40, 70, 40, 50), +/* Edge case, rectangle will not be drawn */ Rect(75, 110, -40, 50), +/* Edge case, rectangle will not be drawn */ Rect(70, 100, 0, 50)}), +/* Color */ Values(cv::Scalar(255, 0, 0)), +/* Thickness */ Values(1), +/* Line type */ Values(LINE_8), +/* Shift */ Values(0), +/* NV12 format or not */ testing::Bool())); + +INSTANTIATE_TEST_CASE_P(RenderCircleTestCPU, RenderCircleTest, + Combine(Values(cv::Size(1280, 720), + cv::Size(640, 480), + cv::Size(128, 128)), + Values(Points{Point(5, 30), Point(40, 70), Point(75, 110)}), +/* Radius */ Values(5), +/* Color */ Values(cv::Scalar(255, 0, 0)), +/* Thickness */ Values(1), +/* Line type */ Values(LINE_8), +/* Shift */ Values(0), +/* NV12 format or not */ testing::Bool())); + +INSTANTIATE_TEST_CASE_P(RenderLineTestCPU, RenderLineTest, + Combine(Values(cv::Size(1280, 720), + cv::Size(640, 480), + cv::Size(128, 128)), + Values(VecOfPairOfPoints{ {Point(5, 30) , Point(5, 40) }, + {Point(40, 70) , Point(50, 70) }, + {Point(75, 110), Point(100, 115)} }), +/* Color */ Values(cv::Scalar(255, 0, 0)), +/* Thickness */ Values(1), +/* Line type */ Values(LINE_8), +/* Shift */ Values(0), +/* NV12 format or not */ testing::Bool())); +} diff --git a/modules/gapi/test/gapi_render_test.cpp b/modules/gapi/test/gapi_render_test.cpp deleted file mode 100644 index af4aa4db30..0000000000 --- a/modules/gapi/test/gapi_render_test.cpp +++ /dev/null @@ -1,227 +0,0 @@ -// This file is part of OpenCV project. -// It is subject to the license terms in the LICENSE file found in the top-level directory -// of this distribution and at http://opencv.org/license.html. -// -// Copyright (C) 2018 Intel Corporation - -#include "test_precomp.hpp" - -#include "api/render_priv.hpp" - -#include -#include -#include - -namespace opencv_test -{ - -namespace -{ - struct RenderTestFixture : public ::testing::Test - { - cv::Size size = {30, 40}; - int thick = 2; - int ff = cv::FONT_HERSHEY_SIMPLEX; - int lt = LINE_8; - double fs = 1; - int radius = 15; - int shift = 0; - - cv::Mat ref_mat {320, 480, CV_8UC3, cv::Scalar::all(255)}; - cv::Mat out_mat {320, 480, CV_8UC3, cv::Scalar::all(255)}; - cv::Scalar color {0, 255, 0}; - std::string text {"some text"}; - - }; -} // namespace - -TEST(BGR2NV12Test, CorrectConversion) -{ - cv::Mat in_mat(320, 240, CV_8UC3); - cv::Mat out_y, out_uv, ref_y, yuv; - cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar::all(255)); - - cv::cvtColor(in_mat, yuv, cv::COLOR_BGR2YUV); - cv::Mat channels[3]; - cv::split(yuv, channels); - - ref_y = channels[0]; - cv::Mat ref_uv(in_mat.size() / 2, CV_8UC2); - cv::resize(channels[1], channels[1], channels[1].size() / 2, 0, 0, cv::INTER_NEAREST); - cv::resize(channels[2], channels[2], channels[2].size() / 2, 0, 0, cv::INTER_NEAREST); - cv::merge(channels + 1, 2, ref_uv); - - cv::gapi::wip::draw::BGR2NV12(in_mat, out_y, out_uv); - - EXPECT_EQ(0, cv::countNonZero(out_y != ref_y)); - EXPECT_EQ(0, cv::countNonZero(out_uv != ref_uv)); -} - -TEST_F(RenderTestFixture, PutText) -{ - std::vector prims; - - for (int i = 0; i < 5; ++i) - { - cv::Point point {30 + i * 60, 40 + i * 50}; - - cv::putText(ref_mat, text, point, ff, fs, color, thick); - prims.emplace_back(cv::gapi::wip::draw::Text{text, point, ff, fs, color, thick, lt, false}); - } - - cv::gapi::wip::draw::render(out_mat, prims); - - EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat)); -} - -TEST_F(RenderTestFixture, Rectangle) -{ - std::vector prims; - - for (int i = 0; i < 5; ++i) - { - cv::Rect rect {30 + i * 60, 40 + i * 50, size.width, size.height}; - cv::rectangle(ref_mat, rect, color, thick, lt, shift); - prims.emplace_back(cv::gapi::wip::draw::Rect{rect, color, thick, lt, shift}); - } - - cv::gapi::wip::draw::render(out_mat, prims); - - EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat)); -} - -TEST_F(RenderTestFixture, Circle) -{ - std::vector prims; - - for (int i = 0; i < 5; ++i) - { - cv::Point center {30 + i * 60, 40 + i * 50}; - cv::circle(ref_mat, center, radius, color, thick, lt, shift); - prims.emplace_back(cv::gapi::wip::draw::Circle{center, radius, color, thick, lt, shift}); - } - - cv::gapi::wip::draw::render(out_mat, prims); - - EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat)); -} - -TEST_F(RenderTestFixture, Line) -{ - std::vector prims; - - for (int i = 0; i < 5; ++i) - { - cv::Point pt1{30 + i * 60 , 40 + i * 50}; - cv::Point pt2{30 + i * 60 + 40, 40 + i * 50}; - - cv::line(ref_mat, pt1, pt2, color, thick, lt, shift); - prims.emplace_back(cv::gapi::wip::draw::Line{pt1, pt2, color, thick, lt, shift}); - } - - cv::gapi::wip::draw::render(out_mat, prims); - - EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat)); -} - -TEST_F(RenderTestFixture, PutTextAndRectangle) -{ - std::vector prims; - - for (int i = 0; i < 5; ++i) - { - cv::Point point {30 + i * 60, 40 + i * 50}; - cv::Rect rect {point, size}; - - cv::rectangle(ref_mat, rect, color, thick); - cv::putText(ref_mat, text, point, ff, fs, color, thick); - - prims.emplace_back(cv::gapi::wip::draw::Rect{rect, color, thick, lt, shift}); - prims.emplace_back(cv::gapi::wip::draw::Text{text, point, ff, fs, color, thick, lt, false}); - } - - cv::gapi::wip::draw::render(out_mat, prims); - - EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat)); -} - -TEST_F(RenderTestFixture, PutTextAndRectangleNV12) -{ - cv::Mat y; - cv::Mat uv; - cv::gapi::wip::draw::BGR2NV12(out_mat, y, uv); - - std::vector prims; - - for (int i = 0; i < 5; ++i) - { - cv::Point point {30 + i * 60, 40 + i * 50}; - cv::Rect rect {point, size}; - - cv::rectangle(ref_mat, rect, color, thick); - cv::putText(ref_mat, text, point, ff, fs, color, thick); - - prims.emplace_back(cv::gapi::wip::draw::Rect{rect, color, thick, lt, shift}); - prims.emplace_back(cv::gapi::wip::draw::Text{text, point, ff, fs, color, thick, lt, false}); - } - - cv::gapi::wip::draw::render(y, uv, prims); - cv::cvtColorTwoPlane(y, uv, out_mat, cv::COLOR_YUV2BGR_NV12); - - cv::gapi::wip::draw::BGR2NV12(ref_mat, y, uv); - cv::cvtColorTwoPlane(y, uv, ref_mat, cv::COLOR_YUV2BGR_NV12); - - EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat)); -} - -TEST_F(RenderTestFixture, CircleNV12) -{ - cv::Mat y; - cv::Mat uv; - cv::gapi::wip::draw::BGR2NV12(out_mat, y, uv); - - std::vector prims; - - for (int i = 0; i < 5; ++i) - { - cv::Point center {30 + i * 60, 40 + i * 50}; - cv::circle(ref_mat, center, radius, color, thick, lt, shift); - prims.emplace_back(cv::gapi::wip::draw::Circle{center, radius, color, thick, lt, shift}); - } - - cv::gapi::wip::draw::render(y, uv, prims); - cv::cvtColorTwoPlane(y, uv, out_mat, cv::COLOR_YUV2BGR_NV12); - - cv::gapi::wip::draw::BGR2NV12(ref_mat, y, uv); - cv::cvtColorTwoPlane(y, uv, ref_mat, cv::COLOR_YUV2BGR_NV12); - - EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat)); -} - -TEST_F(RenderTestFixture, LineNV12) -{ - cv::Mat y; - cv::Mat uv; - cv::gapi::wip::draw::BGR2NV12(out_mat, y, uv); - - std::vector prims; - - for (int i = 0; i < 5; ++i) - { - cv::Point pt1{30 + i * 60 , 40 + i * 50}; - cv::Point pt2{30 + i * 60 + 40, 40 + i * 50}; - - cv::line(ref_mat, pt1, pt2, color, thick, lt, shift); - prims.emplace_back(cv::gapi::wip::draw::Line{pt1, pt2, color, thick, lt, shift}); - } - - cv::gapi::wip::draw::render(y, uv, prims); - cv::cvtColorTwoPlane(y, uv, out_mat, cv::COLOR_YUV2BGR_NV12); - - cv::gapi::wip::draw::BGR2NV12(ref_mat, y, uv); - cv::cvtColorTwoPlane(y, uv, ref_mat, cv::COLOR_YUV2BGR_NV12); - - EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat)); -} - -} // opencv_test