1、PsychtooboxLihan Chen2015.4.28MATLAB 图像刺激 图像处理 图像基础 图像的变换与操纵 计算生成图像图像基础图像基础像素、颜色、索引图像图像基础像素 数字图像 离散的坐标:像素 离散的强度:0255(黑白)图像基础颜色 RGB颜色系统 每个像素的红、绿、蓝均由uint8表示(0255)alpha通道:透明度 24位/32位真彩色imtool(photo.jpg);用MATLAB存取并显示图片读取并显示图片 im=imread(tmp.png);size(im)imshow(im)写入图片 imwrite(im,tmp.jpg);a=ones(400,40),z
2、eros(400,40);%先生成一根白条和一根黑条a1=repmat(a,1,5);%将这个组合复制5遍a2=xor(a1,a1);%将这个黑白条纹与自己的转置做异或逻辑运算imshow(a2)%棋盘格就生成了实例:红蓝3D与双眼竞争 im1=imread(Koala.jpg);im2=imread(Penguins.jpg);%用第二幅图替代第一幅图的红色通道 im1(:,:,1)=im2(:,:,1);%把绿色通道清零 im1(:,:,2)=0;imshow(im1)image与colormap image函数是MATLAB最基本的图像显示函数,可以绘制索引图像,即每个像素的值对应颜色查
3、找表中的索引 colormap:定义图像显示用的颜色查找表 imagesc将数据scale后绘制成图(例如绘制相关矩阵)image与imshow Imshow仅用于显示由RGB或灰度值定义的图像(image也可以)无论是哪个函数,若图像是以uint8表示的,则取值范围为0255,若以double表示,则取值范围是01 将将face.jpg face.jpg 与与 house.jpg house.jpg 融合成一张图融合成一张图片,片,faceface占用红色通道,占用红色通道,househouse占用绿色通占用绿色通道,蓝色通道取值道,蓝色通道取值0.0.由于两张图片不一样由于两张图片不一样大
4、,最后结果保留两张图片重叠部分即可,大,最后结果保留两张图片重叠部分即可,如示例如示例mix.jpg mix.jpg(imread(imread 读出的数据是读出的数据是uint8,uint8,取值范围是取值范围是0-2550-255,imshow imshow 图片的图片的取值范围是取值范围是0-10-1,中间需要除以,中间需要除以255255这个系这个系数数)clear allface=imread(face.jpg);house=imread(house.jpg);mix(:,:,1)=face(1:680,1:550,1);mix(:,:,2)=house(1:680,221:770,
5、2);mix(:,:,3)=0;imshow(mix);image 绘制数据 246810123456789100.50.550.60.650.70.750.80.850.90.951imagesc&colormap 绘制相关矩阵a=rand(10);for i=2:10a(:,i)=a(:,i-1)+rand(10,1)*0.5;endimagesc(corr(a)colorbarcolormap autumnPsychtoolbox-1-listing-lst4.mdata.mat data.mat 中保存了一组虚拟的中保存了一组虚拟的fMRIfMRI数据,数据里数据,数据里记录了记录了V
6、1,V2,V3,V4,V5,ips 6V1,V2,V3,V4,V5,ips 6个脑区个脑区 100 100个时间个时间点的值,请画出其点的值,请画出其6x6 6x6 的相关矩阵。的相关矩阵。load data.matfdata=V1,V2,V3,V4,V5,ips;imagesc(corr(fdata)马赫带效应马赫带效应clear all%清除变量,MEX等,如果有需要的变量还不想清除,需要小心close all%关闭各种figure窗口img=1:10;%图片的值,即是选择colormap第1-10的值figure(1)%打开figure编号为1的窗口paintpots=ones(10,3
7、);%自己创建的一个颜色查找表colormap(paintpots)%将定义好的对应关系输入系统image(img);%呈现图片axis off;%取消在这个任务中没有意义的轴for i=1:10 paintpots(i,:)=(i/10);%令查找表的第i位的值为i/10,即是最大亮度的i/10倍 colormap(paintpots);%将更新的查找表输入系统 pause%等待按键,注意看figure中图片的变化endPsychtoolbox-1-listing-lst3.mclear allclose allcolormap(gray(256)%将颜色查找表设置为灰度图img=resha
8、pe(1:256,16,16);image(img);axis square%将长宽设置为等长axis offpausefor i=1:200 paintpots=rand(256,3);%将颜色查找表设为随机,图片的像素颜色也变成随机 colormap(paintpots);drawnow%立刻呈现end图像的变换与处理图像的变换与处理调整曲线、窗口化像素亮度的变换操作(PS中的“曲线”)实例:去除扫描时背面透过来的影 im=imread(book-000123.png);g=rgb2gray(im);d=double(g);y=uint8(d*1.7-85);imshow(y);imwri
9、te(y,out.png);Psychtoolbox-1-listing-lst5.m图像的“窗口”操纵每个像素的亮度:乘以一个系数生成高斯窗口窗口化图像 imagesc(GaWindow)tmp=double(im).*GaWindow;Windowed =uint8(tmp);imshow(Windowed)Psychtoolbox-1-listing-lst6.m先生成一个先生成一个200 x200200 x200的高斯滤波器,的高斯滤波器,sigma=50sigma=50,读取读取Simpson.jpg,Simpson.jpg,使得二维高斯覆盖在图像上,使得二维高斯覆盖在图像上,高斯覆
10、盖范围外值为高斯覆盖范围外值为0 0,(如,(如3.jpg3.jpg)。并且将这)。并且将这个过程做成动态。通过鼠标控制高斯覆盖的位置,个过程做成动态。通过鼠标控制高斯覆盖的位置,刷新的频率定为刷新的频率定为25Hz 25Hz(既每次呈现一帧图像(既每次呈现一帧图像 要要pause 0.04spause 0.04s),点击鼠标右键退出循环。),点击鼠标右键退出循环。clear allcdata=imread(Simpson.jpg);y,x=size(cdata);X,Y=meshgrid(1:x,1:y);while 1 x0,y0,button=GetMouse;if button(3)=
11、0close allbreakendga=exp(-1/2/502)*(X-x0).2+(Y-y0).2);%ga=ga./max(max(ga);tmp=double(cdata).*ga;imshow(tmp/255)pause(0.04)end邻居处理邻居处理高斯模糊、边缘检测空间滤波 定义一个矩阵(称为滤波器filter)将该矩阵覆盖在每个像素点上 对所有被覆盖的点 将原图像矩阵和该矩阵的对应点相乘 求和变成新图像的点 由邻居决定新的像素点的强度我们对下幅图片用3x3的平均滤波,即滤波的算子是一个3x3的平均矩阵。设原矩阵名为x(图),x(3,3)的值为74,则经过平均滤波之后,这个点
12、的值变为(20+65+1+102+74+54+58+98+50)/9=58。滤波的过程就是对所有点都做这个计算,得到一个新的值。Psychtoolbox-1-listing-lst7.m高斯模糊 改变窗口大小,令x=y=10,stdev=5,生成一个“高斯窗口”让窗口矩阵的和等于1GaFilter=GaWindow/sum(sum(GaWindow);用imfilter或filter2或conv2对原图进行滤波imf=imfilter(im,GaFilter);imshow(imf)高斯模糊的图片边缘检测原始图像原始图像 grayedge(gray)imfilter(gray,fspecial
13、(sobel)更多的滤波器 用fspecial生成二维的特殊滤波器 average 简单平均 gaussian 高斯滤波 motion 模拟摄像机抖动 sobel 检测边缘算子 doc fspecial(1 1)将)将faceface进行高斯滤波,高斯算子的边长是进行高斯滤波,高斯算子的边长是100100个个pixelpixel,sigmasigma为为 30 30。clear allface=imread(face.jpg);op=fspecial(Gaussian,100,30);filface=imfilter(face,op);imshow(filface);(2 2)对)对house
14、house进行边缘检测进行边缘检测。clear allhouse=imread(house.jpg);ghouse=rgb2gray(house);imshow(edge(ghouse,canny)(3 3)制作一个制作一个200 x200 200 x200 竖直朝向顺时针偏转竖直朝向顺时针偏转1515度的度的gaborgabor,其他参数可自己定其他参数可自己定,并解释各个参数的含义。并解释各个参数的含义。clear allX,Y=meshgrid(1:200,1:200);grating=sin(tan(75/180*pi)*X+Y)/25);%(tan(75/180*pi)表示竖直朝向顺
15、时偏转15度,%gabor一个周期是25*2*pi/sqrt(tan(75/180*pi)2+1)个pixel,%(分母是为了标准化)grating=(grating+1)/2;%imshow(grating);op=fspecial(Gaussian,200,50);%高斯算子的边长是200个pixel,sigma为50个pixelop=op/max(max(op);gabor=op.*grating;imshow(gabor)计算生成图像计算生成图像正弦光栅,Gabor Patch,生成正弦光栅 X,Y=meshgrid(1:50,1:100);imshow(sin(X)imshow(si
16、n(X+Y)imshow(sin(X+Y)/2)imshow(sin(X+2*Y)/2)imshow(0.5+0.5*sin(3*X+4*Y)/6)Psychtoolbox-1-listing-lst8.m生成Gabor%先生成正弦光栅 X,Y=meshgrid(-50:50,-50:50);im=sin(X+2*Y)/3);imshow(im)%生成窗口“盖住”光栅 Ga=exp(-(X/20).2)-(Y/20).2);imshow(Ga.*im)生成棋盘格clear allclose allx,y=meshgrid(1:400,1:400);%制作坐标网格x1=sin(x/10);%值域
17、为-1,1;y1=sin(y/10);c=x1.*y1;%负负得正pic=c0;%最后逻辑判断,得到棋盘格imshow(pic)Psychtoolbox函数介绍(图像刺激的实现)SCREEN函数与视觉刺激函数与视觉刺激被试眼睛到屏幕的距离为d,刺激的长度是l,那么视角就是,有tan()=l/d,写成matlab语句就是tand()=l/d,则=atand(l/d)deg2pix.m如果屏幕的分辨率是a x b,屏幕宽度是w,则pixel数=tand()*d*a/w假设屏幕的分辨率为1024x768,我们测得屏幕的宽为40cm,可以得出每cm的长度中有1024/40=25.6个pixel。实际报
18、告刺激大小的时候往往用的是视角,而我们操作matlab用的pixel的数目。如果已知求pixel数,则有pixel数=lx25.6=tand()*d*25.6视角视角扫频扫频 Support for multiple displays myscreens=Screen(Screens);Windows:0=“Virtual display”,spanning the main display and its right neighbour.1=Real main display,2=Real display 2.Stereo display support for easy binocular
19、 stimulation Screen(OpenWindow,stereoMode);Demos:StereoDemo,ImagingStereoDemo One command in your stimulus drawing loop to switch between drawing of left-(bufferid=0)and right-eye(bufferid=1)view:Screen(SelectStereoDrawBuffer,myWindow,bufferid)Psychtoolbox-2 StereoDemo.mStereo display for dual-displ
20、ay setups:Slightly different approach on Windows vs.MacOS/X:Windows:%Open on screen 0,with stereomode 4:win=Screen(OpenWindow,0,.,4);MacOS/X:%Open left-eye window first on lefteyedisplay,with stereomode 10:win=Screen(OpenWindow,lefteyedisplay,.,10);%Open right-eye window righteyedisplay,with stereom
21、ode 10.%Dont care about window handle,this window is just a passive%receiver:Screen(OpenWindow,righteyedisplay,.,10);Use win windowhandle for all further commands.Demos:StereoDemo,ImagingStereoDemoPTB-3 Double buffered drawing model-ConceptScreen:Stimulus onset timestamps Screen(Flip)-Example 1:Fix-
22、Prime-TargetPsychtoolbox2-prime.mScreen(Flip)-Example 1:FixPrimeTarget1%Draw fixation spot to backbuffer:winRect=Screen(Rect,win);slack=Screen(GetFlipInterval,win)/2;Screen(FillOval,win,255,CenterRectInRect(0 0 20 20,winRect);2.%Show fixation spot on next retrace,take onset timestamp:tfixation_onset
23、=Screen(Flip,win);3.%Draw prime stimulus image to backbuffer:Screen(DrawTexture,win,primeImage);4.%Show prime exactly 500 msecs after onset of fixation spot:tprime_onset=Screen(Flip,win,tfixation_onset+0.500-slack);5.%Draw target stimulus image to backbuffer:Screen(DrawTexture,win,targetImage);6.%Sh
24、ow target exactly 100 msecs after onset of prime image:ttarget_onset=Screen(Flip,win,tprime_onset+0.100-slack);7.%Show target exactly for 200 msecs,then blank screen.ttarget_offset=Screen(Flip,win,ttarget_onset+0.200-slack);Choose your presentation times as a multiple of the video refreshduration!10
25、0 Hz is a good refresh setting for 10 ms timing granularityScreen(Flip)-Example 2:Animation with const.fps.1.%Get basic timing info:Duration of a single video refresh interval:refresh=Screen(GetFlipInterval,win);2.%Synchronize to retrace at start of trial/animation loop:vbl=Screen(Flip,win);3.%Loop:
26、Cycle through 300 images:for i=1:3004.%Draw ith image to backbuffer:Screen(DrawTexture,win,myImage(i);5.%Show images exactly 2 refresh cycles apart of each other:vbl=Screen(Flip,win,vbl+(2 0.5)*refresh);6.%Keyboard checks,whatever.Next loop iteration.end;7.%End of animation loop,blank screen,record
27、offset time:toffset=Screen(Flip,win,vbl+(2 0.5)*refresh);Choose your monitors video refresh rate as multiple of wantedanimation framerate!For 25 fps-75 Hz or 100 Hz is a good refreshsetting.For 30 fps-60 Hz,90 Hz,120 Hz.产生视觉刺激的三种方式 导入图片(jpg,.tif,)在PTB里画图 文本图片(text figures)Screen函数 Screen是PTB与视频显示有关的
28、函数的集合 都有哪些子函数?Command window输入Screen Screen(函数字符串,参数列表)http:/docs.psychtoolbox.org/ScreenPsychtoolbox-2-listing9.1-9.10 windowPtr,rect=Screen(Openwindow,windowPtrOrScreenNumber,color,rect,pixelSize,numberOfBuffers,stereomode,multisample,imagingmode,specialFlags,clientRect);使用Screen函数 常用的子函数、使用顺序及功能
29、查询有多少屏幕 Screens 开窗口(创建主页面)OpenWindow 绘制视觉刺激 画线 DrawLine,画点 DrawDots,贴图 DrawTexture 页面切换 Flip 关闭 CloseAll 附注 ShowCursor;HideCursor;Basic 2D drawing commands(Filled)Circles and ellipses:Screen(FrameOval,window,color,boundingrect,penWidth);Screen(FillOval,window,color,boundingrect);(Filled)Rectangles:S
30、creen(FrameRect,window,color,boundingrect,penWidth);Screen(FillRect,window,color,boundingrect);Lines of different thickness and stipple patterns:Screen(DrawLine,window,color,fromH,fromV,toH,toV,penWidth);(Filled)Arcs:Screen(DrawArc,window,color,boundingrect,startAngle,arcAngle);Screen(FrameArc,windo
31、w,color,boundingrect,startAngle,arcAngle);Screen(FillArc,window,color,boundingrect,startAngle,arcAngle);(Filled)convex and concave Polygons:Screen(FillPoly,window,color,xy)批处理-Fast 2D batch drawing and parallelismInstead of drawing one primitive at a time,define hundreds of similarprimitives and dra
32、w them with 1 call:Instead of:Screen(FillRect,win,red1 green1 blue1,left1 top1 right1 bot1);Screen(FillRect,win,red2 green2 blue2,left2 top2 right2 bot2);.Screen(FillRect,win,redn greenn bluen,leftn topn rightn botn);Write:mycolors=red1 green1 blue1;red2 green2 blue2;.;redn greenn bluen;myrects=left
33、1 top1 right1 bot1;left2 top2 right2 bot2;.;leftn topn rightn botn;Screen(FillRect,win,mycolors,myrects);screenNum=0;res=1280 1024;clrdepth=32;wPtr,rect=Screen(OpenWindow,screenNum,0,.0 0 res(1)res(2),clrdepth);black=BlackIndex(wPtr);white=WhiteIndex(wPtr);Screen(FillRect,wPtr,black);Screen(wPtr,Fli
34、p);HideCursor;ticwhile toc3;endScreen(FillRect,wPtr,white);Screen(wPtr,Flip);HideCursor;ticwhile toc3;endScreen(CloseAll);ShowCursorscreenNum=0;flipSpd=13;%a flip every 13 frameswPtr,rect=Screen(OpenWindow,screenNum);monitorFlipInterval=Screen(GetFlipInterval,wPtr);%1/monitorFlipInterval is the fram
35、e rate of the monitorblack=BlackIndex(wPtr);white=WhiteIndex(wPtr);%blank the Screen and wait a secondScreen(FillRect,wPtr,black);Screen(wPtr,Flip);HideCursor;ticwhile toc1;end%make a rectangle in the middle of the screen;flip colors and sizeScreen(FillRect,wPtr,black);vbl=Screen(wPtr,Flip);%collect
36、 the time for the first flip with vblfor i=1:10Screen(FillRect,wPtr,0 0 255,100 150 200 250);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);%flip 13 frames after vblScreen(FillRect,wPtr,255 0 0,100 150 400 450);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);end%blank the screen and w
37、ait a secondScreen(FillRect,wPtr,black);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);ticwhile toc1;end%make circles flip colors&sizeScreen(FillRect,wPtr,black);vbl=Screen(wPtr,Flip);for i=1:10Screen(FillOval,wPtr,0 180 255,500 500 600 600);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInter
38、val);Screen(FillOval,wPtr,0 255 0,400 400 900 700);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);end%blank the Screen and wait a secondScreen(FillRect,wPtr,black);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);ticwhile toc1;end%make lines that flip colors size&positionScreen(FillRec
39、t,wPtr,black);vbl=Screen(wPtr,Flip);for i=1:10Screen(DrawLine,wPtr,0 255 255,500,200,700,600,5);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);Screen(DrawLine,wPtr,255 255 0,100,600,600,100,5);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);end%blank the Screen and wait a secondScreen
40、(FillRect,wPtr,black);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);ticwhile toc1;end%combine the stimuliScreen(FillRect,wPtr,black);vbl=Screen(wPtr,Flip);for i=1:10Screen(FillRect,wPtr,0 0 255,100 150 200 250);Screen(DrawLine,wPtr,0 255 255,500,200,700,600,5);Screen(FillOval,wPtr,0 180 255
41、,500 500 600 600);Screen(TextSize,wPtr,150);Screen(DrawText,wPtr,FUNKY!,200,20,255 50 255);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);Screen(FillRect,wPtr,255 0 0,100 150 400 450);Screen(FillOval,wPtr,0 255 0,400 400 900 700);Screen(DrawLine,wPtr,255 255 0,100,600,600,100,5);vbl=Screen(w
42、Ptr,Flip,vbl+(flipSpd*monitorFlipInterval);end%blank the screen and wait a secondScreen(FillRect,wPtr,black);vbl=Screen(wPtr,Flip,vbl+(flipSpd*monitorFlipInterval);ticwhile toc1;endScreen(CloseAll);ShowCursor http:/en.wikibooks.org/wiki/MATLAB_Programming/Psychtoolbox/Screen_CommandsContents 1 BlitI
43、mage2552 BringMATLABToFront3 BringWindowToFront4 ClearScreen5 Close6 CloseAll7 CloseScreen8 Computer9 CopyWindow10 CopyWindows11 DrawLine12 DrawOval13 DrawPoly14 DrawRect15 DrawText16 FillArc17 FillOval18 FillPoly19 FillRect20 FrameArc 21 FrameOval22 FrameRate23 FrameRect24 Gamma25 GetClut26 GetFron
44、tWindow27 GetImage28 GetImage25529 GetMATLABWindow30 GetWindowByTitle31 GlobalRect32 IsOffscreen33 MATLABToFront34 MoveWindow35 OpenOffscreenWindow36 OpenWindow37 PixelSize38 PixelSizes39 Preference40 PutColorImage41 PutImage42 Rect43 Screens44 SelectMATLAB45 SetClut46 SetClutQ47 SetDrawingRegion48
45、SetGamma49 TextFace50 TextFont51 TextMode52 TextSize53 TextStyle54 TextWidth55 UsageWarnings56 VideoCard57 WaitBlanking58 WaitVBL59 WindowKind60 Windows61 WindowScreenNumber62 WindowTitle63 WindowToBack64 WindowToFront Screen函数:Screens子函数 Screens子函数:查询有多少屏幕 FAQ:有多少屏幕?若有多个显示器,一般主屏幕用来控制,编号最大的屏幕用来呈现刺激
46、此步可省略,可以直接开0号窗口 screens=Screen(Screens);screenNumber=max(screens);Screen函数:OpenWindow子函数 OpenWindow用来打开一个窗口(“画布”)w,rect=Screen(OpenWindow,screenNumber);返回值 w:该窗口的“句柄值”,类似于“指针”以后对这个窗口进行任何操作都要用这个指针 rect:该窗口的“矩形”,由左上、右下两个点的座标定义,比如0 0 1024 768 更多参数 背景颜色、窗口大小?Screen OpenWindow?绘制视觉刺激 画什么?线条?DrawLine 点?Dr
47、awDots 多边形?FillPoly/FramePoly 椭圆?FillOval/FrameOval 文字?DrawText 照片?MakeTexture+DrawTexture 效果?位置?透明?Screen函数:DrawDots子函数 画点:DrawDots Screen(DrawDots,w,xy,size,color,center,type);用来画一个或一堆点,它的参数们:w:窗口的“指针”xy:两行的行向量,每个点的x、y座标各占一行 size:点的宽度,以像素计 color:(每个)点的颜色(R G B 或 R G B A)center:点座标的相对中心 type:方点,圆点,
48、还是抗锯齿的圆点Screen函数:DrawLine子函数 画线:DrawLine Screen(DrawLine,w,color,fromH,fromV,toH,toV,penWidth);用来画一条线,它的参数们:w:窗口的“指针”color:线的颜色 from/to H/V:起点/终点的x/y座标 penWidth:线条的粗细Screen函数:MakeTexture子函数 Texture-由图片转换而来的“纹理”,便于快速贴图 怎么把图片转成纹理?image=imread(photo.jpg);textureIndex=Screen(MakeTexture,w,image);返回值text
49、ureIndex就是这个纹理的“指针”DrawTexture子函数 把纹理(即照片)画到屏幕上 Screen(DrawTexture,w,textureIndex);把照片画到哪里?源矩阵 src_rect=Screen(Rect,tex);画纹理的哪部分(默认全画)?摆在中心 ctr_rect=CenterRect(src_rect,wRect);偏移 dst_rect=OffsetRect(dst_rect,-150,150);画到偏移完的目标上 Screen(DrawTexture,w,tex,src_rect,dst_rect);Screen函数:Flip子函数 Psychtoolbo
50、x有两个缓冲区(“画板”)屏幕上显示的只是其中一面 显示的同时可以在下面的画板中继续画 需要更新的时候,直接切换,速度快 把画板切换上去 马上就翻:Screen(Flip,w);T时刻(或之后)再翻:vbl=Screen(Flip,w,T);返回值vbl是真正刷屏时刻的时间戳(GetSecs)Screen函数:CloseAll子函数 为了避免程序运行时出错,而无法执行到CloseAll就卡在中间关不了全屏窗口 try screens=Screen(Screens);screenNumber=max(screens);w,rect=Screen(OpenWindow,screenNumber);
侵权处理QQ:3464097650--上传资料QQ:3464097650
【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。