Psychtoolbox 脑电实验范式之mp4视频绘制

1. 读取

首先需要使用到Screen('OpenMovie')函数,该函数可以读取mp4、gif格式的数据,具体方式如下:

bash 复制代码
clear;
clc;
Screen('Preference', 'SkipSyncTests', 1);
screens = Screen('Screens');
screenNum = max(screens);
[window, screenRect] = Screen('OpenWindow',screenNum);
%HideCursor;
%设置图片
[leftPtr, dl, fpsl, wl, hl, zhenshul]=Screen('OpenMovie', window, 'F:\Matlab\混淆刺激范式\left.mp4');

其中leftPtr为函数读取视频的指针,dl表示视频时长,fps1表示视频频率,wl表示视频宽度,hl表示视频高度,zhenshul表示帧数。

2. 设置开始时间

需要使用到Screen('SetMovieTimeIndex')函数:

bash 复制代码
Screen('SetMovieTimeIndex', leftPtr, 0)

这里leftPtr表示视频读取后的指针,0表示设置视频从0秒开始播放。

3.设置视频播放区域和位置,循环次数

首先读取屏幕大小即屏幕长宽高

bash 复制代码
[screenXpixels, screenYpixels] = Screen('WindowSize', window);

screenXpixels表示屏幕宽度,screenYpixels表示屏幕高度

接着设置绘制区域,使用CenterRectOnPoint函数:

bash 复制代码
leftRect = CenterRectOnPoint([0, 0, wl, hl], screenXpixels / 4, screenYpixels / 2);

第一个[]表示绘制图的长宽以及从相对当前位置的哪里开始绘制,00表示相对当前位置的起始点开始绘制,screenXpixels / 4, screenYpixels / 2表示CenterRectOnPoint相对屏幕的位置,即从屏幕宽1/4、高1/2处绘制。大致是这个意思,具体如下图:
循环次数设置代码如下:

bash 复制代码
Screen('PlayMovie', leftPtr, 1, 1, 1);
4. 设置循环播放,代码如下:
bash 复制代码
while true
    lefttex = Screen('GetMovieImage', window, leftPtr, [], [], 2);
    if lefttex > 0
        Screen('DrawTexture', window, lefttex, [], leftRect);
        Screen('Close', lefttex);
    end
    Screen('Flip', window);

    % 检查键盘输入以退出循环
    [keyIsDown, ~, keyCode] = KbCheck;
    if keyIsDown
        if keyCode(KbName('Q'))
            break;
        end
    end

end
Screen('PlayMovie', leftPtr, 0);
Screen('CloseMovie', leftPtr);
Screen('CloseAll');
5.整体代码如下,代码给出三个视频left、mid、right运行:
clike 复制代码
clear;
clc;
Screen('Preference', 'SkipSyncTests', 1);
screens = Screen('Screens');
screenNum = max(screens);
[window, screenRect] = Screen('OpenWindow',screenNum);
%HideCursor;
%设置图片

[leftPtr, dl, fpsl, wl, hl, zhenshul]=Screen('OpenMovie', window, 'F:\Matlab\混淆刺激范式\left.mp4');
[midPtr, dm, fpsm, wm, hm, zhenshum]=Screen('OpenMovie', window, 'F:\Matlab\混淆刺激范式\mid.mp4');
[rightPtr, dr, fpsr, wr, hr, zhenshur]=Screen('OpenMovie', window, 'F:\Matlab\混淆刺激范式\right.mp4');
Screen('SetMovieTimeIndex', leftPtr, 0)
Screen('SetMovieTimeIndex', midPtr, 0)
Screen('SetMovieTimeIndex', rightPtr, 0)
% 设置视频播放区域
[screenXpixels, screenYpixels] = Screen('WindowSize', window);



leftRect = CenterRectOnPoint([0, 0, wl, hl], screenXpixels / 4, screenYpixels / 2);
midRect = CenterRectOnPoint([0, 0, wm, hm], screenXpixels / 2, screenYpixels / 2); % 中间视频区域
rightRect = CenterRectOnPoint([0, 0, wr, hr], 3*screenXpixels  / 4, screenYpixels / 2); % 右侧视频区域

Screen('PlayMovie', leftPtr, 1, 1, 1);
Screen('PlayMovie', midPtr, 1, 1, 1);
Screen('PlayMovie', rightPtr, 1, 1, 1);

while true
    lefttex = Screen('GetMovieImage', window, leftPtr, [], [], 2);
    midTex = Screen('GetMovieImage', window, midPtr, [], [], 2);
    rightTex = Screen('GetMovieImage', window, rightPtr, [], [], 2);
    if lefttex > 0
        Screen('DrawTexture', window, lefttex, [], leftRect);
        Screen('Close', lefttex);
    end
    if midTex > 0
        Screen('DrawTexture', window, midTex, [], midRect);
        Screen('Close', midTex);
    end
    if rightTex > 0
        Screen('DrawTexture', window, rightTex, [], rightRect);
        Screen('Close', rightTex);
    end
    Screen('Flip', window);

    % 检查键盘输入以退出循环
    [keyIsDown, ~, keyCode] = KbCheck;
    if keyIsDown
        if keyCode(KbName('Q'))
            break;
        end
    end

end
Screen('PlayMovie', leftPtr, 0);
Screen('PlayMovie', midPtr, 0);
Screen('PlayMovie', rightPtr, 0);
Screen('CloseMovie', leftPtr);
Screen('CloseMovie', midPtr);
Screen('CloseMovie', rightPtr);
Screen('CloseAll');

绘制结果:

相关推荐
Desire.9845 个月前
数据库复习——范式(Normal Form)
数据库·范式