3

In the Matlab central (http://www.mathworks.com/matlabcentral/answers/26033-how-to-insert-text-into-image) I found this code:

Text = sprintf('Create text inserter object \nwith same property values');
H = vision.TextInserter(Text);
H.Color = [1.0 1.0 0];
H.FontSize = 20;
H.Location = [25 25];
I = im2double((imread('football.jpg')));
InsertedImage = step(H, I);
imshow(InsertedImage);

How can I do the same without using the computer vision system toolbox?

Thanks

Dima
  • 38,860
  • 14
  • 75
  • 115
hs100
  • 486
  • 6
  • 20
  • simply use a text annotation and it will work just fine. http://www.mathworks.com/help/matlab/ref/text.html – Benoit_11 Oct 15 '14 at 15:00

2 Answers2

4

To follow up on my comment:

A = imread('football.jpg');

YourText = sprintf('Create text inserter object \nwith same property values');

figure;

imshow(A);

hText = text(25,25,YourText,'Color',[1 1 0],'FontSize',20);

Giving the following:

enter image description here

Look at the doc page to see all the options available to modify/customize your text. Notice that you don't have to use sprintf to generate the string, however the newline character (\n) must be used inside sprintf in order to work.

EDIT In response to your comment below, if you need to save the image with text embedded in it you can use getframe to get the content of the figure and then imwrite to save it:

hFrame = getframe(gca) %// Get content of figure

imwrite(hFrame.cdata,'MyImage.tif','tif') %// Save the actual data, contained in the cdata property of hFrame

EDIT #2 As alternatives to using getframe, look here as there are 2 nice ideas proposed, i.e. using saveas or avi files.

Community
  • 1
  • 1
Benoit_11
  • 13,905
  • 2
  • 24
  • 35
1

The answer above (Benoit_11) requires that you display the image first, write the text, then save the image. This becomes very slow if you are processing the frames of a video (not a single image, or a few of them). To get a much faster processing time, you have to write directly to the image matrix. One alternative I can think of (but that is not very elegant) is to create (or download) small templates for the alphabet characters (e.g. 20x20), then overwrite the image matrix in the required region with those templates. For example, for an RGB "true color" image, we'll have something like this:

im(y:y+19,x:x+19,:)=template;

Hazem
  • 520
  • 5
  • 10