How to measure text letter size in pixels?

14 次查看(过去 30 天)
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 个评论
T Shep
T Shep 2016-10-13
Yeah, I have to be able to do this in real time.
Image Analyst
Image Analyst 2016-10-13
You didn't respond to my "Answer" below. Did you see it? Will that work for you?

请先登录,再进行评论。

回答(2 个)

Image Analyst
Image Analyst 2016-10-13
If you have the Computer Vision System Toolbox, try insertText(): http://www.mathworks.com/help/vision/ref/inserttext.html
  1 个评论
T Shep
T Shep 2016-10-14
编辑:Walter Roberson 2016-10-14
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

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2016-10-14
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 个评论
T Shep
T Shep 2016-10-14
编辑:Walter Roberson 2016-10-14
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 2016-10-14
If you want to know about how Psychtoolbox positions characters, you need to talk to the Psychtoolbox people http://psychtoolbox.org/

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by