[英]How to convert the Cartesian coordinate to image coordinate in FreeType
最近,我用漢字寫OCR字樣,我想使用FreeType(2.3.5)收集字符樣本,這是我的代碼:
FT_Library fontLibrary;
FT_Face fontFace;
int fontSize = 64;
// Initialize
FT_Init_FreeType(&fontLibrary);
FT_New_Face(fontLibrary, "C:\\Windows\\Fonts\\simhei.ttf", 0, &fontFace);
// Setup
FT_Select_Charmap(fontFace, FT_ENCODING_UNICODE);
FT_Set_Pixel_Sizes(fontFace, fontSize, 0);
FT_Load_Char(fontFace, 'H', FT_LOAD_RENDER);
// Retrieve data
FT_GlyphSlot & glyphSlot = fontFace->glyph;
FT_Bitmap charBitmap = glyphSlot->bitmap;
int charWidth = charBitmap.width;
int charHeight = charBitmap.rows;
unsigned char* charBuffer = charBitmap.buffer;
// Construct image
Mat fontImage(fontSize, fontSize, CV_8UC1);
fontImage = Scalar::all(0);
for (int y = 0; y < charHeight; y++)
{
int row = fontSize - glyphSlot->bitmap_top + y;
for (int x = 0; x < charWidth; x++)
{
int col = glyphSlot->bitmap_left + x;
fontImage.at<uchar>(row, col) = charBuffer[y*charWidth + x];
}
}
imshow("Font Image", fontImage);
waitKey(0);
// Uninitialize
FT_Done_Face(fontFace);
FT_Done_FreeType(fontLibrary);
問題是:字符在圖像中未居中對齊 ,字符圖像的坐標看起來很奇怪,在此示例中, “ H”字符的坐標為(fontSize = 64):
bitmap_left = 3
bitmap_top = 44
bitmap.width = 26
bitmap.rows = 43
然后轉換為圖像的坐標:
ROI.left = bitmap_left = 3;
ROI.right = bitmap_left + bitmap.width = 29;
ROI.top = fontSize - bitmap_top = 20;
ROI.bottom = fontSize - bitmap_top + bitmap.rows = 63;
因此4方向的邊距為:
ROI.leftMargin = 3;
ROI.rightMargin = 64 - 29 = 35;
ROI.topMargin = 20;
ROI.bottomMargin = 64 - 63 = 1;
它沒有中心對齊!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.