Insert and Remove text from image
이전 댓글 표시
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.
답변 (2개)
Image Analyst
2016년 1월 22일
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
Sanders A.
2022년 4월 12일
What if I have a cell array of text items from generating them procedurally?
Image Analyst
2022년 4월 12일
To put into the overlay
for k = 1 : length(ca)
thisString = ca{k};
x = whatever;
y = whatever;
text(x, y, thisString);
end
To burn into the image, there is this example from the help for insertText:
% Create texts that contain fractions.
text_str = cell(3,1);
conf_val = [85.212 98.76 78.342];
for ii=1:3
text_str{ii} = ['Confidence: ' num2str(conf_val(ii),'%0.2f') '%'];
end
% Define the positions and colors of the text boxes.
position = [23 373;35 185;77 107];
box_color = {'red','green','yellow'};
% Insert the text with new font size, box color, opacity, and text color.
RGB = insertText(I,position,text_str,'FontSize',18,'BoxColor',...
box_color,'BoxOpacity',0.4,'TextColor','white');
Sanders A.
2022년 4월 20일
Sorry for the unclear question
I had produced text markers on my plot using
textList{jj} = text(x,y,labelList(jj));
and was looking for a way to delete all the labels in one go such as with
delete(textList{:})
But that didn't work. So I just looped through all the elements and deleted them one at a time.
Regardless, I appreciate the time you took to provide such a robust answer!
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
2022년 4월 20일
@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.
Image Analyst
2022년 4월 20일
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
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!