/usr/share/psychtoolbox-3/PsychOpenGL/PsychGLEyetracking/OldEyetrackertest.m is in psychtoolbox-3-common 3.0.11.20131230.dfsg1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | function Eyetrackertest(imfilename)
% Eyetrackertest -- Testscript to test out a few weird
% ideas about Computervision + GLSL based eye tracking
% Not seriously useful for anything in the near future.
%
% Written by Mario Kleiner.
global rayvisdisplaylist;
try
% Assign default name for test-image or test-movie:
if nargin < 1 || isempty(imfilename)
if IsWin
imfilename='U:/test/eyeroi.jpg';
end;
if IsLinux
imfilename='/home/kleinerm/test/eyeroi.jpg';
end;
end;
imfilename
% Read test-image:
inimage=imread(imfilename);
inimage=rgb2gray(inimage);
inimage=inimage(1:128, 1:128);
size(inimage)
kernel = fspecial('gaussian', 11, 5.5);
inimage2=single(inimage);
myimg=conv2(kernel, inimage2);
tic;
for i=1:1000
myimg=conv2(kernel, inimage2);
end
toc
AssertOpenGL;
oldsynctest = Screen('Preference','SkipSyncTests',1);
InitializeMatlabOpenGL;
moglcore('DEBUGLEVEL',2);
[win , winRect] = Screen('OpenWindow', max(Screen('Screens')));
Screen('FillRect', win, [0 0 255]);
Screen('Flip', win);
width=RectWidth(winRect);
height=RectHeight(winRect);
AssertGLSL;
% Create texture from input image:
imtex=Screen('MakeTexture', win, inimage);
imrect=Screen('Rect', imtex);
imloc=CenterRect(imrect, Screen('Rect', win));
% Show input image:
Screen('DrawTexture', win, imtex);
Screen('Flip',win);
Waitkey;
% Create offscreen window of proper size to hold blurred
% input image:
img_blurred=Screen('OpenOffscreenWindow', win, [], imrect);
% Create offscreen window 1 for remapped image:
pingpongrect=[0 0 256 360];
pingpong1=Screen('OpenOffscreenWindow', win, [0 0 0 ], pingpongrect);
glpingpong1=Screen('GetOpenGLTexture', win, pingpong1);
pingpong2=Screen('OpenOffscreenWindow', win, [0 0 0 ], pingpongrect);
glpingpong2=Screen('GetOpenGLTexture', win, pingpong2);
glGetError;
% Build and initialize horizontal "clamped derivative" shader:
horizderivshader = LoadGLSLProgramFromFiles('HorizontalDerivativeShader');
% Build and initialize horizontal "sum" shader:
horizsumshader = LoadGLSLProgramFromFiles('HorizontalSumShader');
% Build and initialze horizontal edge detection shader:
horizedgedistshader = LoadGLSLProgramFromFiles('HorizontalMinimumEdgeDistanceShader');
% Build and initialize gaussian blur shader (5x5, stddev=1.5):
blurshader = Create2DGaussianBlurShader(2.5, 11);
% Select blurshader:
glUseProgram(blurshader);
% Draw input image imtex into img_blurred, applying the blurshader:
Screen('CopyWindow', imtex, img_blurred);
glFinish;
tic
for i = 1:1000
Screen('CopyWindow', imtex, img_blurred);
end
glFinish;
toc
% Disable blurshader:
glUseProgram(0);
% Build visualization display list:
% Get an OpenGL texture handle for the blurred image:
[glimg_blurred textarget] = Screen('GetOpenGLTexture', win, img_blurred);
% Set framebuffer as drawing target:
glDisable(GL.DEPTH_TEST);
redrawcount = 0;
tstart = GetSecs;
while(1)
% Query mouse position and button state:
[mx, my, buttons]=GetMouse;
if any(buttons)
break;
end;
% Redraw input image:
Screen('DrawTexture', win, img_blurred);
% Remap cursor position to position in blurred input image (cx,cy):
cx= mx - imloc(RectLeft);
cy= imloc(RectBottom) - my;
targetx=0;
targety=0;
% Sample into pingpong1 - Window:
Screen('BeginOpenGL', pingpong1);
GLSLResampleRayStarToRectangle(glimg_blurred, cx, cy, 0, 359, 1, 256, targetx, targety);
% Visualize sampling star:
Screen('glPoint', win, 255, mx, my);
glPushMatrix;
glTranslatef(mx, my, 0);
glCallList(rayvisdisplaylist);
glPopMatrix;
% Draw pingpong image:
Screen('DrawTexture', win, pingpong1, [], pingpongrect);
% Compute per-beam summed positive derivative:
% glUseProgram(horizderivshader);
Screen('CopyWindow', pingpong1, pingpong2);
glUseProgram(0);
% Draw derivative image:
Screen('DrawTexture', win , pingpong2, [], OffsetRect(pingpongrect, 0, 400));
% Compute per-beam sum of derivatives:
% glUseProgram(horizsumshader);
glUseProgram(horizedgedistshader);
Screen('CopyWindow', pingpong2, pingpong1, [], [0 0 1 360]);
glUseProgram(0);
% Draw per-beam sum:
Screen('DrawTexture', win , pingpong1, [0 0 1 360], OffsetRect(pingpongrect, 800, 0), 0, 0);
% Get it back:
weights=Screen('GetImage', pingpong1, [0 0 1 360]);
wvals = (255 - double(weights(:,1,1)))/255;
%wvals = wvals/sum(wvals);
% Compute weighted shift vector:
dx=0; dy=0;
for angle=1:360
dx = dx + wvals(angle)*cos(angle/360*2*3.141592654);
dy = dy + wvals(angle)*sin(angle/360*2*3.141592654);
Screen('DrawLine', win, [255 255 0], 0, angle-1, (1-wvals(angle))*255, angle-1);
end;
ssize=2;
n=norm([dx dy]);
if n==0
n=1;
end;
dx = dx / n * ssize;
dy = dy / n * ssize;
Screen('DrawLine', win, 255, double(mx), double(my), double(mx+dx*100), double(my+dy*100));
mx=mx+dx;
my=my+dy;
if mod(redrawcount, 2)
% SetMouse(mx,my);
end;
Screen('Flip',win, 0, 0, 2-0);
redrawcount = redrawcount + 1;
end;
avgupdates = redrawcount / (GetSecs - tstart)
Screen('EndOpenGL', win);
Screen('Close', win);
return;
% Close onscreen window and release all other ressources:
Screen('CloseAll');
% Reenable Synctests after this simple demo:
Screen('Preference','SkipSyncTests', oldsynctest);
% Well done!
return;
%catch
Screen('CloseAll');
psychrethrow(lasterror);
end;
function Waitkey
while KbCheck; end;
KbWait;
while KbCheck; end;
return;
function GLSLResampleRayStarToRectangle(inputTexture, cx, cy, startAngle, endAngle, incrementAngle, raylength, tx, ty)
global GL;
persistent raydisplaylist;
global rayvisdisplaylist;
if isempty(GL)
error('GLResampleRayStarToRectangle called without properly setup Matlab-OpenGL!');
end;
if isempty(raydisplaylist)
% First invocation: Need to setup sampling display list:
raydisplaylist = glGenLists(1);
glNewList(raydisplaylist, GL.COMPILE);
angle=startAngle;
y=0;
glBegin(GL.LINES);
while (angle <= endAngle)
radians = angle / 360 * 2 * 3.141592654;
glTexCoord2f(0, 0); % Each sampling line starts at (cx,cy)+(0,0).
glVertex2f(0, y); % Target is (0,y) for the y'th line.
% Sampling line ends at a distance of 'raylength' away in
% directions of 'angle'
glTexCoord2f(raylength * cos(radians), raylength * sin(radians));
glVertex2f(raylength, y);
% Submit next sampling line...
angle= angle + incrementAngle;
y=y+1;
end;
glEnd;
glEndList;
% Build the same object, this time for visualization:
rayvisdisplaylist = glGenLists(1);
glNewList(rayvisdisplaylist, GL.COMPILE);
angle=startAngle;
y=0;
glColor3f(0,255,0);
glBegin(GL.LINES);
while (angle <= endAngle)
radians = angle / 360 * 2 * 3.141592654;
glVertex2f(10 * cos(radians), 10 * sin(radians));
% Sampling line ends at a distance of 'raylength' away in
% directions of 'angle'
glVertex2f(raylength * cos(radians), raylength * sin(radians));
% Submit next sampling line...
angle= angle + incrementAngle;
y=y+1;
end;
glEnd;
glEndList;
end;
if isempty(tx)
tx=0;
end;
if isempty(ty)
ty=0;
end;
% Visualize sampling star:
glMatrixMode(GL.MODELVIEW);
glPushMatrix;
% Set drawing position for remapped rectangle:
glTranslatef(tx, ty, 0);
% Setup texture matrix so center of our sampling raystar is at position:
% (cx,cy):
glMatrixMode(GL.TEXTURE);
glPushMatrix;
glLoadIdentity;
glTranslatef(cx, cy, 0);
% Bind input texture:
glPushAttrib(mor(GL.ENABLE_BIT, GL.TEXTURE_BIT));
glBindTexture(GL.TEXTURE_RECTANGLE_EXT, inputTexture);
glEnable(GL.TEXTURE_RECTANGLE_EXT);
glTexEnvfv(GL.TEXTURE_ENV,GL.TEXTURE_ENV_MODE,GL.REPLACE);
% Draw our star, thereby resampling/remapping the colors along the stars
% rays into a rectangular shaped region of the framebuffer or output
% texture:
glCallList(raydisplaylist);
% Restore previous state:
glPopAttrib;
glBindTexture(GL.TEXTURE_RECTANGLE_EXT, 0);
glPopMatrix;
glMatrixMode(GL.MODELVIEW);
glPopMatrix;
return;
|