3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Psychtoolboxの画面を画像として保存したいときはScreen('GetImage')

Last updated at Posted at 2020-02-20

Psychtoolbox(PTB)は、Matlabで心理物理学実験を行うためのツールボックスです。公式サイトはこちら。ちなみに私はPsychtoolboxをがんばるの管理人ですが、解説記事を書くのにQiitaのほうが使い勝手がよいので、これからはこちらで情報を共有していこうかと考え中です。
どうぞよろしくお願いいたします。


研究成果を発表するときなど、Psychtoolboxで行っている実験画面を第三者に見せたいということありませんか? 私はあります!

要は画面をキャプチャーして、画像として保存してプレゼン資料に貼り付ければいいわけですが、実験中はフルスクリーンであることが多く、思うようにいかない。そんなかたはぜひ、次の方法をお試しください。

Screen('GetImage')

実はPTBには、画面をキャプチャーするための命令がそなわっています。Screen('GetImage')を使えば、あっという間に画像を取得できます。詳細はコマンドウィンドウにScreen('GetImage?')と入力して表示されるヘルプをご覧ください。

チュートリアルで公開されているコードをベースに説明します。

% Clear the workspace and the screen
sca;
close all;
clearvars;

% Here we call some default settings for setting up Psychtoolbox
PsychDefaultSetup(2);

% Get the screen numbers. This gives us a number for each of the screens
% attached to our computer.
screens = Screen('Screens');

% To draw we select the maximum of these numbers. So in a situation where we
% have two screens attached to our monitor we will draw to the external
% screen.
screenNumber = max(screens);

% Define black and white (white will be 1 and black 0). This is because
% in general luminace values are defined between 0 and 1 with 255 steps in
% between. All values in Psychtoolbox are defined between 0 and 1
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);

% Do a simply calculation to calculate the luminance value for grey. This
% will be half the luminace values for white
grey = white / 2;
 
% Open an on screen window using PsychImaging and color it grey.
[windowPtr, windowRect] = PsychImaging('OpenWindow', screenNumber, grey, [10 10 800 800]);
 
% 青い四角形を描く
Screen('FillRect', windowPtr, [0 0 1], [350 350 450 450]);
Screen('Flip', windowPtr); % 画面を更新

imageArray=Screen('GetImage', windowPtr); % 画面全体の色情報を取得
imwrite(imageArray, 'output.jpg'); 

% Now we have drawn to the screen we wait for a keyboard button press (any
% key) to terminate the demo.
KbStrokeWait;

% Clear the screen.
sca;

このコードを実行すると、画面全体がoutput.jpgという名前の画像ファイルとして保存されます。保存される場所は現在のフォルダーです。

注意点として、imageArrayはuint8型になります。matlabで一般的に使用される型であるdoubleとは異なるために、そのまま算術演算を行おうとするとエラーになります。double関数で変換してください。

例えば imageArrayは0から255の値を取ります。これを正規化して0から1にしたいと思ったとき、

imageArray/255

という計算はエラーになるので、

double(imageArray)/255

とする必要があります。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?