cl-projects.de
 
Bauteile-Verwaltung part-db
Tools
Laminator-Umbau

Verschiedenes
miscellaneous stuff

Simple Font Library for MATLAB

Introduction

Having to annotate camera images with timestamps and frame numbers, I was in need of code to add the text into the image matrix. Searching the web, I found only complex solutions, some of them being part of toolboxes. As you may know, MATLAB toolbox licenses can be quite expensive, and when you're using MATLAB in a floating-license network environment, then the toolbox you need may not be available when you need it. I didn't require features like true-type font support, instead I consider true-type fonts to be annoying if you add things like a frame number to the frames in a movie. Therefore, I decided to do my own solution.

Character Matrix

The character matrix are at the heart of any fixed-width font face. All characters have identical dimensions and they are described by a matrix of dots.

As I wanted to have a command line appearance, I obtained the font from my Linux distribution. The character matrices were obtained with a conversion script from file cp850-8x16.psf shipped in the Ubuntu Xenial package 'console-data', version 1.12. This is font with character width 8 and character height 16. When programming in C with performance in mind, I would go for a binary representation of the matrix (as in the Linux PSF files). In contrast to this, I decided to use clear text representation of the matrices. Accordingly, the file with 256 chars, each with height=16 and width=8 has a total size of 256*16*(8+1)=36864 bytes. (This calculation includes the newline character at the end of each line.)

For example, this is the "@" character in the 16x8 charset that I used:

     *12345678*
01   |        |
02   |        |
03   |        |
04   | XXXXX  |
05   |XX   XX |
06   |XX   XX |
07   |XX XXXX |
08   |XX XXXX |
09   |XX XXXX |
10   |XX XXX  |
11   |XX      |
12   | XXXXX  |
13   |        |
14   |        |
15   |        |
16   |        |
     *--------*
Here, X marks the pixels that are filled with foreground color, while those with whitespace are background color.

A Simple Example

clear all;

q=font_matrix_load();
m=font_string_to_matrix(q,'Hallo World');

figure;
imagesc(m');
First, this loads the font information in variable q. Then, the string "Hallo World" is converted into a bitmap matrix. This matrix is finally displayed using imagesc.

Using imwrite(m','hallo.png');, I stored this matrix as PNG file. It was subsequently scaled by a factor of four with image processing software. This is the (scaled) result:

A limitation of the current version of the program is that one can only convert a single line of text into a pixel matrix.

If you plan to display text into greyscale images that use a colormap, as it is often the case in scientific applications, then the following procedure proved to be useful:

  • Convert the greyscale image data into RGB images using the MATLAB function ind2rgb. Calling this function, you have to specify the greyscale data as well as the colormap. It returns a 3-D matrix object.
  • Then you can apply the matrix generated by font_string_to_matrix to each of the color sub-channels: red, green, and blue.
  • The result is then exported to disk as image file. Here I used imwrite for convenience, however, you can use whatever function you prefer.

Summary

Pros:
  • No need to use expensive toolboxes for this basic functionality
  • Ships all needed resources, such as fonts
  • Result looks the same on Linux, MacOS, and Windows
Cons:
  • So far, only a single line of text can be converted into a pixel matrix
  • Code not optimized, for historical reasons you may have to transpose the pixel matrix to rotate image by 90 degrees

Downloads

All sources are released under the GNU General Public License.

Impressum