How to measure text letter size in pixels?

24 views (last 30 days)
T Shep
T Shep on 12 Oct 2016
Commented: Walter Roberson on 14 Oct 2016
I am trying to put up a letter (x) and measure it's height and width in pixels. I would like to do it for a number of different size fonts. I thought there was a scan command that would start at the top of the screen and scan until it ran into the text, then record that value. I also would like to be able to put up two letter X's and measure the distance in pixels between them.
  3 Comments
T Shep
T Shep on 13 Oct 2016
Yeah, I have to be able to do this in real time.
Image Analyst
Image Analyst on 13 Oct 2016
You didn't respond to my "Answer" below. Did you see it? Will that work for you?

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 13 Oct 2016
If you have the Computer Vision System Toolbox, try insertText(): http://www.mathworks.com/help/vision/ref/inserttext.html
  1 Comment
T Shep
T Shep on 14 Oct 2016
Edited: Walter Roberson on 14 Oct 2016
Sorry, the answer was collapsed and I did not see it. This is what I have so far. I can scan the height and width of the first letter but I want to be able to continue on to the second letter and scan it as well.
function xw = x_width_space(img)
fW0=0;
NW1=0;
NW2=0;
for i=1:size(img,1);
if ~isempty(find(img(:,i) < 50)) %%if pixel val is < 50, inked row.
if fW0==0
NW1=i; %%starting point
fW0=1;
end
else % if non-ink row
if fW0==1
NW2=i; %%ending point + 1 row
fW0=0;
end
end
end

Sign in to comment.


Walter Roberson
Walter Roberson on 14 Oct 2016
Create a uicontrol('style','text') and appropriate FontName, FontSize, FontUnits, FontWeight, FontAngle. Do not worry about the Position. You can set the Visibility to off. Once it has been set up, set the uicontrol Units to pixels .
Now for each character to be tested, set the String property of the uicontrol to just that one character, and then ask for the Extent property of the control. This will tell you how big the control would have to be to hold the string. Record it, keep going with the next desired character, or set the FontSize and ask again, and so on.
After you are done, delete the uicontrol.
  2 Comments
T Shep
T Shep on 14 Oct 2016
Edited: Walter Roberson on 14 Oct 2016
Sorry, this is the fill code. I can measure the width and height of the first letter, but I am unable to move on to the second letter. I want to be able to find the spacing in between the first and the second letter in this loop. The function I use is at the bottom of this script.
ptstart = 7; %%%%%%%starting pt
ptend = 250; %%%%%%%ending pt
fonttype = 'Courier'; %%%%%%%ending pt
for i = ptstart:ptend % pt sizes
Screen('TextSize',window,i);
DrawFormattedText(window, repmat('x',1,2), 'center', 'center',0); % write formatted statement to offsreen buffer
Screen('flip',window);
img = Screen('GetImage',window);
xh(i-6) = x_height(img(:,:,1)) % x-height in pixels
xw(i-6) = x_height(img(:,:,1)') % x-width in pixels
end
x = [ptstart:ptend]; % pt size
ptpxw = [ x; xh; xw]; % row 1: pt size; row 2: pixel #
Screen('Closeall');
ShowCursor;
time = datestr(now);
%%save lookup tables
%save('fontsize_calibrated2.mat', '-mat','time','ptpxw'); %%,'file_content');
save('fontsize_calibrated3.mat', '-mat', 'time','ptpxw'); %%,'file_content');
function xw = x_height(img)
f0=0;
N1=0;
N2=0;
for i=1:size(img,1);
if ~isempty(find(img(i,:) < 50)) %%if pixel val is < 50, inked row.
if f0==0
N1=i; %%starting point
f0=1;
end
else % if non-ink row
if f0==1
N2=i; %%ending point + 1 row
f0=0;
end
end
end
xw = N2-N1;
%
Walter Roberson
Walter Roberson on 14 Oct 2016
If you want to know about how Psychtoolbox positions characters, you need to talk to the Psychtoolbox people http://psychtoolbox.org/

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!