MATLAB: How to add text to an image and make the text become part of the image within MATLAB

imageImage Processing Toolboxininserttext;

I would like to add a few characters to an image. I want the characters to be part of the image, not a text object.

Best Answer

If you have the Computer Vision System Toolbox, you can use the "insertText" function:
For example:
I = imread('peppers.png');
J = insertText(I, [100 315 ], 'Peppers are good for you!');
imshow(J);
"insertText" supports unicode format. Please refer the following link for more information:
 
If the Computer Vision Toolbox is not available, you can use the following workaround:
You can display a text and use the GETFRAME function to capture the text as an image first. Then you replace the image pixels with the pixels from the text. The code below demonstrates the suggested steps above. Make sure that the image and axes objects are visible within the figure window when executing the GETFRAME command.
% Read example image
load('clown.mat');
im = uint8(255*ind2rgb(X,map));
%% Create the text mask
% Make an image the same size and put text in it
hf = figure('color','white','units','normalized','position',[.1 .1 .8 .8]);
image(ones(size(im)));
set(gca,'units','pixels','position',[5 5 size(im,2)-1 size(im,1)-1],'visible','off')
% Text at arbitrary position
text('units','pixels','position',[100 100],'fontsize',30,'string','some text')
% Capture the text image
% Note that the size will have changed by about 1 pixel
tim = getframe(gca);
close(hf)
% Extract the cdata
tim2 = tim.cdata;
% Make a mask with the negative of the text
tmask = tim2==0;
% Place white text
% Replace mask pixels with UINT8 max
im(tmask) = uint8(255);
image(im);
axis off