Code covered by the BSD License  

Highlights from
Add Text To Image

image thumbnail

Add Text To Image

by

 

26 Mar 2013 (Updated )

Overlays rasterized text on an image matrix.

AddTextToImageDemo.m
% AddTextToImageDemo is a script to demonstrate what AddTextToImage does.
% It takes about 45 seconds to run on my desktop PC - most of that time is
% spent generating the fonts and displaying the images.
%
% Daniel Warren
% Particle Therapy Cancer Research Institute
% University of Oxford

Image = ones([500 500 3]);

Iterations = 200;

Strings = {'text','image'};

FontNames = {'Arial','Times New Roman','Comic Sans MS','Century Gothic',...
         'Script MT Bold','Courier New','OCR A Extended','Verdana',...
         'Gill Sans MT','Calibri','Cambria'};

FontSizes = 10:20:150;

k = 0;
Fonts = cell(1,length(FontNames)*length(FontSizes));
for i = 1:length(FontNames)
    for j = 1:length(FontSizes)
        k = k+1;
        Fonts{k} = BitmapFont(FontNames{i},FontSizes(j),unique([Strings{:}]));
    end
end

StringIndices = ceil(rand(Iterations,1)*length(Strings));
FontIndices = ceil(rand(Iterations,1)*length(Fonts));
Colors = hsv2rgb([rand(Iterations,1) 0.7+0.3*rand(Iterations,1) 0.9+0.1*rand(Iterations,1)]);
XPositions = ceil((-0.2+1.2*rand(1,Iterations))*size(Image,1));
YPositions = ceil((-0.1+1.1*rand(1,Iterations))*size(Image,2));

figure('Units','Pixels','Position',[50 50 50+size(Image,1) 50+size(Image,2)]);
handle = image(Image);
set(gca,'Units','Normalized','Position',[0 0 1 1]);
axis off image;
for i = 1:Iterations
    Image = AddTextToImage(Image,Strings{StringIndices(i)},[XPositions(i) YPositions(i)],Colors(i,:),Fonts{FontIndices(i)});
    set(handle,'CData',Image);
    drawnow;
end

Contact us