如何从 DrawFormattedText 获取像素信息

How can I get pixel information from DrawFormattedText

我正在使用 DrawFormattedText 函数在 Psychtoolbox 中显示文本。我想知道如何计算此函数中文本产生的像素数。

一个最小的例子看起来像这样(你应该能够copy/paste这个):

[w, wRect]=Screen('OpenWindow', 0, [0 128 128],[],[],[],[],128);
Screen('TextFont',w, 'Arial'); %Set font
Screen('TextSize',w, 16); %Set text size
[nx, ny, bbox] = DrawFormattedText(w, 'HowManyPixelsDoIHave?', 'center', 'center', 60);
Screen('Flip',w);
KbWait; %Wait for response before closing
Screen('CloseAll');

在这个例子中,我想知道有多少像素用于写入文本"HowManyPixelsDoIHave?"我需要一个度量来比较各种打印的文本字符串。

我似乎不太了解如何访问 DrawFormattedText 函数生成的内容。

整个屏幕及其像素值的矩阵就足够了。如果有人知道如何将其缩小到屏幕的特定区域,将不胜感激。

感谢您提供的任何帮助!

提取像素实际上是中继 easy.I我不确定您是否想要显示文本需要多少 space 的信息,或者您想要多少黑色的信息字母 B 包含的像素数。

版本 1 - 我需要多少space

所有信息都存储在bbox变量中。 bbox 包含定义打印文本的矩形的点。

您可以通过在命令行中键入 bbox 来获取这些值。 输出应如下所示:

>> bbox
bbox =

   1592    517   1770    533

您可以获得的个人价值:

bbox(1) % x left
bbox(2) % y top
bbox(3) % x right
bbox(4) % y botom

这是您的问题的代码

 [w, wRect]=Screen('OpenWindow', 0, [0 128 128],[],[],[],[],128);
Screen('TextFont',w, 'Arial'); %Set font
Screen('TextSize',w, 16); %Set text size
[nx, ny, bbox] = DrawFormattedText(w, 'HowManyPixelsDoIHave?', 'center', 'center', 60);
Screen('Flip',w);
KbWait; %Wait for response before closing
Screen('CloseAll');

pixelWidth  =  bbox(3) - bbox(1) %x
pixelHeight = bbox(4) - bbox(2) %y

pixelWidth 包含为您的 x 轴打印整个文本(包括白色 space)所需的像素数。 pixelHeight 对您的 y 轴执行相同的操作。

我认为你应该看看一些 matlab/octave 基础知识,访问向量的特定元素很容易学习而且非常重要。

版本 2 - 字母 B 需要多少黑色像素

有一个名为 Screen('GetImage', 的函数可以将您当前的屏幕保存到一个变量中。对于这个提议,我们创建一个 100 * 100 像素大的 window。因此,如果我们保存 window,我们会得到一个 100*100*3 图像作为变量。 *3 告诉我们该图像有三层,分别是 RGB 颜色的 R、G 和 B 值 space。 RGB 的值从 0 到 255。

我们所做的是打开一个背景为黑色 [0 0 0] 的 window,然后在该屏幕上用稍亮的黑色 [1 1 1] 书写。当我们现在用 Screen('GetImage', 保存 window 时,第一层会得到类似这样的东西 image(:,:,1):

  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  1  1  1  1  1  1  1  0  0  0  0
  0  0  0  1  0  0  0  0  0  0  1  0  0  0
  0  0  0  1  0  0  0  0  0  0  0  1  0  0
  0  0  0  1  0  0  0  0  0  0  0  1  0  0
  0  0  0  1  0  0  0  0  0  0  1  0  0  0
  0  0  0  1  1  1  1  1  1  1  1  0  0  0
  0  0  0  1  0  0  0  0  0  0  1  0  0  0
  0  0  0  1  0  0  0  0  0  0  0  1  0  0
  0  0  0  1  0  0  0  0  0  0  0  1  0  0
  0  0  0  1  0  0  0  0  0  0  0  1  0  0
  0  0  0  1  0  0  0  0  0  0  1  0  0  0
  0  0  0  1  1  1  1  1  1  1  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0

只需将其加起来 2 次,您就知道包含多少像素。

WINDOWSIZE = [100 100 200 200]
[w, wRect]=Screen('OpenWindow', 0, [0 0 0] ,WINDOWSIZE );

Screen('TextFont', w, 'Arial'); %Set font
Screen('TextSize',  w, 16); %Set text size
Screen('TextColor', w  ,[1 1 1]); % rgb for the textfont displays 1

[nx, ny, bbox] = DrawFormattedText(w, 'Ban', 'center', 'center' );
Screen('Flip',w);
ban = Screen('GetImage', w);
KbWait; %Wait for response before closing
[nx, ny, bbox] = DrawFormattedText(w, 'San', 'center', 'center' );
Screen('Flip',w);
san = Screen('GetImage', w);
KbWait; %Wait for response before closing
Screen('CloseAll');

pixelsum_san= sum(sum(san(:,:,1)))
pixelsum_ban =sum(sum(ban(:,:,1)))

结果如下:

pixelsum_san =  79
pixelsum_ban =  90

根据@wuschLOR 的回答推断:

要获取我使用的屏幕内容的 3D 数组:

IM = Screen('GetImage',w);

然后我从每个维度的矩阵中减去(绝对)每个维度的背景值:

bgcolour = [0, 128, 128];
for l = 1:size(IM,3)

    nobgval(l) = sum(sum(abs(double(IM(:,:,l))-bgcolour(l))));

end

然后我将其相加得到 'metric' 屏幕内容与背景的差异:

pixelmetric = sum(nobgval)

我不确定这在所有情况下的可靠性如何,但为了比较以相同字体大小显示的不同单词等,我假设它应该有效。我很想听听是否有人对这种方法有具体的批评(或者更好,更好的解决方案!):)

非常感谢!