How to convert a ASCII character to matrix

16 views (last 30 days)
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.

Accepted Answer

Guillaume
Guillaume on 20 Jul 2016
Edited: Guillaume on 20 Jul 2016
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 Comments
david jones
david jones on 25 Jul 2016
Edited: david jones on 25 Jul 2016
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 on 25 Jul 2016
It's up to you to decide which font to use depending on the look you want for the characters.

Sign in to comment.

More Answers (2)

Thorsten
Thorsten on 20 Jul 2016
Edited: Thorsten on 25 Jul 2016
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 on 20 Jul 2016
Edited: Walter Roberson on 20 Jul 2016
  3 Comments
Guillaume
Guillaume on 25 Jul 2016
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!