13 PTB Screen 函数

PTB 中,最核心的一个函数是 Screen 函数,许多功能都要通过它来实现。

老套路,遇到一个函数,都是先 help 一下看看它的具体用法与功能介绍。

>> help Screen

Screen is a MEX file for precise control of the video display. Screen has
  many functions; type "Screen" for a list:
    Screen
 
  For explanation of any particular screen function, just add a question
  mark "?". E.g. for 'OpenWindow', try either of these equivalent forms:
    Screen('OpenWindow?')
    Screen OpenWindow?

上面截取了一部分 help 后的文本说明。解释下其中几条关键信息:

  • MEX file, 这是说 Screen 函数是一个 Matlab 可执行文件,你可以把它理解为用 C 程序语言写出来的一个函数。
  • Screen has many functions, Screen 可以做很多事情,它是一个多功能的函数。PTB 把几乎所有与屏幕显示有关的事情都交给这个函数了。
    可以将这个函数理解为一个机器人,这个机器人可以做有关屏幕显示内容的事情。但是需要用这个机器人能听得懂的方式下达指令。
  • 这个机器人(Screen 函数)具体能做哪些事情?我们不太可能全部都记下来,只能每次都问问它。问它可以做哪些事情,只要在命令行窗口里输入 Screen 就可以了。
>> Screen
Screen('DrawLine', windowPtr [,color], fromH, fromV, toH, toV [,penWidth]);
Screen('DrawArc',windowPtr,[color],[rect],startAngle,arcAngle)
Screen('FrameArc',windowPtr,[color],[rect],startAngle,arcAngle[,penWidth] [,penHeight] [,penMode])
Screen('FillArc',windowPtr,[color],[rect],startAngle,arcAngle)
Screen('FillRect', windowPtr [,color] [,rect] );
Screen('FrameRect', windowPtr [,color] [,rect] [,penWidth]);
Screen('FillOval', windowPtr [,color] [,rect] [,perfectUpToMaxDiameter]);
Screen('FrameOval', windowPtr [,color] [,rect] [,penWidth] [,penHeight] [,penMode]);
Screen('FramePoly', windowPtr [,color], pointList [,penWidth]);
Screen('FillPoly', windowPtr [,color], pointList [, isConvex]);

截取了一段输出结果,以 Screen('DrawLine', windowPtr [,color], fromH, fromV, toH, toV [,penWidth]); 为例。

第一个单引号里面 DrawLine,其实就是 Screen 函数可以做的一件事情。但这个到底是干什么的?怎么用?

  • 要看某一个具体的 Screen function 有什么用以及怎么用,可以在相应 “ 功能词 ” 后面加一个 ? 获得说明。举个例子,看看 DrawLine 的介绍:
>> Screen DrawLine?

Usage:

Screen('DrawLine', windowPtr [,color], fromH, fromV, toH, toV [,penWidth]);

Draw a line. "color" is the clut index (scalar or [r g b a] vector) that you
want to poke into each pixel; default produces black. "fromH" and "fromV" are
the starting x and y positions, respectively. "toH" and "toV" are the ending x
and y positions, respectively. Default "penWidth" is 1. 

See also: DrawLines
>> 

这段说明文字,很清楚地说明了,它是用来画一条线的,可以给它指定颜色,起点、终点以及线条的粗细等特征。

请大家自己再阅读一下 Screen 命令的说明,自己在命令行窗口中多试着了解下其他的功能。

好了,善用这两个命令,可以说我们的进度槽大涨,离最终目标不太远了。

在继续之前,还需要先了解一下 PTB 是怎么精确控制图形显示的,这对我们后面使用 Screen 函数有帮助。

PTB 利用 double buffering 辅以 VBL Synchronization 技术来控制图形显示并计时(实际上,E-prime 也是用的这种办法)。

double buffering 先设定两帧图像,一帧称为前景图,一帧称为背景图。前景图是可见的,背景图不可见,如下图:

《13 PTB Screen 函数》 double buffering 的前景与背景

接下来,先在背景图上写内容:

《13 PTB Screen 函数》 draw on back

然后,通过一个 Flip 动作,将背景翻转为前景,同时清空背景内容:

《13 PTB Screen 函数》 图片6.png

这样循环往复,一帧一帧的图像就显示出来。

PTB 利用另外一个叫作 VBL Synchronization 的方式完成 Flip 动作并计时。

我们知道,计算机显示器有一个很关键的参数是显示频率,比如,大多数笔记本电脑的屏幕显示频率是 60 HZ,意味着每秒显示 60 帧图像。用图来表示,就是下面这样子的:

《13 PTB Screen 函数》 图像显示示意图

显然,每两帧图像之间就有时间差,称这个时间差为VBL(Vertical BLank interval). 对于 60 HZ 的显示器,我们可以估算 VBL 值,大约为 16.67 ms. 有了这个信息,上面的图就可以用下面这个图来表示:

《13 PTB Screen 函数》 图像显示示意图

PTB 的 “Flip” 动作就在 VBL 中完成,计时也是以 VBL 为基准的。所以,我们可以看到,如果没有辅助其他计时工具,仅使用 E-prime 或者 PTB 的方式做实验,时间精度不可能做到低于16 毫秒。我个人认为,计时精度在 50 毫秒以上才比较可靠。除非,用了高刷新率的显示器。

    原文作者:花生_毛豆
    原文地址: https://www.jianshu.com/p/b8987f91486e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞