From 6032d86e237a4faa163bccfca4a309e4af78cc00 Mon Sep 17 00:00:00 2001 From: Philipp Hasper Date: Thu, 11 Jan 2018 13:27:16 +0100 Subject: [PATCH] Added getFontScaleFromHeight() --- modules/imgproc/include/opencv2/imgproc.hpp | 14 ++++++++++++++ modules/imgproc/src/drawing.cpp | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index 0d1ba52f58..bdb6e57f56 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -4730,6 +4730,20 @@ CV_EXPORTS_W Size getTextSize(const String& text, int fontFace, double fontScale, int thickness, CV_OUT int* baseLine); + +/** @brief Calculates the font-specific size to use to achieve a given height in pixels. + +@param fontFace Font to use, see cv::HersheyFonts. +@param pixelHeight Pixel height to compute the fontScale for +@param thickness Thickness of lines used to render the text.See putText for details. +@return The fontSize to use for cv::putText + +@see cv::putText +*/ +CV_EXPORTS_W double getFontScaleFromHeight(const int fontFace, + const int pixelHeight, + const int thickness = 1); + /** @brief Line iterator The class is used to iterate over all the pixels on the raster line diff --git a/modules/imgproc/src/drawing.cpp b/modules/imgproc/src/drawing.cpp index 380c4052a4..8338a90168 100644 --- a/modules/imgproc/src/drawing.cpp +++ b/modules/imgproc/src/drawing.cpp @@ -2364,6 +2364,17 @@ Size getTextSize( const String& text, int fontFace, double fontScale, int thickn return size; } +double getFontScaleFromHeight(const int fontFace, const int pixelHeight, const int thickness) +{ + // By https://stackoverflow.com/a/27898487/1531708 + const int* ascii = getFontData(fontFace); + + int base_line = (ascii[0] & 15); + int cap_line = (ascii[0] >> 4) & 15; + + return static_cast(pixelHeight - static_cast((thickness + 1)) / 2.0) / static_cast(cap_line + base_line); +} + }