필터 지우기
필터 지우기

How to measure text letter size in pixels?

조회 수: 14 (최근 30일)
T Shep
T Shep 2016년 10월 12일
댓글: Walter Roberson 2016년 10월 14일
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