[eyetracker] new data, updated assignment, part of solution

This commit is contained in:
2021-02-01 10:37:01 +01:00
parent e953a9aacb
commit 56d40404bd
17 changed files with 592 additions and 25 deletions

View File

@@ -0,0 +1,15 @@
function [time] = convert_indices_to_time(indices, framerate)
% [time] = convert_indices_to_time(indices, frame_rate)
%
% Converts the vector of frame indices to time. The time vector will start
% at zero.
%
% Arguments:
% indices: vecor, the frame indices
% framerate:, scalar, the framerate in Hz
%
% Returns:
% time: vector, the time vector
indices = indices - indices(1);
time = indices / framerate;

View File

@@ -0,0 +1,7 @@
function [x, y] = convert_pixel_to_meter(x_pos, y_pos, width_m, height_m, width_pixel, height_pixel)
x_resolution = width_m/width_pixel;
y_resolution = height_m/height_pixel;
x = x_pos * x_resolution;
y = y_pos * y_resolution;

View File

@@ -0,0 +1,17 @@
function [true_x, true_y] = correct_eye_positions(eye_x, eye_y, x_offset, y_offset)
% [true_x, true_y] = correct_eye_positions(eye_x, eye_y, x_offset, y_offset)
%
% Applies the measured offset between x- and y- fixation coordinates to
% correct the recorded x- and y- eye coordinates.
%
% Arguments:
% eye_x, eye_y: vector, the recorded x- and y-eye positions.
% x_offset, y_offset: scalar, the x- and y-offset between
% fixation point and eye position during fixation.
%
% Returns:
% true_x, true_y: vector, the corrected eye positions
true_x = eye_x + x_offset;
true_y = eye_y + y_offset;

View File

@@ -0,0 +1,22 @@
function [new_x, new_y] = fix_eye_lost(x, y, frame_index, eye_found)
% [new_x, new_y] = fix_eye_lost(x, y, frame_index, eye_found)
%
% Fixes intervals in the data in which eye detection was lost. Function uses linear interpolation.
%
% Arguments:
% x, y: vector, the eye positions
% frame_indices: vector, same length as x and y, the frame
% indices (or times)
% eye_found: vector, ones and zeros indicate whether the eye was
% reliably found by the tracker or not.
%
% Returns:
% new_x, new_y: vectors of double, the new positions
valid_x_positions = double(x(eye_found == 1));
valid_y_positions = double(y(eye_found == 1));
valid_frame_indices = double(frame_index(eye_found == 1));
new_x = interp1(valid_frame_indices, valid_x_positions, double(frame_index));
new_y = interp1(valid_frame_indices, valid_y_positions, double(frame_index));

View File

@@ -0,0 +1,20 @@
function [x_offset, y_offset] = get_fixation_offset(eye_x, eye_y, event_marker, fixation_event_marker, fixation_x, fixation_y)
% [x_offset, y_offset] = get_fixation_offset(eye_x_pos, eye_y_pos, event_marker, fixation_event_marker, fixation_x, fixation_y)
%
% Estimates the x, and y-offset between screen position and reported eye position while fixating
%
% Arguments:
% eye_x, eye_y: The eye positions reported by the tracker, vectors of coordinates
% event_marker: vector, the recorded event markers
% fixation_event_marker: scalar, the event marker that represents the fixation period
% fixation_x, fixation_y: scalar, the x and y position of the fixation cross on the screen
%
% Returns:
% x_offset, y_offset: scalar the deviation between reported x
% and y positions and the true positions.
eye_fixation_x_pos = median(eye_x(event_marker == fixation_event_marker));
eye_fixation_y_pos = median(eye_y(event_marker == fixation_event_marker));
x_offset = fixation_x - eye_fixation_x_pos;
y_offset = fixation_y - eye_fixation_y_pos;

View File

@@ -0,0 +1,21 @@
function [frame_index, gaze_x, gaze_y, marker, eye_found] = load_eye_tracking_data(filename)
% [frame_index, gaze_x, gaze_y, marker, eye_found] = load_eye_tracking_data(filename)
%
% Loads the eyetracking data stored in the specified file. Raises an error,
% if the file was not found.
%
% Arguments:
% filename: string, the path to the data file
%
% Returns:
% frame_index: vector, the recorded frame numbers
% gaze_x, gaze_y: vector, the recorded x and y eye positions
% marker: vector, the recorded event marker
% eye_found: vector, 1, or zero values indicating whether or not
% the eye was actually detected by the tracker.
if exist(filename, "file") ~= 2
error("file %s does not exist or is no mat file", filename);
end
load(filename, "gaze_x", "gaze_y", "frame_index", "marker", "eye_found")

View File

@@ -0,0 +1,35 @@
% Project Eyetracking
% Name: John Doe
% Matrikelnummer: 123456789
clear
close all
%% Definition of constants
framerate = 60; % Hz
screen_width = 0.376; % m
screen_height = 0.301; % m
screen_resolution_x = 1280; % pixel
screen_resolution_y = 1024; % pixel
fixation_x = screen_resolution_x/2; % position of fixation cross on screen
fixation_y = screen_resolution_y/2; % position of fixation cross on screen
fixation_marker = 1; % event marker for fixtion period
movement_marker = 2; % event marker for free movement period
data_files = [fullfile("..", "data", "1_1.mat"), fullfile("..", "data", "1_5.mat")];
scenes = [fullfile("..", "data", "Genesis_VIII.png"), fullfile("..", "data", "Genesis_XXXIX.png")];
%% Process files
for i = 1:length(data_files)
file = data_files(i);
scene = scenes(i);
fprintf("processing: %s\n", file)
[frame_indices, x, y, event_marker, eye_found] = load_eye_tracking_data(file);
[x_offset, y_offset] = get_fixation_offset(x, y, event_marker, fixation_marker, fixation_x, fixation_y);
[true_x, true_y] = correct_eye_positions(x, y, x_offset, y_offset);
[true_x, true_y] = fix_eye_lost(true_x, true_y, frame_indices, eye_found);
time = convert_indices_to_time(frame_indices, framerate);
fig = plot_image_position_overlay(true_x, true_y, time, scene);
saveas(fig, sprintf("test_%i", i), "pdf")
end

View File

@@ -0,0 +1,20 @@
function fig = plot_image_position_overlay(x, y, time, image)
if length(x) ~= length(y) || length(y) ~= length(time)
error("Length of vectors x, y, and time does not match!");
end
if ~exist(image, "file")
error("Image %s could not be found!", image);
end
fig = figure();
imshow(image)
hold on
scatter(x, y, [], time)
c = colorbar();
fig.PaperSize = [10.0, 10.0];
fig.PaperPosition = [0., 0., 10.0, 10.0];
c.Label.String = "time [s]";
c.Label.FontSize = 9;