0
近日轉換,我在中國的文字措辭上的OCR,我想用的FreeType(2.3.5)收集字符的樣本,這裏是我的代碼:如何直角座標到圖像座標FreeType中
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;
IT IS NOT中心對準!!!
這個答案的鏈接已經死了。當我來這裏尋找Freetype信息時,可能需要編輯。 – Robinson