Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
EyeTracking/gaze_dispersion.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
137 lines (119 sloc)
5.02 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addpath('gbvs'); | |
addpath('eyetrackingstory_01/pilot_data/free_ebooks'); | |
addpath('eyetrackingstory_01/stimuli/free_ebooks/young/withbackground'); | |
addpath('eyetrackingstory_01/stimuli/free_ebooks/old/withbackground'); | |
output_root_blank = 'Output/NoImage/'; | |
output_root_image = 'Output/WithImage/'; | |
output_root_vizdata = 'Output/visualization data/'; | |
% gaze dispersion | |
data = readtable('samplereport_pilotdata.xlsx'); | |
image = char(''); | |
blank_img = repmat(255, 1, 1, 1); | |
file = char(''); | |
eye = char(''); | |
output_filename = ''; | |
matrix = zeros(1, 1); | |
image_maps = containers.Map; | |
vectors = containers.Map; | |
first = true; | |
for row_index = 1:height(data) | |
disp(row_index); | |
row = data(row_index, :); | |
string_array = table2array(row(1, {'DATA_FILE', 'image', 'EYE_TRACKED'})); | |
[current_file, current_image, current_eye] = string_array{:}; | |
subject = erase(current_file, {'.edf'}); | |
if(~isKey(image_maps, current_image)) | |
image_object = imread(current_image); | |
[img_height, img_width, img_dim] = size(image_object); | |
blank_img = repmat(255, img_height, img_width, img_dim); | |
image_maps(current_image) = containers.Map; | |
vectors(current_image) = containers.Map; | |
end | |
image_map = image_maps(current_image); | |
vector_image_set = vectors(current_image); | |
if(~isKey(image_map, subject)) | |
image_map(subject) = zeros(img_height, img_width); | |
vector_image_set(subject) = containers.Map; | |
vector_subject_set = vector_image_set(subject); | |
vector_subject_set('x') = zeros(1, img_width); | |
vector_subject_set('y') = zeros(1, img_height); | |
vector_image_set(subject) = vector_subject_set; | |
end | |
matrix = image_map(subject); | |
vector_subject_set = vector_image_set(subject); | |
x_array = vector_subject_set('x'); | |
y_array = vector_subject_set('y'); | |
if(strcmp(current_eye, 'Right')) | |
number_array = table2array(row(1, {'RIGHT_GAZE_X', 'RIGHT_GAZE_Y'})); | |
else | |
number_array = table2array(row(1, {'LEFT_GAZE_X', 'LEFT_GAZE_Y'})); | |
end | |
if(any(isnan(number_array))) | |
continue | |
end | |
number_array_cell = num2cell(number_array); | |
[x_coord, y_coord] = number_array_cell{:}; | |
x_coord = int16(x_coord); | |
y_coord = int16(y_coord); | |
if(x_coord > 0 && x_coord <= img_width && y_coord > 0 && y_coord <= img_height) | |
matrix(y_coord, x_coord) = matrix(y_coord, x_coord) + 1; | |
image_map(subject) = matrix; | |
image_maps(current_image) = image_map; | |
x_array(x_coord) = 1; | |
y_array(y_coord) = 1; | |
vector_subject_set('x') = x_array; | |
vector_subject_set('y') = y_array; | |
vector_image_set(subject) = vector_subject_set; | |
vectors(current_image) = vector_image_set; | |
end | |
end | |
disp('Creating heatmap images...'); | |
image_keys = keys(image_maps); | |
for key_index = 1:length(image_keys) | |
current_image_cell = image_keys(key_index); | |
current_image = current_image_cell{:}; | |
mkdir(strcat(output_root_vizdata, current_image)); | |
image_map = image_maps(current_image); | |
subject_keys = keys(image_map); | |
for subject_index = 1:length(subject_keys) | |
image_data = im2double(imread(current_image)); | |
image_data = flip(image_data, 1); | |
subject_cell = subject_keys(subject_index); | |
subject = subject_cell{:}; | |
mkdir(strcat(output_root_vizdata, current_image, '/', subject)); | |
matrix = image_map(subject); | |
% flip over x-axis because eye-link (0,0) is at top left | |
matrix = flip(matrix, 1); | |
csv_filename = strcat(output_root_vizdata, current_image, '/', subject, '/matrix.csv'); | |
csvwrite(csv_filename, matrix); | |
[row, col] = find(matrix); | |
plot(col, row, 'r.'); | |
xlim([0 img_width]); | |
ylim([0 img_height]); | |
set(gcf, 'position', [0, 0, img_width, img_height]); | |
saveas(gcf, strcat(output_root_vizdata, current_image, '/', subject, '/scatter.png')); | |
clearvars gcf gca; | |
pts_x = linspace(0, img_width, img_width/10); | |
pts_y = linspace(0, img_height, img_height/10); | |
N = histcounts2(row(:), col(:), pts_y, pts_x); | |
[xG, yG] = meshgrid(-5:5); | |
sigma = 2.5; | |
g = exp(-xG.^2./(2.*sigma.^2)-yG.^2./(2.*sigma.^2)); | |
g = g./sum(g(:)); | |
figure, imshow(image_data), hold on; | |
hImg = imagesc(pts_x, pts_y, conv2(N, g, 'same')); | |
set(hImg, 'AlphaData', 0.6); | |
%imagesc(pts_x, pts_y, conv2(N, g, 'same')); | |
%imagesc(conv2(N, g, 'same')); | |
axis equal; | |
set(gca, 'XLim', pts_x([1 end]), 'YLim', pts_y([1 end]), 'YDir', 'normal'); | |
saveas(gcf, strcat(output_root_vizdata, current_image, '/', subject, '/heatmap_image.png')); | |
clearvars gcf gca; | |
imagesc(pts_x, pts_y, conv2(N, g, 'same')); | |
axis equal; | |
set(gca, 'XLim', pts_x([1 end]), 'YLim', pts_y([1 end]), 'YDir', 'normal'); | |
saveas(gcf, strcat(output_root_vizdata, current_image, '/', subject, '/heatmap.png')); | |
clearvars gcf gca image_data; | |
close all; | |
end | |
end |