您好我想要找到的是一种为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,fontFace,fontScale,thickness,& 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] != '\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;
}
现在像魔术一样找我,但也许有人比我更了解这段代码。
- 编辑2 -
现在我已经编写了函数getTextScalefromheight,它满足了我的要求:
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;
}
由于在opencv中无法更改文本的比例,因此我认为这个解决方案是好的(我必须从文件drawing.cpp中获取的opencv源代码中重新定义g_HersheyGlyphs和getFontData)。
答案 0 :(得分:1)
我的解决方案是使用以下功能:
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;
}