-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerateGIF.m
More file actions
187 lines (153 loc) · 5.32 KB
/
generateGIF.m
File metadata and controls
187 lines (153 loc) · 5.32 KB
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
%% Author: Mengmi Zhang
%% Kreiman Lab
%% web: http://klab.tch.harvard.edu/
%% Date: April 5, 2018
clear all;
close all;
clc;
% -- layers: numlayer, numtemplates, convsize
% -- layers: 5, 64, 14
% -- layers: 10, 128, 7
% -- layers: 17, 256, 4
% -- layers: 23, 512, 4
% -- layers: 24, 512, 2
% -- layers: 30, 512, 2
% -- layers: 31, 512, 1
FixData = [];
Fix_posx = {};
Fix_posy = {};
LayerList = [1];
indexlist = {0,0,0,0,0,0};
receptiveSize = 100; %for IOR and oracle checking
arraysize = 80; %maximum number of fixations;
w = 1024;
h = 1280;
NumImage = 1; %1 waldo image only
scoremat = zeros(NumImage, arraysize);
vi_AM = {};
vi_FP = {};
for i = 1:NumImage
trialname = 'cropped_2_1.jpg';
path = ['sampleimg/gt' trialname(8:end) ];
gt = imread(path);
gt = imresize(gt,[w,h]);
gt = mat2gray(gt);
gt = im2bw(gt,0.5);
gt = double(gt);
img = imread(['sampleimg/' trialname]);
img = imresize(img, [w, h]);
display(['img: ' num2str(i)]);
piecedir = dir(['choppedwaldo/img_id' trialname(8:end-4) '_*_layertopdown.mat']);
wholeimg = zeros(length(LayerList),size(img,1), size(img,2));
posx = [];
posy = [];
for l = 1: length(LayerList)
for j = 1: length(piecedir)
%j
input = load(['choppedwaldo/' piecedir(j).name]);
comp = imread(['choppedwaldo/' piecedir(j).name(1:end-17) '.jpg' ]);
input = input.x;
input = imresize(input, [size(comp,1) size(comp,2)]);
C = strsplit(piecedir(j).name,'_');
startpos = str2num(C{5});
endpos = str2num(C{6});
wholeimg(l,startpos: startpos+size(comp,1) - 1, endpos: endpos+size(comp,2) - 1) = input;
end
wholeimg(l,:,:) = mat2gray(wholeimg(l,:,:));
end
wholeimg = squeeze(mean(wholeimg,1));
%apply IOR
found = 0;
fixtime = 1;
salimg = wholeimg;
for f = 1: arraysize
[Y,idx] = max(salimg(:));
[x y]= ind2sub(size(salimg),idx);
posx = [posx; x];
posy = [posy; y];
fixatedPlace_leftx = x - receptiveSize/2 + 1;
fixatedPlace_rightx = x + receptiveSize/2;
fixatedPlace_lefty = y - receptiveSize/2 + 1;
fixatedPlace_righty = y + receptiveSize/2;
if fixatedPlace_leftx < 1
fixatedPlace_leftx = 1;
end
if fixatedPlace_lefty < 1
fixatedPlace_lefty = 1;
end
if fixatedPlace_rightx > size(gt,1)
fixatedPlace_rightx = size(gt,1);
end
if fixatedPlace_righty > size(gt,2)
fixatedPlace_righty = size(gt,2);
end
fixatedPlace = gt(fixatedPlace_leftx:fixatedPlace_rightx, fixatedPlace_lefty:fixatedPlace_righty);
fixatedRGB = img(fixatedPlace_leftx:fixatedPlace_rightx, fixatedPlace_lefty:fixatedPlace_righty,:);
%%%%%%%%%%%%%%% display the search process %%%%%%%%%%%%%%%
displaysalimg = imresize( salimg, [480 640]);
subplot(2,2,1);
displaystimuli = imread(['sampleimg/' trialname ]);
displaystimuli = imresize(displaystimuli, [480 640]);
imshow(displaystimuli);
title('stimuli');
subplot(2,2,2);
displaygt = imread(['sampleimg/waldo.JPG']);
imshow(displaygt);
title('target');
subplot(2,2,3);
displayheat = heatmap_overlay(displaystimuli, displaysalimg);
radius = 25;
displayheat = insertShape(displayheat,'circle',[y/h*640,x/w*480 radius],'Color','r','linewidth',8);
imshow(displayheat);
title('attention map');
%hold on;
%plot(y/h*640,x/w*480,'ro','markers',20,'linewidth',2);
hold off;
subplot(2,2,4);
imshow(fixatedRGB);
title('fixated area');
vi_AM{f} = imresize(displayheat,[930 1462]);
vi_FP{f} = imresize(fixatedRGB,[753 730]);
drawnow;
%pause(0.5);
%%%%%%%%%%%%%%% display the search process %%%%%%%%%%%%%%%
if sum(sum(fixatedPlace)) > 0
found = 1;
break;
else
found = 0;
fixtime = fixtime + 1;
salimg(fixatedPlace_leftx:fixatedPlace_rightx, fixatedPlace_lefty:fixatedPlace_righty) = 0;
salimg = mat2gray(salimg);
end
end
Fix_posx = [Fix_posx posy'];
Fix_posy = [Fix_posy posx'];
display(['img id: ' num2str(i) '; target found at fixation step: ' num2str(fixtime) ]);
if fixtime <= arraysize
scoremat(i,fixtime) = 1;
end
end
FixData.Fix_posx = Fix_posx;
FixData.Fix_posy = Fix_posy;
filename = 'GIF/AM.gif'; % Specify the output file name
nImages = 7;
time = 0.5;
for idx = 1:nImages
[A,map] = rgb2ind(vi_AM{idx},256);
if idx == 1
imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',time);
else
imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',time);
end
end
filename = 'GIF/FP.gif'; % Specify the output file name
nImages = 7;
for idx = 1:nImages
[A,map] = rgb2ind(vi_FP{idx},256);
if idx == 1
imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',time);
else
imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',time);
end
end