Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert the Cartesian coordinate to image coordinate in FreeType
    text
    copied!<p>Recently, I worded on OCR in Chinese characters, I wanna use <strong>FreeType(2.3.5)</strong> to collect the samples of character, here's my code :</p> <pre><code>FT_Library fontLibrary; FT_Face fontFace; int fontSize = 64; // Initialize FT_Init_FreeType(&amp;fontLibrary); FT_New_Face(fontLibrary, "C:\\Windows\\Fonts\\simhei.ttf", 0, &amp;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 &amp; glyphSlot = fontFace-&gt;glyph; FT_Bitmap charBitmap = glyphSlot-&gt;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 &lt; charHeight; y++) { int row = fontSize - glyphSlot-&gt;bitmap_top + y; for (int x = 0; x &lt; charWidth; x++) { int col = glyphSlot-&gt;bitmap_left + x; fontImage.at&lt;uchar&gt;(row, col) = charBuffer[y*charWidth + x]; } } imshow("Font Image", fontImage); waitKey(0); // Uninitialize FT_Done_Face(fontFace); FT_Done_FreeType(fontLibrary); </code></pre> <p>The problem is : the character is <strong>not center aligned in image</strong>, the coordinate of character image looks strange, in this example, the coordinate of <strong>'H'</strong> character is (fontSize = 64) :</p> <pre><code>bitmap_left = 3 bitmap_top = 44 bitmap.width = 26 bitmap.rows = 43 </code></pre> <p>then convert to the coordinate of image : </p> <pre><code>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; </code></pre> <p>so the margin in 4-direction is :</p> <pre><code>ROI.leftMargin = 3; ROI.rightMargin = 64 - 29 = 35; ROI.topMargin = 20; ROI.bottomMargin = 64 - 63 = 1; </code></pre> <p>IT IS NOT CENTER ALIGNED !!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload