-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui.m
338 lines (238 loc) · 10.4 KB
/
gui.m
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
function varargout = gui(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_OpeningFcn, ...
'gui_OutputFcn', @gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before gui is made visible.
function gui_OpeningFcn(hObject, eventdata, handles, varargin)
if exist('features','var') && exist('labels','var')
set(handles.lblFeaturesStatus, 'String', 'Dataset loaded');
else
set(handles.lblFeaturesStatus, 'String', 'Dataset is not loaded');
end
handles.N = str2double(get(handles.txtCount,'String'));
set(handles.table, 'data', zeros(handles.N,1));
set(handles.table, 'ColumnEditable', true);
fTypes = {'colorHistogram' 'edgeHistogram' 'colorAutoCorrelogram' 'colorMoments' 'coocMatrix'};
handles.fTypes = fTypes;
handles.selTypeInds = 1:length(fTypes);
set(handles.lstFeatures, 'Value', handles.selTypeInds);
set(handles.lstFeatures, 'String', handles.fTypes);
set(handles.scrHor, 'Visible', 'off');
handles.distType = 'cityblock';
handles.dataset = '1000';
% Choose default command line output for gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in btnLoadFeatures.
function btnLoadFeatures_Callback(hObject, eventdata, handles)
if exist([handles.dataset '.mat'],'file') > 0
load(handles.dataset);
set(handles.lblFeaturesStatus, 'String', 'Dataset loaded');
handles.features = features;
handles.labels = labels;
guidata(hObject, handles);
else
set(handles.lblFeaturesStatus, 'String', 'Dataset is not loaded');
end
% --- Executes on button press in btnCreateFeatures.
function btnCreateFeatures_Callback(hObject, eventdata, handles)
[features, labels] = prepareDataset(['images' handles.dataset], handles.fTypes);
set(handles.lblFeaturesStatus, 'String', 'Dataset loaded');
handles.features = features;
handles.labels = labels;
beep;
guidata(hObject, handles);
% --- Executes on button press in btnSaveFeatures.
function btnSaveFeatures_Callback(hObject, eventdata, handles)
features = handles.features;
labels = handles.labels;
if exist('features','var') && exist('labels','var')
save(handles.dataset, 'features','labels');
helpdlg('Features have been saved successfuly!');
else
errordlg('There are no features to save!');
end
function txtCount_Callback(hObject, eventdata, handles)
% Hints: get(hObject,'String') returns contents of txtCount as text
% str2double(get(hObject,'String')) returns contents of txtCount as a double
handles.N = str2double(get(handles.txtCount,'String'));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function txtCount_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in btnLoadImage.
function btnLoadImage_Callback(hObject, eventdata, handles)
[fname, pthname] = uigetfile(['images' handles.dataset '/*.jpg'], 'Select the query image');
if (fname ~= 0)
handles.imgPath = strcat(pthname, fname);
slashInds = strfind(pthname,filesep);
handles.imgLabel = str2double(pthname(slashInds(end-1)+1:slashInds(end)-1));
guidata(hObject, handles);
end
% --- Executes on button press in btnQuery.
function btnQuery_Callback(hObject, eventdata, handles)
performQuery(hObject, handles, true);
% Performs the image query
function performQuery(hObject, handles, rstWeights)
if isfield(handles, 'imgPath')
if rstWeights
handles.features = resetWeights(handles.features, handles.selTypeInds);
end
features = handles.features;
labels = handles.labels;
if rstWeights
f = extractFeatures(handles.imgPath, handles.fTypes, features(:,3));
handles.f = f;
else
f = handles.f;
end
N = handles.N;
set(handles.table,'data', zeros(N,1));
inds = getClosestImages(features, f, N, handles.distType, handles.selTypeInds);
yCnt = 2;
xCnt = ceil(N/2);
plotWidth = 120*xCnt;
% Set the size of the inner panel to the full plot size so that it can be scrolled inside the outer panel
set(handles.pnlInner, 'Position', [0 0 plotWidth 350]);
% Display the images returned by the query
axes('Unit', 'pixels', 'Parent', handles.pnlInner);
for i = 1:N
% Compute the index in the subplot that will result in the desired layout
tInd = ceil(i/2);
if mod(i,2) == 0;
tInd = tInd + ceil(N/2);
end
imgPath = labels{inds(i),2};
I = imread(imgPath);
subtightplot(yCnt, xCnt, tInd);
imshow(I, []);
title(num2str(i));
end
% Compute the precision
precision = 0;
for i = 1:N
if labels{inds(i),1} == handles.imgLabel
precision = precision + 1;
end
end
precision = precision / N;
set(handles.lblPrecision, 'String', num2str(precision));
% Generate the feedback automatically
feedback = generateFeedback(labels, inds, handles.imgLabel, N);
set(handles.table,'data',feedback);
set(handles.scrHor, 'Visible', 'on');
set(handles.scrHor, 'Value', 0);
guidata(hObject, handles);
else
errordlg('No image is loaded');
end
% --- Executes on button press in btnUpdateResults.
function btnUpdateResults_Callback(hObject, eventdata, handles)
feedback = get(handles.table, 'data');
handles.features = updateWeights(handles.features, handles.f, feedback, handles.N, handles.distType, handles.selTypeInds);
performQuery(hObject, handles, false);
% --- Executes on button press in btnTestPerformance.
function btnTestPerformance_Callback(hObject, eventdata, handles)
if isfield(handles, 'features') && isfield(handles, 'labels')
disp('Measuring performance...');
feedbackCnt = str2double(get(handles.txtFeedbackCount,'String'));
tic;
results = testPerformance(handles.features, handles.labels, handles.N, handles.distType, handles.selTypeInds, feedbackCnt);
elapsedTime = toc;
disp(results);
[MAP, MAR] = wMean(results);
fprintf('Mean precision : %.2f. Mean recall: %.2f. Elapsed time : %.2f\n', MAP, MAR, elapsedTime);
beep;
guidata(hObject, handles);
else
errordlg('No dataset is loaded!');
end
% --- Executes on slider movement.
function scrHor_Callback(hObject, eventdata, handles)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
outerPnlPos = get(handles.pnlOuter, 'Position');
innerPnlPos = get(handles.pnlInner, 'Position');
maxOffset = innerPnlPos(3) - outerPnlPos(3);
if maxOffset < 0
maxOffset = 0;
end
scrPos = get(hObject,'Value');
innerPnlPos(1) = -scrPos * maxOffset;
set(handles.pnlInner, 'Position', innerPnlPos);
% --- Executes on selection change in cmbDistMetric.
function cmbDistMetric_Callback(hObject, eventdata, handles)
% Hints: contents = cellstr(get(hObject,'String')) returns cmbDistMetric contents as cell array
% contents{get(hObject,'Value')} returns selected item from cmbDistMetric
cmbContents = cellstr(get(hObject,'String'));
handles.distType = cmbContents{get(hObject,'Value')};
guidata(hObject, handles);
% --- Executes on selection change in lstFeatures.
function lstFeatures_Callback(hObject, eventdata, handles)
% Hints: contents = cellstr(get(hObject,'String')) returns lstFeatures contents as cell array
% contents{get(hObject,'Value')} returns selected item from lstFeatures
handles.selTypeInds = get(hObject,'Value');
guidata(hObject, handles);
% --- Executes on selection change in cmbDataset.
function cmbDataset_Callback(hObject, eventdata, handles)
% Hints: contents = cellstr(get(hObject,'String')) returns cmbDataset contents as cell array
% contents{get(hObject,'Value')} returns selected item from cmbDataset
contents = cellstr(get(hObject,'String'));
handles.dataset = contents{get(hObject,'Value')};
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function scrHor_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes during object creation, after setting all properties.
function cmbDistMetric_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function txtFeedbackCount_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function lstFeatures_CreateFcn(hObject, eventdata, handles)
% hObject handle to lstFeatures (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function cmbDataset_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function [MAP, MAR] = wMean(a)
MAP = sum(a(:,1) .* a(:,3)) / sum(a(:,3));
MAR = sum(a(:,2) .* a(:,3)) / sum(a(:,3));