Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV find the text Scale from a size

Tags:

c++

opencv

Hello what I am trying to find is a way to get the right text scale for a ROI. The scale value should be also controlled from the size where the text will be inserted.

In easy words what I am trying to find is the function "getTextScale" or something like this:

std::String text = "Huhu";
int fontface = cv::FONT_HERSHEY_PLAIN;
int thickness = 2;
int baseline = 0;
cv::Size sz(500,200);
double fontScale = cv::getTextScale(text, fontFace, thickness, &baseline,sz);

After this calculation cv::getTextSize(text, fontFace, fontScale, thickness, &baseline); would retrieve similar values as sz. Has opencv this feature ?

--EDIT--

I have found out that the cv::getTextSize in opencv looks as follows:

Size getTextSize( const string& text, int fontFace, double fontScale, int thickness, int* _base_line)
{
    Size size;
    double view_x = 0;
    const char **faces = cv::g_HersheyGlyphs;
    const int* ascii = getFontData(fontFace);

    int base_line = (ascii[0] & 15);
    int cap_line = (ascii[0] >> 4) & 15;
    size.height = cvRound((cap_line + base_line)*fontScale + (thickness+1)/2);

    for( int i = 0; text[i] != '\0'; i++ )
    {
        int c = (uchar)text[i];
        Point p;

        if( c >= 127 || c < ' ' )
            c = '?';

        const char* ptr = faces[ascii[(c-' ')+1]];
        p.x = (uchar)ptr[0] - 'R';
        p.y = (uchar)ptr[1] - 'R';
        view_x += (p.y - p.x)*fontScale;
    }

    size.width = cvRound(view_x + thickness);
    if( _base_line )
        *_base_line = cvRound(base_line*fontScale + thickness*0.5);
    return size;
}

Looks for me now like magic but maybe someone understands this code better than me.

--EDIT 2--

Now I have written the function getTextScalefromheight and it satisfies my requirements:

double getTextScalefromheight(int fontFace, int thickness, int height)
{

    Size size;
    double view_x = 0;
    const char **faces = g_HersheyGlyphs;
    const int* ascii = getFontData(fontFace);

    int base_line = (ascii[0] & 15);
    int cap_line = (ascii[0] >> 4) & 15;

    double fontScale = static_cast<double>(height - static_cast<double>((thickness + 1)) / 2.0) / static_cast<double>(cap_line + base_line);

    return fontScale;

}

As the proportion of the text can not be changed in opencv is this solution in my opinion ok (I had to redifine g_HersheyGlyphs and getFontData from opencv sources taken from file drawing.cpp).

like image 318
jamk Avatar asked Dec 15 '14 10:12

jamk


1 Answers

My Solution is to use the following function:

double getTextScalefromheight(int fontFace, int thickness, int height)
{

    Size size;
    double view_x = 0;
    const char **faces = g_HersheyGlyphs;
    const int* ascii = getFontData(fontFace);

    int base_line = (ascii[0] & 15);
    int cap_line = (ascii[0] >> 4) & 15;

    double fontScale = static_cast<double>(height - static_cast<double>((thickness + 1)) / 2.0) / static_cast<double>(cap_line + base_line);

    return fontScale;

}
like image 188
jamk Avatar answered Nov 15 '22 14:11

jamk