Insert and Remove text from image

218 views (last 30 days)
jack
jack on 22 Jan 2016
Commented: Image Analyst on 20 Apr 2022
Hi guys,
I need your help in solving the following problem:
I have an image which i would like to add to it text but after i need to delete that text, how would i do that if i use the method described in the following link for adding text to images : http://www.mathworks.com/help/vision/ref/inserttext.html#btp_i7c-1
Moreover how would i add labels to point using plot on images?
Thanks in advance.

Answers (2)

Image Analyst
Image Analyst on 22 Jan 2016
That method burns text into the image. If you want to undo it, you'd have to still have the original image and just use that again.
To put text into the overlay, use text(). You can get the handle of that and call delete to remove it. The underlying image is not changed at all
hText = text(x, y, string);
% Get rid of it now
delete(hText);
  5 Comments
DGM
DGM on 20 Apr 2022
Edited: DGM on 20 Apr 2022
if you're using a cell array to hold the handles, you could do:
% create a figure with some text objects in it
x = linspace(0,1,3);
y = x;
labelList = {'banana' 'orange','apple'};
hold on;
for jj = 1:numel(x)
textList{jj} = text(x(jj),y(jj),labelList(jj));
end
pause(2) % pause for dramatic effect
cellfun(@delete,textList) % delete them
But you don't really need to use a cell array for the handles. If you just use a handles array, you can just use delete() by itself.
% create a figure with some text objects in it
x = linspace(0,1,3);
y = x;
labelList = {'banana' 'orange','apple'};
hold on;
for jj = 1:numel(x)
textList(jj) = text(x(jj),y(jj),labelList(jj));
end
pause(2) % pause for dramatic effect
delete(textList)
Image Analyst
Image Analyst on 20 Apr 2022
@Sanders A. see my Answer below on this page. I just added a new Answer.
You can also have functions to delete lines, etc. Just search for 'Type's like 'line', 'xline', etc.

Sign in to comment.


Image Analyst
Image Analyst on 20 Apr 2022
To clear all text from an axes, you can use this function I wrote:
%=====================================================================
% Erases all text labels from the specified axes.
function ClearTextFromAxes(handleToAxes)
try
handlesToChildObjectsInAxes = findobj(handleToAxes, 'Type', 'text');
if ~isempty(handlesToChildObjectsInAxes)
delete(handlesToChildObjectsInAxes);
end
catch ME
errorMessage = sprintf('Error in program %s, function %s(), at line %d.\n\nError Message:\n%s', ...
mfilename, ME.stack(1).name, ME.stack(1).line, ME.message);
uiwait(warndlg(errorMessage));
end
return; % from ClearTextFromAxes

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!