当前位置:首页 > articles > 正文内容

计算文本大小

Luz4周前 (07-20)articles128

Calculate text size

  • user3596335 问题:
    • 注意:这个问题https://stackoverflow.com/questions/32755439/how-to-put-text-into-a-bounding-box-in-opencv“>如何在OpenCV中将文本放入边界框?在某些方面与此类似,但这不是同一个问题。问题的运算试图将文本扩展到其图像的整个大小,而答案中获得标记的代码只是使用掩码调整文本的大小。
    • I'm using
      openCV
      combined with
      C++
      to do some image detection & manipulation.
    • 因此,我想在特定原点对齐长度未知的文本。应该计算字体比例,因为我想为最大文本宽度指定一个宽度因子,如下图所示:
    • “![在此处输入图像描述“src=”https://i.stack.imgur.com/2Ewj5.png“/
    • 这是我到目前为止得到的代码:
    • int fontFace = cv::FONT_HERSHEY_DUPLEX,    fontScale = myTextString.size() / 10;cv::Size textSize = getTextSize(image, fontFace, fontScale, 0, 0);putText(image, myTextString, cv::Point( (origin.x + textSize.width)/2, (origin.y + textSize.height)/2 ), fontFace, fontScale, Scalar(255, 0, 0));
  • 回答:
    • mainactual - vote: 4
      • 像这样的东西应该可以做到。您可以更改边距的计算方式,以更改字体的水平/垂直对齐方式。
      • If the height doesn't matter, you can just leave
        target.height
        a large number.
      • void drawtorect(cv::Mat & mat, cv::Rect target, int face, int thickness, cv::Scalar color, const std::string & str){    cv::Size rect = cv::getTextSize(str, face, 1.0, thickness, 0);    double scalex = (double)target.width / (double)rect.width;    double scaley = (double)target.height / (double)rect.height;    double scale = std::min(scalex, scaley);    int marginx = scale == scalex ? 0 : (int)((double)target.width * (scalex - scale) / scalex * 0.5);    int marginy = scale == scaley ? 0 : (int)((double)target.height * (scaley - scale) / scaley * 0.5);    cv::putText(mat, str, cv::Point(target.x + marginx, target.y + target.height - marginy), face, scale, color, thickness, 8, false);}
      • *编辑*
      • // Sample codeint L = 80; // width and height per squareint M = 60;cv::Mat m( 5*M, 7*L,CV_8UC3,cv::Scalar(0,0,0) );// create checkerboardfor ( int y=0,ymax=m.rows-M;y<=ymax; y+=M){    int c = (y/M)%2 == 0 ? 0 : 1;    for ( int x=0,xmax=m.cols-L;x<=xmax;x+=L)    {        if ( (c++)%2!=0 )            continue; // skip odd squares        // convenient way to do this        m( cv::Rect(x,y,L,M) ).setTo( cv::Scalar(64,64,64) );    }}// fill checkerboard ROIs by some textint64 id=1;for ( int y=0,ymax=m.rows-M;y<=ymax; y+=M){    for ( int x=0,xmax=m.cols-L;x<=xmax;x+=L)    {        std::stringstream ss;        ss<<(id<<=1); // some increasing text input        drawtorect( m, cv::Rect(x,y,L,M), cv::FONT_HERSHEY_PLAIN,1,cv::Scalar(255,255,255),ss.str() );    }}
      • “在此处输入图像描述”src=”https://i.stack.imgur.com/x7AK7.png“/

相关文章

使用nodejs和mongodb进行前端搜索

Frontend search with nodejs and mongodbCaleb 问题: 我今天刚开始学习mongodb,我有一段代码可以用来查询我的数据库和搜索地址:“Park Lane 38”。这是代码,这是结果:...

应该如何使用kotlin将findViewById传递数据从活动页面写入片段页面

How should write the findViewById pass data to Fragment page from Activity page using kotlinMIN LEE 问题: //这是我的活动页面覆盖...

C++:泛型&gt&gt;如何为一个通用函数定义多个模板解题,该函数充当静态实例创建者

C++:Generics >> How to define several template deklarations for a generic function, which serves as an static inst...

快速查看绿皮书等交易

SwiftUI View of Transactions like GreenBooksD-H 问题: 我想看看这样的交易。收款人应该在左上角。然后是收款人下的类别。然后,最右边是顶部的花费金额和底部的日期。然而,我能做到的最好的就...

C++:泛型&gt&gt;如何为一个通用函数定义多个模板解题,该函数充当静态实例创建者

C++:Generics >> How to define several template deklarations for a generic function, which serves as an static inst...

三js如何向FBX模型添加多个没有蒙皮的Mixamo动画?

Three.js how to add multiple Mixamo animations without skin to an FBX model?Anye 问题: I'm trying to create a game whe...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。