How to convert a ASCII character to matrix

17 ビュー (過去 30 日間)
david jones
david jones 2016 年 7 月 20 日
編集済み: david jones 2017 年 8 月 8 日
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.

採用された回答

Guillaume
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 件のコメント
david jones
david jones 2016 年 7 月 25 日
編集済み: david jones 2016 年 7 月 25 日
Thanks, you method works and now I have at least some data. How can I find out which font should I used? I tried to install 8-bit font suggested by "Walter Roberson" Some of them work and some of them not.
Guillaume
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 件)

Thorsten
Thorsten 2016 年 7 月 20 日
編集済み: Thorsten 2016 年 7 月 25 日
There are datasets around; google 8 x 8 character bitmaps
To extract a matrix from the hex numbers, you can use
% character 'n'
n = dec2bin(hex2dec(['00'; '00'; '1F'; '33'; '33'; '33'; '33'; '00']));
M = 1 - (n - '0');
imshow(M)

Walter Roberson
Walter Roberson 2016 年 7 月 20 日
編集済み: Walter Roberson 2016 年 7 月 20 日
  3 件のコメント
Guillaume
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTracking and Motion Estimation についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by