How to convert a ASCII character to matrix
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
david jones
el 20 de Jul. de 2016
Editada: david jones
el 8 de Ag. de 2017
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 comentarios
Respuesta aceptada
Guillaume
el 20 de Jul. de 2016
Editada: Guillaume
el 20 de Jul. de 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 comentarios
Guillaume
el 25 de Jul. de 2016
It's up to you to decide which font to use depending on the look you want for the characters.
Más respuestas (2)
Thorsten
el 20 de Jul. de 2016
Editada: Thorsten
el 25 de Jul. de 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)
0 comentarios
Walter Roberson
el 20 de Jul. de 2016
Editada: Walter Roberson
el 20 de Jul. de 2016
Download an 8 pixel bitmapped font. Available ones include
... and others.
3 comentarios
Guillaume
el 25 de Jul. de 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.
Walter Roberson
el 25 de Jul. de 2016
Ver también
Categorías
Más información sobre Tracking and Motion Estimation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!