-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.m
469 lines (428 loc) · 17.7 KB
/
main.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
function varargout = main(varargin)
% MAIN MATLAB code for main.fig
% MAIN, by itself, creates a new MAIN or raises the existing
% singleton*.
%
% H = MAIN returns the handle to a new MAIN or the handle to
% the existing singleton*.
%
% MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAIN.M with the given input arguments.
%
% MAIN('Property','Value',...) creates a new MAIN or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before main_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to main_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help main
% Last Modified by GUIDE v2.5 26-Jul-2019 14:04:34
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @main_OpeningFcn, ...
'gui_OutputFcn', @main_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 main is made visible.
function main_OpeningFcn(hObject, ~, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to main (see VARARGIN)
% Choose default command line output for main
handles.output = hObject;
handles.readSuccess = 0;
set(gcf, 'WindowButtonDownFcn', @mouseClicked);
set(gcf, 'WindowButtonUpFcn', @mouseReleased);
set(gcf, 'WindowKeyPressFcn', @hotkeyPressed);
set(gcf, 'WindowScrollWheelFcn', @scrollWhellFunction);
set(gcf, 'WindowButtonMotionFcn', @updateCursor);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes main wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = main_OutputFcn(~, ~, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in ReadImage.
function ReadImage_Callback(hObject, ~, handles)
% hObject handle to ReadImage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
path = get(handles.FolderPath, 'String');
if ~exist(path, 'dir')
set(handles.ReadResult, 'String', 'The path is not valid!');
return;
end
formats = get(handles.ImageFormat, 'String');
dirs = dir([path,'\*.',formats{get(handles.ImageFormat, 'Value')}]);
if isempty(dirs)
set(handles.ReadResult, 'String', 'There is no image under this path!');
return;
end
set(handles.ReadResult, 'String', [num2str(size(dirs, 1)), ' images read!']);
handles.readSuccess = 1;
handles.imagePath = [path, '\'];
handles.images = dirs;
handles.imageIndex = 1;
handles.slotList = SlotList(handles.SlotTable, handles.TableInfo);
loadImageAndMarks(hObject, handles);
% --- Executes on button press in SaveMark.
function SaveMark_Callback(hObject, ~, handles)
% hObject handle to SaveMark (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~handles.readSuccess
set(handles.LoadResult, 'String', 'Select folder first!');
return;
end
name = [handles.imagePath, handles.images(handles.imageIndex).name];
name(end - 2 : end) = 'mat';
set(handles.LoadResult, 'String', 'Save Failed!');
data.marks = handles.pointList.ToVector();
data.slots = handles.slotList.slots;
save(name, '-struct', 'data', 'marks', 'slots');
set(handles.LoadResult, 'String', 'Save Success!');
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
guidata(hObject, handles);
% --- Executes on button press in LoadLastImage.
function LoadLastImage_Callback(hObject, ~, handles)
% hObject handle to LoadLastImage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~handles.readSuccess
set(handles.LoadResult, 'String', 'Select folder first!');
return;
end
if handles.imageIndex == 1
set(handles.LoadResult, 'String', 'It is the first image!');
return;
end
handles.imageIndex = handles.imageIndex - 1;
loadImageAndMarks(hObject, handles);
% --- Executes on button press in LoadNextImage.
function LoadNextImage_Callback(hObject, ~, handles)
% hObject handle to LoadNextImage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~handles.readSuccess
set(handles.LoadResult, 'String', 'Select folder first!');
return;
end
if handles.imageIndex == size(handles.images, 1)
set(handles.LoadResult, 'String', 'It is the last image!');
return;
end
handles.imageIndex = handles.imageIndex + 1;
loadImageAndMarks(hObject, handles);
% --- Executes on button press in ChooseFolder.
function ChooseFolder_Callback(~, ~, handles)
% hObject handle to ChooseFolder (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dname = uigetdir(get(handles.FolderPath, 'String'));
if dname ~= 0
set(handles.FolderPath, 'String', dname);
end
% --- Executes on button press in TurnToPage.
function TurnToPage_Callback(hObject, ~, handles)
% hObject handle to TurnToPage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~handles.readSuccess
set(handles.LoadResult, 'String', 'Select folder first!');
return;
end
page = str2double(get(handles.PageToTurn, 'String'));
page = page(1);
if page == fix(page) && page > 0 && page <= size(handles.images, 1)
handles.imageIndex = page;
loadImageAndMarks(hObject, handles);
else
set(handles.LoadResult, 'String', 'Invaid Index!');
end
% --- Executes when entered data in editable cell(s) in SlotTable.
function SlotTable_CellEditCallback(hObject, ~, handles)
% hObject handle to SlotTable (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
handles.slotList.UpdateSlotsFromTable();
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
guidata(hObject, handles);
% --- Executes on button press in DeleteSample.
function DeleteSample_Callback(hObject, ~, handles)
% hObject handle to DeleteSample (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~handles.readSuccess
set(handles.LoadResult, 'String', 'Select folder first!');
return;
end
name = [handles.imagePath, handles.images(handles.imageIndex).name];
delete(name);
if exist([name(1:end-3), 'mat'], 'file')
delete([name(1:end-3), 'mat']);
end
handles.images(handles.imageIndex) = [];
if isempty(handles.images)
set(handles.ReadResult, 'String', '');
handles.readSuccess = 0;
set(handles.LoadResult, 'String', 'Select folder first!');
guidata(hObject, handles);
return
end
if handles.imageIndex > size(handles.images, 1)
handles.imageIndex = handles.imageIndex - 1;
end
set(handles.ReadResult, 'String', [num2str(size(handles.images, 1)), ' images read!']);
loadImageAndMarks(hObject, handles);
function loadImageAndMarks(hObject, handles)
name = [handles.imagePath, handles.images(handles.imageIndex).name];
if ~exist(name, 'file')
set(handles.LoadResult, 'String', 'The image does not exist');
else
hold off;
image = imread(name);
imshow(image);
[imageHeight, imageWidth, ~] = size(image);
if imageHeight ~= imageWidth
set(handles.LoadResult, 'String', 'Incorrect image ratio');
return
else
handles.imageSize = imageHeight;
end
hold on;
set(handles.ImageFileName, 'String', ...
['No.', num2str(handles.imageIndex),': ', handles.images(handles.imageIndex).name]);
handles.pointList = [];
name(end - 2 : end) = 'mat';
if exist(name, 'file')
data = load(name);
if ~isfield(data,'slots')
data.slots = [];
end
if isfield(data,'marks')
handles.pointList = PointList(data.marks, handles.SelectedMark);
else
handles.pointList = PointList([], handles.SelectedMark);
end
handles.slotList.Initialize(data.slots, handles.pointList.points, handles.imageSize);
else
handles.pointList = PointList([], handles.SelectedMark);
handles.slotList.Initialize([], handles.pointList.points, handles.imageSize);
end
handles.manification = 1;
% For moving point with mouse
handles.movingPoint = false;
handles.pointMovingOffset = [0, 0];
% For moving axes with mouse
handles.movingAxes = false;
handles.axesMovingStart = [0, 0];
handles.originalXLimits = [0, 0];
handles.originalYLimits = [0, 0];
set(handles.LoadResult, 'String', 'Load Success!');
guidata(hObject, handles);
end
function scrollWhellFunction(hObject, eventdata)
handles = guidata(hObject);
if ~handles.readSuccess || handles.movingAxes
return;
end
cursorPoint = get(handles.AxesImage, 'CurrentPoint');
curX = cursorPoint(1,1);
curY = cursorPoint(1,2);
xLimits = get(handles.AxesImage, 'xlim');
yLimits = get(handles.AxesImage, 'ylim');
axesSizes = [handles.imageSize, 0.4*handles.imageSize, 0.1*handles.imageSize];
if ~(curX > min(xLimits) && curX < max(xLimits) && curY > min(yLimits) && curY < max(yLimits))
return
end
if eventdata.VerticalScrollCount > 0
if handles.manification <= 1
handles.manification = 1;
return;
end
if handles.manification == 2
set(handles.ReferenceAxes, 'xlim', [0.5,handles.imageSize+0.5], ...
'ylim', [0.5,handles.imageSize+0.5]);
set(handles.AxesImage, 'xlim', [0.5,handles.imageSize+0.5], ...
'ylim', [0.5,handles.imageSize+0.5]);
elseif handles.manification == 3
m = handles.manification;
x1 = curX - axesSizes(m-1) / axesSizes(m) * (curX-xLimits(1));
y1 = curY - axesSizes(m-1) / axesSizes(m) * (curY-yLimits(1));
x2 = curX + axesSizes(m-1) / axesSizes(m) * (xLimits(2)-curX);
y2 = curY + axesSizes(m-1) / axesSizes(m) * (yLimits(2)-curY);
set(handles.ReferenceAxes, 'xlim', [x1, x2], 'ylim', [y1, y2]);
set(handles.AxesImage, 'xlim', [x1, x2], 'ylim', [y1, y2]);
end
handles.manification = handles.manification - 1;
guidata(hObject, handles);
elseif eventdata.VerticalScrollCount < 0
if handles.manification >= 3
handles.manification = 3;
return
end
m = handles.manification;
x1 = curX - axesSizes(m+1) / axesSizes(m) * (curX-xLimits(1));
y1 = curY - axesSizes(m+1) / axesSizes(m) * (curY-yLimits(1));
x2 = curX + axesSizes(m+1) / axesSizes(m) * (xLimits(2)-curX);
y2 = curY + axesSizes(m+1) / axesSizes(m) * (yLimits(2)-curY);
set(handles.ReferenceAxes, 'xlim', [x1, x2], 'ylim', [y1, y2]);
set(handles.AxesImage, 'xlim', [x1, x2], 'ylim', [y1, y2]);
handles.manification = handles.manification + 1;
handles.pointList.DeselectIfNotInRange(x1, y1, x2, y2);
guidata(hObject, handles);
end
function mouseClicked( hObject, ~ )
handles = guidata(hObject);
if ~handles.readSuccess
return;
end
cursorPoint = get(handles.AxesImage, 'CurrentPoint');
curX = cursorPoint(1,1);
curY = cursorPoint(1,2);
xLimits = get(handles.AxesImage, 'xlim');
yLimits = get(handles.AxesImage, 'ylim');
if curX > min(xLimits) && curX < max(xLimits) && curY > min(yLimits) && curY < max(yLimits)...
&& curX > 0.5 && curX < handles.imageSize + 0.5 && curY > 0.5 && curY < handles.imageSize + 0.5
if strcmp(get(gcf,'selectionType'), 'normal')
if handles.manification == 2 || handles.manification == 3
if handles.pointList.FindPointInRange(curX, curY)
[px, py] = handles.pointList.GetSelectedPostion();
handles.pointMovingOffset = [px, py] - [curX, curY];
handles.movingPoint = true;
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
else
handles.axesMovingStart = get(handles.ReferenceAxes, 'CurrentPoint');
handles.originalXLimits = xLimits;
handles.originalYLimits = yLimits;
handles.movingAxes = true;
end
else
if ~handles.pointList.FindPointInRange(curX, curY)
handles.pointList.AddPoint(curX, curY);
end
[px, py] = handles.pointList.GetSelectedPostion();
handles.pointMovingOffset = [px, py] - [curX, curY];
handles.movingPoint = true;
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
end
elseif strcmp(get(gcf,'selectionType'), 'alt')
if handles.pointList.FindPointInRange(curX, curY)
handles.pointList.DeletePoint();
if handles.movingPoint
handles.movingPoint = false;
end
end
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
end
guidata(hObject, handles);
end
function mouseReleased(hObject, ~)
handles = guidata(hObject);
if ~handles.readSuccess
return;
end
% disp(get(gcf,'selectionType'));
if handles.movingPoint
if strcmp(get(gcf,'selectionType'), 'normal')
handles.movingPoint = false;
end
elseif handles.movingAxes
if strcmp(get(gcf,'selectionType'), 'normal') || strcmp(get(gcf,'selectionType'), 'alt')
handles.movingAxes = false;
end
end
guidata(hObject, handles);
function updateCursor(hObject, ~)
handles = guidata(hObject);
if ~handles.readSuccess
return;
end
cursorPoint = get(handles.AxesImage, 'CurrentPoint');
curX = cursorPoint(1,1);
curY = cursorPoint(1,2);
set(handles.CurrentPoint, 'String', ['(',num2str(curX,'%.2f'),',',num2str(curY,'%.2f'),')']);
xLimits = get(handles.AxesImage, 'xlim');
yLimits = get(handles.AxesImage, 'ylim');
if curX > min(xLimits) && curX < max(xLimits) && curY > min(yLimits) && curY < max(yLimits)...
&& curX > 0.5 && curX < handles.imageSize + 0.5 && curY > 0.5 && curY < handles.imageSize + 0.5
if handles.movingPoint
handles.pointList.SetPointPosition(curX + handles.pointMovingOffset(1), curY + handles.pointMovingOffset(2));
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
elseif handles.movingAxes
movingVector = handles.axesMovingStart - get(handles.ReferenceAxes, 'CurrentPoint');
set(handles.AxesImage, 'xlim', handles.originalXLimits + movingVector(1, 1), 'ylim', handles.originalYLimits + movingVector(1, 2));
end
guidata(hObject, handles);
end
function hotkeyPressed( hObject, eventdata )
%HOTKEYPRESSED Respond the keyboard press event
% Detailed explanation goes here
handles = guidata(hObject);
if ~handles.readSuccess
return;
end
if handles.manification == 1
step = 1;
elseif handles.manification == 2
step = 0.3;
elseif handles.manification == 3
step = 0.1;
end
if strcmp(get(gcf, 'CurrentCharacter'),'w') && ~handles.movingPoint
handles.pointList.ShiftPointPosition(0, -step);
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
guidata(hObject, handles);
elseif strcmp(get(gcf, 'CurrentCharacter'),'a') && ~handles.movingPoint
handles.pointList.ShiftPointPosition(-step, 0);
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
guidata(hObject, handles);
elseif strcmp(get(gcf, 'CurrentCharacter'),'s') && ~handles.movingPoint
handles.pointList.ShiftPointPosition(0, step);
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
guidata(hObject, handles);
elseif strcmp(get(gcf, 'CurrentCharacter'),'d') && ~handles.movingPoint
handles.pointList.ShiftPointPosition(step, 0);
handles.slotList.Replot(handles.pointList.points, handles.imageSize);
guidata(hObject, handles);
elseif strcmp(get(gcf, 'CurrentCharacter'),'f')
main('SaveMark_Callback', handles.SaveMark, eventdata, handles);
elseif strcmp(get(gcf, 'CurrentCharacter'),'q')
main('SaveMark_Callback', handles.SaveMark, eventdata, handles);
main('LoadLastImage_Callback', hObject, eventdata, handles);
elseif strcmp(get(gcf, 'CurrentCharacter'),'e')
main('SaveMark_Callback', handles.SaveMark, eventdata, handles);
main('LoadNextImage_Callback', hObject, eventdata, handles);
elseif double(get(gcf, 'CurrentCharacter'))==29
main('LoadNextImage_Callback', hObject, eventdata, handles);
elseif double(get(gcf, 'CurrentCharacter'))==28
main('LoadLastImage_Callback', hObject, eventdata, handles);
end