How to convert a ASCII character to matrix
17 ビュー (過去 30 日間)
古いコメントを表示
I am working on pattern recognition and need to have a dataset containing characters on a white background and size should be 8 by 8 pixels. The dirty and tedious solution is to create a matrix for each character manually like this:
A=[255,255,255,000,000,255,255,255;
255,255,000,255,255,000,255,255;
255,000,255,255,255,255,000,255;
000,255,255,255,255,255,255,000;
000,255,255,255,255,255,255,000;
000,000,000,000,000,000,000,000;
000,255,255,255,255,255,255,000;
000,255,255,255,255,255,255,000];
imshow(A);
it works but it takes ages to create a dataset with 1K-10K images. I tried to do so by using 'insertText' but it did not work properly as desired size of matrix is too small.
Briefly, I would like to use 'char' to create a database of all characters and symbols. How I can write a program to create like the above matrix automatically? Any help is really appreciated.
0 件のコメント
採用された回答
Guillaume
2016 年 7 月 20 日
編集済み: Guillaume
2016 年 7 月 20 日
I would like to use 'char' to create a database of all characters and symbols I don't think you understand what this means. A 'char' is simply a number that stands for a given character. In order to display that character in an image or on screen, the software (through the OS usually) must look up that character in a font, render it at the size specified and blend it with the background.
That is, there is no built-in database that says that number 65 (character 'A') should render as the idealised image you've specified. If you want that you've got no other choice than build that database yourself or find one that somebody else has made.
You can get adequate results with insertText but of course, it's never going to be the same as your idealised image:
%generate images for character 32 to 127 (ASCII):
imgs = arrayfun(@(c) rgb2gray(insertText(zeros(8, 8, 3), [5 6], c, ...
'Font', 'Lucida Console', ... You want a non-serif font. Experiment with that
'FontSize', 9, ... Experiment with that. size is not pixels
'AnchorPoint', 'Center', ...
'TextColor', [1 1 1], ...
'BoxOpacity', 0) ...
) > 0.45, .... You may want to experiment with the binarisation threshold as well
char(32:127), 'UniformOutput', false);
imshow([imgs{:}]); %display all images side by side
3 件のコメント
Guillaume
2016 年 7 月 25 日
It's up to you to decide which font to use depending on the look you want for the characters.
その他の回答 (2 件)
Walter Roberson
2016 年 7 月 20 日
編集済み: Walter Roberson
2016 年 7 月 20 日
Download an 8 pixel bitmapped font. Available ones include
... and others.
3 件のコメント
Guillaume
2016 年 7 月 25 日
A truetype font (TTF) does not contain a pixel matrix for characters. It contains instruction on how to draw the characters (in the form of paths).
Fonts (except for some very old formats) are vector graphics, not raster graphics. Therefore you need an intermediary (the OS, matlab's InsertText, etc.) to render the font as a bitmap, taking into accounts the font size you specify.
Walter Roberson
2016 年 7 月 25 日
参考
カテゴリ
Help Center および File Exchange で Tracking and Motion Estimation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!