Image 如何在FreeType中将笛卡尔坐标转换为图像坐标

Image 如何在FreeType中将笛卡尔坐标转换为图像坐标,image,opencv,fonts,coordinate,freetype,Image,Opencv,Fonts,Coordinate,Freetype,最近,我用汉字在OCR上写了字,我想用FreeType2.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 F

最近,我用汉字在OCR上写了字,我想用FreeType2.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);
然后转换为图像的坐标:

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;

它不是中心对齐的

我自己解决了这个问题,图像的左上坐标存储在glyphSlot中,下面是博客:

此答案的链接现在已失效。也许编辑是必要的,因为我来这里寻找自由类型的信息。
ROI.leftMargin = 3;
ROI.rightMargin = 64 - 29 = 35;
ROI.topMargin = 20;
ROI.bottomMargin = 64 - 63 = 1;