Code covered by the BSD License  

Highlights from
Add Text To Image


5.0 | 5 ratings Rate this file 81 Downloads (last 30 days) File Size: 7.72 KB File ID: #40959
image thumbnail

Add Text To Image



26 Mar 2013 (Updated )

Overlays rasterized text on an image matrix.

| Watch this File

File Information

Sometimes it's nice to be able to place text on an image and know precisely where it will render irrespective of the figure size and shape and other whims of the text() function. This submission achieves that by directly overwriting part of the image matrix with rasterized text generated using bitmapped fonts. It allows the use of any system font that text() will take. Text size, colour and position can also be controlled.

This code can undoubtedly be tweaked (or maybe just rewritten) to produce more aesthetically pleasing results. Feel free to build upon it!

Required Products MATLAB
MATLAB release MATLAB 7.14 (R2012a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
15 Apr 2014 Thai V. Hoang

Nice functions. Do you have any solution to smooth the text contour when font size is very high as 150?

25 Nov 2013 Jonathan  
14 Jul 2013 Dimitris Vayenas  
02 May 2013 Robert

Just what I needed, thanks. Suggested change to prevent text figure from being visible (lines from BitmapFont.m):

%fighandle = figure('Position',[50 50 150+Size 150+Size],'Units','pixels','Color',[1 1 1]); % original
fighandle = figure('Visible','Off','Position',[50 50 150+Size 150+Size],'Units','pixels','Color',[1 1 1]);

%Bitmap = getframe(gcf); % original
Bitmap = hardcopy(fighandle, path, '-dOpenGL', '-r0');

%Bitmap = mean(Bitmap.cdata,3); % original
Bitmap = mean(Bitmap,3);

17 Apr 2013 B Treeby

Nice function, exactly what I was looking for! I had to add a drawnow after the call to text in BitmapFont.m to prevent the same letter being captured several times by getFrame. It would also be nice to remove the reliance on the padarray function and the image processing toolbox.

14 Apr 2013 Daniel Warren

@Sav: That's not something I thought about, but how's about this?

Text = 'Test Text';
Position = [100 100];
FontName = 'Arial';
FontSize = 32;
Color = [1 1 0];
Border = 1; % thickness in pixels

Mask = AddTextToImage(false(size(Image(:,:,1))),Text,Position,1,FontName,FontSize);
Outline = logical(imdilate(Mask,ones(1+2*Border,1+2*Border))-Mask);
TextOnImage = AddTextToImage(Image,Text,Position,Color,FontName,FontSize);

for i = 1:3
tmp = TextOnImage(:,:,i);
tmp(Outline) = 0;
TextOnImage(:,:,i) = tmp;

12 Apr 2013 Sav Deb

Is it possible with this script to create an outlined text?

27 Mar 2013

Wrote a demonstration script to produce the screenshot, which revealed a vertical clipping bug when using large fonts - this has been fixed. Also avoided some error conditions when requested position is almost or completely outside the image.

18 Apr 2013

Included drawnow call in BitmapFont and removed dependency on Image Processing Toolbox as suggested by B Treeby. Also added a separate version which adds a border to the text with custom colour and thickness.

Contact us