您好,我正在努力寻找一种方法来获得适合 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 中更改文本的比例,我认为这个解决方案是可行的(我不得不重新定义 g_HersheyGlyphs 和 getFontData,来自文件 drawing.cpp 中的 opencv 源)。
最佳答案
我的解决方案是使用以下函数:
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;
}
关于c++ - OpenCV 从尺寸中找到文本比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27482514/