-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcorrection_gui.m
353 lines (315 loc) · 13.3 KB
/
correction_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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
function varargout = correction_gui(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @correction_gui_OpeningFcn, ...
'gui_OutputFcn', @correction_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 correction_gui is made visible.
function correction_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for correction_gui
handles.output = hObject;
clc;
warning('off','all');
handles.basedir = pwd;
% Update handles structure
guidata(hObject, handles);
global LABcolorTransform;
global RGBcolorTransform;
LABcolorTransform = makecform('srgb2lab');
RGBcolorTransform = makecform('lab2srgb');
input_im=imread(fullfile(handles.basedir,'input_images','sample_4.jpg'));
handles.input_im = input_im;
axes(handles.input);
imshow(input_im);
ref_im=imread(fullfile(handles.basedir,'reference','2.jpg'));
handles.ref_im = ref_im;
axes(handles.reference);
imshow(ref_im);
load(fullfile(handles.basedir,'picked_colors','sample_4_picked_colors.mat'))
load(fullfile(handles.basedir,'params','sample_4_params.mat'))
handles.picked_colors = struct('picked_rbg', picked_rbg,'output_rbg',output_rbg);
stt1 = sprintf('%1.3f',lambds(1));
stt2 = sprintf('%1.3f',lambds(2));
stt3 = sprintf('%1.3f',lambds(3));
set(handles.lambda1, 'string',stt1);
set(handles.lambda2, 'string',stt2);
set(handles.lambda3, 'string',stt3);
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = correction_gui_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in load_input_im.
function load_input_im_Callback(hObject, eventdata, handles)
global input_im_path
[im_path,full_path] = uigetfile('*.jpg;*.png;','Image file (*.jpg,*.png)',fullfile(handles.basedir,'input_images'));
if (im_path==0)
return
end
full_im_path=fullfile(full_path,im_path);
input_im_path = full_path;
input_im=imread(full_im_path);
axes(handles.input);
imshow(input_im);
handles.input_im = input_im;
guidata(hObject, handles);
% --- Executes on button press in load_ref_im.
function load_ref_im_Callback(hObject, eventdata, handles)
[im_path,full_path] = uigetfile('*.jpg;*.png;','Image file (*.jpg,*.png)',fullfile(handles.basedir,'reference'));
if (im_path==0)
return
end
full_im_path=fullfile(full_path,im_path);
ref_im=imread(full_im_path);
axes(handles.reference);
imshow(ref_im);
handles.ref_im = ref_im;
guidata(hObject, handles);
function enlarge_input_Callback(hObject, eventdata, handles)
figure();
imshow(handles.input_im);
function enlarge_ref_Callback(hObject, eventdata, handles)
figure();
imshow(handles.ref_im);
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
global LABcolorTransform;
ref_im = handles.ref_im;
input_im = handles.input_im;
picked_rbg = handles.picked_colors.picked_rbg;
output_rbg = handles.picked_colors.output_rbg;
global result_image;
global AB_Transformation
ref_im_lab = applycform(im2double(ref_im), LABcolorTransform);
input_im_lab = applycform(im2double(input_im), LABcolorTransform);
lambda1=get(handles.lambda1, 'string');
l1=str2double(lambda1);
if (l1==0)
msgbox(sprintf('Variable lambda_1 must be different than 0'),'Error','Error')
return
end
lambda2=get(handles.lambda2, 'string');
l2=str2double(lambda2);
lambda3=get(handles.lambda3, 'string');
l3=str2double(lambda3);
w2=[1 0 0;0 1 0;0 0 1];
[A,B]=estimate_Ab_matrix_trust_region_method(picked_rbg,output_rbg,input_im_lab,ref_im_lab,l1,l2,l3,w2);
AB=[A B];
AB_Transformation = AB;
result_image=transform_by_color_matrix(AB,input_im);
figure;
imshow(result_image);
set(handles.save_result,'Enable','on');
set(handles.Apply_All,'Enable','on');
function lambda1_Callback(hObject, eventdata, handles)
function lambda2_Callback(hObject, eventdata, handles)
function lambda3_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function lambda1_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 lambda2_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 lambda3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in demo1.
function demo1_Callback(hObject, eventdata, handles)
input_im=imread(fullfile(handles.basedir,'input_images','sample_1.jpg'));
axes(handles.input);
imshow(input_im);
ref_im=imread(fullfile(handles.basedir,'reference','3.jpg'));
axes(handles.reference);
imshow(ref_im);
load(fullfile(handles.basedir,'picked_colors','sample_1_picked_colors.mat'))
load(fullfile(handles.basedir,'params','sample_1_params.mat'))
stt1 = sprintf('%1.3f',lambds(1));
stt2 = sprintf('%1.3f',lambds(2));
stt3 = sprintf('%1.3f',lambds(3));
set(handles.lambda1, 'string',stt1);
set(handles.lambda2, 'string',stt2);
set(handles.lambda3, 'string',stt3);
handles.picked_colors = struct('picked_rbg', picked_rbg,'output_rbg',output_rbg);
handles.ref_im = ref_im;
handles.input_im = input_im;
guidata(hObject, handles);
% --- Executes on button press in demo2.
function demo2_Callback(hObject, eventdata, handles)
input_im=imread(fullfile(handles.basedir,'input_images','sample_2.jpg'));
axes(handles.input);
imshow(input_im);
ref_im=imread(fullfile(handles.basedir,'reference','6.jpg'));
axes(handles.reference);
imshow(ref_im);
load(fullfile(handles.basedir,'picked_colors','sample_2_picked_colors.mat'))
load(fullfile(handles.basedir,'params','sample_2_params.mat'))
stt1 = sprintf('%1.3f',lambds(1));
stt2 = sprintf('%1.3f',lambds(2));
stt3 = sprintf('%1.3f',lambds(3));
set(handles.lambda1, 'string',stt1);
set(handles.lambda2, 'string',stt2);
set(handles.lambda3, 'string',stt3);
handles.picked_colors = struct('picked_rbg', picked_rbg,'output_rbg',output_rbg);
handles.ref_im = ref_im;
handles.input_im = input_im;
guidata(hObject, handles);
% --- Executes on button press in demo3.
function demo3_Callback(hObject, eventdata, handles)
input_im=imread(fullfile(handles.basedir,'input_images','sample_3.jpg'));
axes(handles.input);
imshow(input_im);
ref_im=imread(fullfile(handles.basedir,'reference','2.jpg'));
axes(handles.reference);
imshow(ref_im);
load(fullfile(handles.basedir,'picked_colors','sample_3_picked_colors.mat'))
load(fullfile(handles.basedir,'params','sample_3_params.mat'))
stt1 = sprintf('%1.3f',lambds(1));
stt2 = sprintf('%1.3f',lambds(2));
stt3 = sprintf('%1.3f',lambds(3));
set(handles.lambda1, 'string',stt1);
set(handles.lambda2, 'string',stt2);
set(handles.lambda3, 'string',stt3);
handles.picked_colors = struct('picked_rbg', picked_rbg,'output_rbg',output_rbg);
handles.ref_im = ref_im;
handles.input_im = input_im;
guidata(hObject, handles);
function load_calib1_Callback(hObject, eventdata, handles)
global calib_im_underwater
[im_path,full_path] = uigetfile('*.jpg;*.png;','Image file (*.jpg,*.png)',fullfile(handles.basedir,'color_board_underwater\'));
if (im_path==0)
return
end
full_im_path=fullfile(full_path,im_path);
calib_im_underwater=imread(full_im_path);
function load_calib2_Callback(hObject, eventdata, handles)
global calib_im_abovewater
[im_path,full_path] = uigetfile('*.jpg;*.png;','Image file (*.jpg,*.png)',fullfile(handles.basedir,'color_board_above_water\'));
if (im_path==0)
return
end
full_im_path=fullfile(full_path,im_path);
calib_im_abovewater=imread(full_im_path);
function points_picking_Callback(hObject, eventdata, handles)
global calib_im_underwater;
global calib_im_abovewater;
if (isempty(calib_im_underwater) || isempty(calib_im_abovewater))
warndlg('Calib pictures not loaded');
return
end
n_samples=get(handles.num_samples, 'string');
n_samples=str2num(n_samples);
if (n_samples==0)
set(handles.num_samples,'string','24');
warndlg('Number of picked colors must be different than 0')
return
end
[output_rbg,picked_rbg] = colors_picking(calib_im_abovewater,calib_im_underwater,n_samples,'LAB');
handles.picked_colors = struct('picked_rbg', picked_rbg,'output_rbg',output_rbg);
guidata(hObject, handles);
function load_picked_points_Callback(hObject, eventdata, handles)
[mat_path,full_path] = uigetfile('*.mat;','Mat file (*.mat)',fullfile(handles.basedir,'picked_colors','sample_1_picked_colors.mat'));
if (mat_path==0)
return
end
full_mat_path=fullfile(full_path,mat_path);
load(full_mat_path);
handles.picked_colors = struct('picked_rbg', picked_rbg,'output_rbg',output_rbg);
guidata(hObject, handles);
function save_picked_points_Callback(hObject, eventdata, handles)
picked_rbg = handles.picked_colors.picked_rbg;
output_rbg = handles.picked_colors.output_rbg;
[mat_path,full_path] = uiputfile('*.mat;','Mat file (*.mat)',fullfile(handles.basedir,'picked_colors','new_picked_colors.mat'));
if (mat_path==0)
return
end
full_mat_path=fullfile(full_path,mat_path);
save(full_mat_path,'output_rbg','picked_rbg');
function num_samples_Callback(hObject, eventdata, handles)
function num_samples_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function save_result_Callback(hObject, eventdata, handles)
global result_image;
[im_path,full_path] = uiputfile('*.png;','Image file (*.png)',fullfile(handles.basedir,'results','new_image.png'));
if (im_path==0)
return
end
full_im_path=fullfile(full_path,im_path);
imwrite(result_image,full_im_path);
% --- Executes on button press in Apply_All.
function Apply_All_Callback(hObject, eventdata, handles)
global AB_Transformation;
file_list=uipickfiles('Type',{ '*.jpg', 'JPG-files';'*.png', 'PNG-files' });
if (iscell(file_list))
if (isempty(file_list))
return
end
elseif (file_list==0)
return
end
output_folder = uigetdir('','Select save directory');
if (output_folder==0)
return
end
h= waitbar(0,'Processing...');
for i=1:length(file_list)
[~,name,ext] = fileparts(file_list{i});
image_name = [name ext];
dummy_image = imread(file_list{i});
dummy_image_transformed = transform_by_color_matrix(AB_Transformation,dummy_image);
imwrite(dummy_image_transformed,fullfile(output_folder,image_name))
waitbar((i-2)/length(file_list),h,'Processing...')
end
delete(h);
msgbox(sprintf('Multiple files processing finished!'),'Finished','Finished')
function visualize_color_mapping_Callback(hObject, eventdata, handles)
picked_rbg = handles.picked_colors.picked_rbg;
output_rbg = handles.picked_colors.output_rbg;
output_points_rgb = lab2rgb(output_rbg(1:3,1:end)');
picked_points_rgb = lab2rgb(picked_rbg(1:3,1:end)');
output_colors_array = cat(3,...
output_points_rgb(1:end,1)',...
output_points_rgb(1:end,2)',...
output_points_rgb(1:end,3)');
picked_colors_array = cat(3,...
picked_points_rgb(1:end,1)',...
picked_points_rgb(1:end,2)',...
picked_points_rgb(1:end,3)');
figure;
subplot(2,1,1)
imshow(picked_colors_array,'InitialMagnification','fit')
title('Colors picked from underwater image')
subplot(2,1,2)
imshow(output_colors_array,'InitialMagnification','fit')
title('Colors picked from abovewater image')
% --- Executes on button press in back_button.
function back_button_Callback(hObject, eventdata, handles)
main();
close(handles.correction_gui);
function correction_presets_Callback(hObject, eventdata, handles)
handles.picked_colors = correction_preview(handles);
guidata(hObject, handles);
function correction_presets_by_ref_Callback(hObject, eventdata, handles)
handles.ref_im = correction_preview_ref(handles);
axes(handles.reference);
imshow(handles.ref_im);
guidata(hObject, handles);