Search code examples
matlabmatlab-cvst

insert text into image without computer vision system toolbox - Matlab


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


Solution

  • 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.