How to add text to an image

Hello, If I have an image with the size 512*512 (or any other size) how do I add a string to any location I choose for example location (100,250) all the codes I found are too long but is there a one line code that does this?

 Accepted Answer

Image Analyst
Image Analyst on 16 Dec 2013
You can do it with a function in the Computer Vision System Toolbox. If you don't have that, you'll have to use this from Brett Shoelson: http://www.mathworks.com/matlabcentral/fileexchange/38721-embed-text-and-graphics-in-an-image

13 Comments

I found the thing I was looking for "text" but I still have a problem when I type >> text (84,145,'random') The result is good and shows in image however my code has certain parameters and in those parameters there is a calculation which results in LAX = 82 LAY = 145 so when I type>> text (LAX,LAY,'random') the matlab gives me the following error (Error using text Invalid parameter/value pair arguments) How to make the (text) order read LAX,LAY results in a way that doesnt give error?
You're not telling me the whole story. This works just fine:
grayImage = imread('cell.tif');
imshow(grayImage);
axis on;
LAX = 82;
LAY = 145
text(LAX, LAY, 'random')
the code is too long to post here about 1000 lines and you are right it does work when you put it like this but in the calculation in the original code it doesn't here is the part of the code
LAX = (LeftX + RightX) / 2
LAY = (LeftY + RightY) / 2
text (LAX,LAY,'random');
It calculates these numbers but gives me an error as shown
You must have overwritten text with something else. Before the text line, put this
which -all text
whos text
tell me what it says
It said the following
built-in (C:\Program Files\MATLAB\R2011b\toolbox\matlab\graph2d\text) Error using text Invalid parameter/value pair arguments
Error in fayezcode (line 83) text (LAX,LAY,'random');
That doesn't make any sense. It looks like it's the normal built in text() function. What happens if you put this line right before that line
text(82, 145, 'random2');
theUnits = get(gca, 'Units') % See if it's normalized, pixels, or whatever.
Does that work? Does it display the 'random2' text without complaining?
Yes it shows random2 perfectly and says the following
theUnits =
normalized
You can't have numbers more than 1 if the units are normalized. OK, what does this do:
text(.4, .5, 'random');
It works but puts the text on the very top-left corner of the image
If you want to work with pixels then try setting the units property to pixels instead of normalized.
(error using set invalid handle) I did the following - It still doesnt work
LAX = (LeftX + RightX) / 2
LAY = (LeftY + RightY) / 2
set (LAX,'units','{pixels}');
set (LAY,'units','{pixels}');
text(LAX, LAY, 'random');
Hello I found a solution that worked I converted to binary then from binary to dec again by adding this
LAX = dec2bin(LAX);
LAX = bin2dec(LAX);
LAY = dec2bin(LAY);
LAY = bin2dec(LAY);
It worked for some reason so the problem is solved for me but if there is a better solution I will keep my eyes on this topic thank you very much for all your help
That's nonsense. You don't set the units property of a scalar variable to anything. LAX and LAY don't even have properties like 'units'! You set the units property of an axes or figure to something.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!