-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlend.asv
325 lines (293 loc) · 11.4 KB
/
Blend.asv
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
function varargout = Blend(varargin)
% BLEND MATLAB code for Blend.fig
% BLEND, by itself, creates a new BLEND or raises the existing
% singleton*.
%
% H = BLEND returns the handle to a new BLEND or the handle to
% the existing singleton*.
%
% BLEND('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BLEND.M with the given input arguments.
%
% BLEND('Property','Value',...) creates a new BLEND or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Blend_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Blend_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 Blend
% Last Modified by GUIDE v2.5 13-Nov-2019 11:25:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Blend_OpeningFcn, ...
'gui_OutputFcn', @Blend_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 Blend is made visible.
function Blend_OpeningFcn(hObject, eventdata, 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 Blend (see VARARGIN)
% Choose default command line output for Blend
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% This sets up the initial plot - only do when we are invisible
% so window can get raised using Blend.
if strcmp(get(hObject,'Visible'),'off')
% plot(rand(5));
end
% UIWAIT makes Blend wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Blend_OutputFcn(hObject, eventdata, 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;
% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject handle to OpenMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
open(file);
end
% --- Executes on button press in Img1.
% --- read images selected by the user
function Img1_Callback(hObject, eventdata, handles)
% hObject handle to Img1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[img1, path1, ~] = uigetfile('*.jpg; *.png; *.jpeg', 'Pick a source image');
if ~isequal(img1, 0)
fg = im2double(imread(horzcat(path1,img1)));
end
axes(handles.Source);
cla;
fg1 = fg;
hObject.UserData = fg;
imshow(fg)
% --- Executes on button press in Img2.
% --- read images selected by the user
function Img2_Callback(hObject, eventdata, handles)
% hObject handle to Img2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[img2, path2, ~] = uigetfile('*.jpg; *.png; *.jpeg', 'Pick a source image');
if ~isequal(img2, 0)
bg = im2double(imread(horzcat(path2,img2)));
end
axes(handles.Target);
cla;
imshow(bg)
hObject.UserData = bg;
% --- Executes during object creation, after setting all properties.
function Img2_CreateFcn(hObject, eventdata, handles)
% hObject handle to Img2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in Rect.
% --- select rectangular ROI
function Rect_Callback(hObject, eventdata, handles)
% hObject handle to Rect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
el = findobj('Tag', 'Ellipse');
fr = findobj('Tag', 'FreeHnd');
crt = findobj('Tag', 'DrawROI');
axes(handles.Source);
try
roi = crt.UserData{3};
delete(roi);
catch
end
el.Value=0;
fr.Value=0;
% --- Executes on button press in Ellipse.
% --- select elliptical ROI
function Ellipse_Callback(hObject, eventdata, handles)
% hObject handle to Ellipse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
re = findobj('Tag', 'Rect');
fr = findobj('Tag', 'FreeHnd');
crt = findobj('Tag', 'DrawROI');
axes(handles.Source);
try
roi = crt.UserData{3};
delete(roi);
catch
end
re.Value=0;
fr.Value=0;
% --- Executes on button press in FreeHnd.
% --- select freehand ROI
function FreeHnd_Callback(hObject, eventdata, handles)
% hObject handle to FreeHnd (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
re = findobj('Tag', 'Rect');
el = findobj('Tag', 'Ellipse');
crt = findobj('Tag', 'DrawROI');
axes(handles.Source);
try
roi = crt.UserData{3};
delete(roi);
catch
end
re.Value=0;
el.Value=0;
% --- Executes on button press in Blnd.
% --- Blend the selected images based on selected ROI
function Blnd_Callback(hObject, eventdata, handles)
% hObject handle to Blnd (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h1 = findobj('Tag', 'Img1');
h2 = findobj('Tag', 'Img2');
nl = findobj('Tag', 'nlayers');
msk = findobj('Tag', 'DrawROI');
trg = findobj('Tag', 'SetTrgtLocn');
bg = h2.UserData;
try
fg1 = trg.UserData{1};
bw_mask = trg.UserData{2};
catch
fg1 = msk.UserData{1};
bw_mask = msk.UserData{2};
end
%handle RGB images.
if size(bg,3)>1
blendedImg(:,:,1) = blendPyramid(fg1(:,:,1),bg(:,:,1),bw_mask,str2double(nl.String));
blendedImg(:,:,2) = blendPyramid(fg1(:,:,2),bg(:,:,2),bw_mask,str2double(nl.String));
blendedImg(:,:,3) = blendPyramid(fg1(:,:,3),bg(:,:,3),bw_mask,str2double(nl.String));
else
blendedImg(:,:,1) = blendPyramid(fg1(:,:,1),bg(:,:,1),bw_mask,str2double(nl.String));
end
% blendedImg = ScaleRGBValues(blendedImg);
axes(handles.Source);
imshow(h1.UserData);
axes(handles.Target);
imshow(bg);
axes(handles.Outputs);
imshow(blendedImg);
imwrite(blendedImg, 'blend.png');
% --- Executes on button press in SetTrgtLocn.
function SetTrgtLocn_Callback(hObject, eventdata, handles)
% hObject handle to SetTrgtLocn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
droi = findobj('Tag', 'DrawROI');
fgp = droi.UserData{1};
bwm = droi.UserData{2};
src = findobj('Tag', 'SetSrcLocn');
axes(handles.Target);
trgtLoc = drawrectangle();
trgtPos = trgtLoc.Position;
try
srcPos = src.UserData;
transVec = trgtPos(1:2)-srcPos(1:2);
catch
transVec = [0,0];
end
%translate image to location selected by the user.
fgp=imtranslate(fgp, transVec);
bwm = imtranslate(bwm, transVec);
axes(handles.Source);
imshow(fgp);
axes(handles.Outputs);
imshow(bwm);
hObject.UserData{1} = fgp;
hObject.UserData{2} = bwm;
imwrite(bwm, 'mask.png');
imwrite(fgp, 'fgPadded.png');
delete(trgtLoc);
% --- Executes on button press in SetSrcLocn.
function SetSrcLocn_Callback(hObject, eventdata, handles)
% hObject handle to SetSrcLocn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.Source);
srcLoc = drawrectangle();
srcPos = srcLoc.Position;
hObject.UserData = srcPos;
delete(srcLoc);
function nlayers_Callback(hObject, eventdata, handles)
% hObject handle to nlayers (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of nlayers as text
% str2double(get(hObject,'String')) returns contents of nlayers as a double
% --- Executes during object creation, after setting all properties.
function nlayers_CreateFcn(hObject, eventdata, handles)
% hObject handle to nlayers (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit 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 on button press in DrawROI.
% --- draw ROI based on selected region type
function DrawROI_Callback(hObject, eventdata, handles)
% hObject handle to DrawROI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h1 = findobj('Tag', 'Img1');
h2 = findobj('Tag', 'Img2');
re = findobj('Tag', 'Rect');
el = findobj('Tag', 'Ellipse');
fr = findobj('Tag', 'FreeHnd');
src = findobj('Tag', 'SetSrcLocn');
trg = findobj('Tag', 'SetTrgtLocn');
fg1 = trg.UserData;
if isempty(fg1)
fg1 = h1.UserData;
end
bg = h2.UserData;
axes(handles.Source);
if re.Value == 1 && el.Value == 0 && fr.Value == 0
[fg1, roi, bw_mask] = createROIMask(fg1, bg, 1);
elseif re.Value == 0 && el.Value == 1 && fr.Value == 0
[fg1, roi, bw_mask] = createROIMask(fg1, bg, 2);
else
[fg1, roi, bw_mask] = createROIMask(fg1, bg, 3);
end
axes(handles.Outputs);
imshow(bw_mask);
axes(handles.Source);
imshow(fg1);
imwrite(bw_mask, 'mask.png');
imwrite(fg1, 'fgPadded.png');
hObject.UserData{1} = fg1;
hObject.UserData{2} = bw_mask;
hObject.UserData{3} = roi;
% --- Executes on button press in Reset.
function Reset_Callback(hObject, eventdata, handles)
% hObject handle to Reset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close(Blend);
run('Blend');