您好,我想要找到的是一种获得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] != '\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; }
现在看起来像魔术,但也许有人比我更了解这个代码.
–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; }