1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

Merge pull request #15972 from TolyaTalamanov:at/ftext-primitive

This commit is contained in:
Alexander Alekhin
2019-12-03 20:09:21 +00:00
18 changed files with 648 additions and 39 deletions
@@ -132,6 +132,14 @@ struct Fixture : public RenderBGRTestBase API { \
GAPI_RENDER_TEST_FIXTURE_NV12(RenderNV12##Fixture, GET_VA_ARGS(API), Number, __VA_ARGS__) \
using Points = std::vector<cv::Point>;
GAPI_RENDER_TEST_FIXTURES(TestTexts, FIXTURE_API(std::string, cv::Point, double, cv::Scalar), 4, text, org, fs, color)
GAPI_RENDER_TEST_FIXTURES(TestRects, FIXTURE_API(cv::Rect, cv::Scalar, int), 3, rect, color, thick)
GAPI_RENDER_TEST_FIXTURES(TestCircles, FIXTURE_API(cv::Point, int, cv::Scalar, int), 4, center, radius, color, thick)
GAPI_RENDER_TEST_FIXTURES(TestLines, FIXTURE_API(cv::Point, cv::Point, cv::Scalar, int), 4, pt1, pt2, color, thick)
GAPI_RENDER_TEST_FIXTURES(TestMosaics, FIXTURE_API(cv::Rect, int, int), 3, mos, cellsz, decim)
GAPI_RENDER_TEST_FIXTURES(TestImages, FIXTURE_API(cv::Rect, cv::Scalar, double), 3, rect, color, transparency)
GAPI_RENDER_TEST_FIXTURES(TestPolylines, FIXTURE_API(Points, cv::Scalar, int), 3, points, color, thick)
} // opencv_test
#endif //OPENCV_GAPI_RENDER_TESTS_HPP
@@ -96,6 +96,7 @@ public:
in_mat2 = cv::Mat(sz_in, type);
sc = initScalarRandU(100);
cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
@@ -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
#include "../test_precomp.hpp"
#ifdef HAVE_FREETYPE
#include <random>
#include <opencv2/core/utils/configuration.private.hpp>
#include "api/ft_render.hpp"
namespace opencv_test
{
static std::string getFontPath()
{
static std::string path = cv::utils::getConfigurationParameterString("OPENCV_TEST_FREETYPE_FONT_PATH",
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc");
return path;
}
inline void RunTest(const std::string& font,
size_t num_iters,
size_t lower_char_code,
size_t upper_char_code)
{
cv::gapi::wip::draw::FTTextRender ftpr(font);
std::mt19937 gen{std::random_device()()};
std::uniform_int_distribution<int> dist(lower_char_code, upper_char_code);
std::uniform_int_distribution<int> dist_size(2, 200);
for (size_t i = 0; i < num_iters; ++i)
{
size_t text_size = dist_size(gen);
std::wstring text;
for (size_t j = 0; j < text_size; ++j)
{
wchar_t c = dist(gen);
text += c;
}
int fh = dist_size(gen);
int baseline = 0;
cv::Size size;
ASSERT_NO_THROW(size = ftpr.getTextSize(text, fh, &baseline));
cv::Mat bmp(size, CV_8UC1, cv::Scalar::all(0));
cv::Point org(0, bmp.rows - baseline);
ASSERT_NO_THROW(ftpr.putText(bmp, text, org, fh));
}
}
TEST(FTTextRenderTest, Smoke_Test_Ascii)
{
RunTest(getFontPath(), 2000, 32, 126);
}
TEST(FTTextRenderTest, Smoke_Test_Unicode)
{
RunTest(getFontPath(), 2000, 20320, 30000);
}
} // namespace opencv_test
#endif // HAVE_FREETYPE
@@ -5,6 +5,10 @@
// Copyright (C) 2018 Intel Corporation
#ifdef HAVE_FREETYPE
#include <codecvt>
#endif // HAVE_FREETYPE
#include "../test_precomp.hpp"
#include "../common/gapi_render_tests.hpp"
@@ -13,6 +17,10 @@
namespace opencv_test
{
#ifdef HAVE_FREETYPE
GAPI_RENDER_TEST_FIXTURES(OCVTestFTexts, FIXTURE_API(std::wstring, cv::Point, int, cv::Scalar), 4, text, org, fh, color)
#endif // HAVE_FREETYPE
GAPI_RENDER_TEST_FIXTURES(OCVTestTexts, FIXTURE_API(std::string, cv::Point, int, double, cv::Scalar, int, int, bool), 8, text, org, ff, fs, color, thick, lt, blo)
GAPI_RENDER_TEST_FIXTURES(OCVTestRects, FIXTURE_API(cv::Rect, cv::Scalar, int, int, int), 5, rect, color, thick, lt, shift)
GAPI_RENDER_TEST_FIXTURES(OCVTestCircles, FIXTURE_API(cv::Point, int, cv::Scalar, int, int, int), 6, center, radius, color, thick, lt, shift)
@@ -65,6 +73,54 @@ TEST_P(RenderNV12OCVTestTexts, AccuracyTest)
}
}
# ifdef HAVE_FREETYPE
TEST_P(RenderBGROCVTestFTexts, AccuracyTest)
{
// G-API code //////////////////////////////////////////////////////////////
cv::gapi::wip::draw::Prims prims;
prims.emplace_back(cv::gapi::wip::draw::FText{text, org, fh, color});
EXPECT_NO_THROW(cv::gapi::wip::draw::render(gapi_mat, prims,
cv::compile_args(cv::gapi::wip::draw::freetype_font{
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc"
})));
}
TEST_P(RenderNV12OCVTestFTexts, AccuracyTest)
{
// G-API code //////////////////////////////////////////////////////////////
cv::gapi::wip::draw::Prims prims;
prims.emplace_back(cv::gapi::wip::draw::FText{text, org, fh, color});
EXPECT_NO_THROW(cv::gapi::wip::draw::render(y_gapi_mat, uv_gapi_mat, prims,
cv::compile_args(cv::gapi::wip::draw::freetype_font{
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc"
})));
}
static std::wstring to_wstring(const char* bytes)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
return converter.from_bytes(bytes);
}
TEST(RenderFText, FontsNotPassedToCompileArgs)
{
cv::Mat in_mat(640, 480, CV_8UC3, cv::Scalar::all(0));
std::wstring text = to_wstring("\xe4\xbd\xa0\xe5\xa5\xbd");
cv::Point org(100, 100);
int fh = 60;
cv::Scalar color(200, 100, 25);
cv::gapi::wip::draw::Prims prims;
prims.emplace_back(cv::gapi::wip::draw::FText{text, org, fh, color});
EXPECT_ANY_THROW(cv::gapi::wip::draw::render(in_mat, prims));
}
#endif // HAVE_FREETYPE
TEST_P(RenderBGROCVTestRects, AccuracyTest)
{
// G-API code //////////////////////////////////////////////////////////////
@@ -418,6 +474,25 @@ INSTANTIATE_TEST_CASE_P(RenderNV12OCVTestTextsImpl, RenderNV12OCVTestTexts,
Values(LINE_8),
Values(false)));
#ifdef HAVE_FREETYPE
INSTANTIATE_TEST_CASE_P(RenderBGROCVTestFTextsImpl, RenderBGROCVTestFTexts,
Combine(Values(cv::Size(1280, 720)),
Values(to_wstring("\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c"),
to_wstring("\xe3\x80\xa4\xe3\x80\xa5\xe3\x80\xa6\xe3\x80\xa7\xe3\x80\xa8\xe3\x80\x85\xe3\x80\x86")),
Values(cv::Point(200, 200)),
Values(64),
Values(cv::Scalar(0, 255, 0))));
INSTANTIATE_TEST_CASE_P(RenderNV12OCVTestFTextsImpl, RenderNV12OCVTestFTexts,
Combine(Values(cv::Size(1280, 720)),
Values(to_wstring("\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c"),
to_wstring("\xe3\x80\xa4\xe3\x80\xa5\xe3\x80\xa6\xe3\x80\xa7\xe3\x80\xa8\xe3\x80\x85\xe3\x80\x86")),
Values(cv::Point(200, 200)),
Values(64),
Values(cv::Scalar(0, 255, 0))));
#endif // HAVE_FREETYPE
INSTANTIATE_TEST_CASE_P(RenderBGROCVTestMosaicsImpl, RenderBGROCVTestMosaics,
Combine(Values(cv::Size(1280, 720)),
Values(cv::Rect(100, 100, 200, 200)),