加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – OpenCV从大小中找到文本缩放

发布时间:2020-12-16 03:12:23 所属栏目:百科 来源:网络整理
导读:您好,我想要找到的是一种获得ROI的正确文本比例的方法.比例值也应该从插入文本的大小来控制. 简单来说,我想要找到的是函数“getTextScale”或类似的东西: std::String text = "Huhu";int fontface = cv::FONT_HERSHEY_PLAIN;int thickness = 2;int baseline
您好,我想要找到的是一种获得ROI的正确文本比例的方法.比例值也应该从插入文本的大小来控制.

简单来说,我想要找到的是函数“getTextScale”或类似的东西:

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);

在这个计算之后cv :: getTextSize(text,fontScale,& baseline);将检索与sz类似的值.有opencv这个功能吗?

– 编辑 –

我发现opencv中的cv :: getTextSize如下所示:

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] != ''; 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;
}

现在看起来像魔术,但也许有人比我更了解这个代码.

–EDIT 2–

现在我已经写了函数getTextScalefromheight它满足我的要求:

double getTextScalefromheight(int fontFace,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;

}

由于文本的比例在opencv中无法改变,这个解决方案在我看来很好(我不得不重新定义g_HersheyGlyphs和getFontData从opencv源文件draw.cpp).

解决方法

我的解决方案是使用以下功能:
double getTextScalefromheight(int fontFace,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;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读