-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwindowPickingHelper.m
More file actions
370 lines (289 loc) · 10.2 KB
/
windowPickingHelper.m
File metadata and controls
370 lines (289 loc) · 10.2 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
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
function [ roi, wins ] = windowPickingHelper( data4d , im2d, p_roi, p_wins, interactive)
%windowPickingHelper is a GUI to pick a real-space ROI and EWPC-space spot
% windows for 4D STEM EWPC lattice mapping.
% Inputs:
% data4d -- 4D STEM dataset to be inspected for EWPC lattice mapping.
% Dimensions ordered [k1,k2,x1,x2].
% im2d -- A 2D real-space image, e.g. ADF, to assist in navigating the
% data.
% p_roi -- (optional) preset roi value, enter to update or view
% selections rather than create from scratch.
% p_wins -- (optional) preset wins list, enter to update or view
% selections rather than create from scratch.
% interactive -- (optional) choose whether to run program
% interactively (default). If false, the program will plot
% the given presets and not wait for user interaction.
% Outputs:
% roi -- boundaries of real-space region of interest, in order
% [x1min,x1max,x2min,x2max].
% wins -- boundaries of EWPC-space windows selecting peaks to map, in
% NX4 array for N windows, with each row as
% [x1min,x1max,x2min,x2max].
%
%This function is part of the PC-STEM Package by Elliot Padgett in the
%Muller Group at Cornell University. Last updated July 18, 2019.
%Set default inputs
if nargin>2
preset = 1;
p_roi = [p_roi(3:4), p_roi(1:2)] ; %follow index convention
else
preset = 0;
end
if nargin < 5
interactive = 1;
end
%Set up figure
drawnow
f=figure();
%Set up guidata structure for shared data
gdata = struct('d4d',data4d);
gdata.EwpcWindows = [];
gdata.Im2D = im2d;
gdata.Transf = @(x) x.^1;
guidata(f,gdata)
%% Set up full image and ROI selection box
%show adf image
gdata.ImageAx = subplot(2,3,1);
plotIM(im2d)
[N_k1,N_k2,N_x1,N_x2] = size(data4d);
axis([1,N_x2,1,N_x1])
title({'2D image:','Select ROI'})
guidata(gcf,gdata)
%make ADF ROI selection box
pause(0.01) % clears some display timing bug
if preset %use provided preset value for roi box position
pos = [p_roi(1),p_roi(3),p_roi(2)-p_roi(1),p_roi(4)-p_roi(3)];
gdata.ImageRoiBox = imrect(gdata.ImageAx, pos, ...
'PositionConstraintFcn',...
makeConstrainToRectFcn('imrect', get(gca,'XLim'), get(gca,'YLim')));
else %Ask user to place box position
gdata.ImageRoiBox = imrect(gdata.ImageAx,...
'PositionConstraintFcn',...
makeConstrainToRectFcn('imrect', get(gca,'XLim'), get(gca,'YLim')));
end
addNewPositionCallback(gdata.ImageRoiBox,@AdfRoiCallback);
gdata.ImageRoiBox.Deletable = false;
%Display cropped area in separate subfigure
gdata.CroppedImageAx = subplot(2,3,4);
gdata.CroppedImage = plotIM(im2d);
title({'ROI: Select','View Point'})
guidata(gcf,gdata)
pos = round(gdata.ImageRoiBox.getPosition);
pos(pos<1)=1;
setPosition(gdata.ImageRoiBox,pos)
gdata.adfRoi = [pos(1),pos(1)+pos(3),pos(2),pos(2)+pos(4)];
axis(gdata.adfRoi)
cmin = min(min(im2d(gdata.adfRoi(3):gdata.adfRoi(4),gdata.adfRoi(1):gdata.adfRoi(2))));
cmax = max(max(im2d(gdata.adfRoi(3):gdata.adfRoi(4),gdata.adfRoi(1):gdata.adfRoi(2))));
set(gca,'Clim',[cmin,cmax])
guidata(gcf,gdata)
%% Set up ROI-zoomed image and diffraction selection point
%wait until user clicks on ROI to show
gdata.CroppedImage.ButtonDownFcn = @RoiWindowButtonDownFcn;
if preset
CreateDiffPoint( [pos(1)+pos(3)/2,pos(2)+pos(4)/2] )
else
uiwait
end
gdata = guidata(gcf);
pause(0.01)
%% Set up EWPC selection boxes
%Declare function to create new boxes with click
gdata.EwpcIm.ButtonDownFcn = @EWPCWindowButtonDownFcn;
if preset
subplot(2,3,5)
for i=1:size(p_wins,1)
w = p_wins(i,:);
p = [w(3),w(1),w(4)-w(3),w(2)-w(1)];
h = imrect(gca,p);
setPositionConstraintFcn(h,makeConstrainToRectFcn('imrect', get(gca,'XLim'), get(gca,'YLim')))
addNewPositionCallback(h,@UpdateEWPCWins);
addlistener( h , 'ObjectBeingDestroyed', @EWPCWinDeleted );
gdata.EwpcWindows = [gdata.EwpcWindows, h];
guidata(gcf,gdata)
end
UpdateEWPCWins( h.getPosition )
else
uiwait
end
gdata = guidata(gcf);
%% finish program when user hits space
if interactive % non-interactive skips to end
gdata = guidata(gcf);
roi = gdata.adfRoi;
fprintf('When finished, hit any key to return.\n')
waitforbuttonpress;
while isempty(f.CurrentCharacter)
waitforbuttonpress;
end
end
%User has ended function, format return values
gdata = guidata(gcf);
roi = gdata.adfRoi;
roi = [roi(3:4), roi(1:2)] ; %follow index convention
wins = zeros(length(gdata.EwpcWindows),4);
for i=1:length(gdata.EwpcWindows)
p = round(getPosition(gdata.EwpcWindows(i)));
p(p<1)=1;
wins(i,:) = [p(2),p(2)+p(4),p(1),p(1)+p(3)]; %follow index convention
end
pause(0.01)
drawnow
pause(0.01)
fprintf('Done.\n')
end
%% Helper functions and callback functions
function AdfRoiCallback( pos )
gdata = guidata(gcf);
pos = round(pos);
pos(pos<1)=1;
setPosition(gdata.ImageRoiBox,pos)
gdata.adfRoi = [pos(1),pos(1)+pos(3),pos(2),pos(2)+pos(4)];
subplot(gdata.CroppedImageAx)
axis(gdata.adfRoi)
im2d = gdata.Im2D;
cmin = min(min(im2d(gdata.adfRoi(3):gdata.adfRoi(4),gdata.adfRoi(1):gdata.adfRoi(2))));
cmax = max(max(im2d(gdata.adfRoi(3):gdata.adfRoi(4),gdata.adfRoi(1):gdata.adfRoi(2))));
set(gca,'Clim',[cmin,cmax])
guidata(gcf,gdata)
end
function RoiWindowButtonDownFcn(hObject, eventdata, handles)
gdata = guidata(gcf);
pt = get(gdata.CroppedImageAx,'CurrentPoint');
if isfield(gdata,'DiffPoint')
%move point to mouse click location
setPosition(gdata.DiffPoint,pt(1,1:2));
guidata(gcf,gdata)
else
%Create CBED/EWPC selection point
CreateDiffPoint( pt(1,1:2) )
uiresume
end
end
function DiffPointCallback( pos )
gdata = guidata(gcf);
diffPointPos = round(pos);
setPosition(gdata.DiffPoint,diffPointPos);
t = gdata.Transf;
gdata.CbedIm.CData=log(gdata.d4d(:,:,diffPointPos(2),diffPointPos(1)));
gdata.EwpcIm.CData = bsat(t(ewpc(gdata.d4d(:,:,diffPointPos(2),diffPointPos(1)))));
guidata(gcf,gdata)
UpdateEWPCWins( pos )
end
function EWPCWindowButtonDownFcn(hObject, eventdata, handles)
gdata = guidata(gcf);
uiresume
subplot(2,3,5)
h = imrect(gca);
setPositionConstraintFcn(h,makeConstrainToRectFcn('imrect', get(gca,'XLim'), get(gca,'YLim')))
addNewPositionCallback(h,@UpdateEWPCWins);
addlistener( h , 'ObjectBeingDestroyed', @EWPCWinDeleted );
gdata.EwpcWindows = [gdata.EwpcWindows, h];
guidata(gcf,gdata)
UpdateEWPCWins( h.getPosition )
end
function UpdateEWPCWins( pos )
gdata = guidata(gcf);
diffPointPos = round(getPosition(gdata.DiffPoint));
ewpcPointBoxes = gdata.EwpcWindows;
colors = get(gca,'colororder');
for i = 1:length(ewpcPointBoxes)
h = ewpcPointBoxes(i);
p = round(getPosition(h));
p(p<1)=1;
a=subplot(length(ewpcPointBoxes),3,3*i);
t = gdata.Transf;
PCim = bsat(t(ewpc(gdata.d4d(:,:,diffPointPos(2),diffPointPos(1)))));
plotIM(PCim);
ewpcRoi = [p(1),p(1)+p(3),p(2),p(2)+p(4)];
axis(ewpcRoi)
% imax = max(max(PCim(ewpcRoi(1):ewpcRoi(2),ewpcRoi(3):ewpcRoi(4))));
% imin = min(min(PCim(ewpcRoi(1):ewpcRoi(2),ewpcRoi(3):ewpcRoi(4))));
imax = max(max(PCim(ewpcRoi(3):ewpcRoi(4),ewpcRoi(1):ewpcRoi(2))));
imin = min(min(PCim(ewpcRoi(3):ewpcRoi(4),ewpcRoi(1):ewpcRoi(2))));
caxis([imin,imax]);
title(sprintf('EWPC spot %d',i))
coloridx = mod(i-1,length(colors))+1;
a.XColor = colors(coloridx,:); a.YColor = colors(coloridx,:); a.LineWidth = 4;
setColor(h,colors(coloridx,:));
end
end
function EWPCWinDeleted( src,evt )
f=gcf;
if strcmp(f.BeingDeleted,'off')
gdata = guidata(gcf);
gdata.EwpcWindows = gdata.EwpcWindows(gdata.EwpcWindows~=src);
guidata(gcf,gdata)
UpdateEWPCWins( 1 )
end
end
function CreateDiffPoint( pt )
gdata = guidata(gcf);
%Create CBED/EWPC selection point
gdata.DiffPoint = impoint(gca,pt);
guidata(gcf,gdata)
addNewPositionCallback(gdata.DiffPoint,@DiffPointCallback);
setPositionConstraintFcn(gdata.DiffPoint,makeConstrainToRectFcn('impoint', get(gdata.ImageAx,'XLim'), get(gdata.ImageAx,'YLim')))
diffPointPos = round(getPosition(gdata.DiffPoint));
guidata(gcf,gdata)
setPosition(gdata.DiffPoint,diffPointPos);
%Show CBED at selected point
gdata.CbedAx = subplot(2,3,2);
gdata.CbedIm = plotIM(log(gdata.d4d(:,:,diffPointPos(2),diffPointPos(1))));
title('log(CBED)')
%Show EWPC at selected point
gdata.EwpcAx=subplot(2,3,5);
t = gdata.Transf;
gdata.EwpcIm=plotIM(bsat(t(ewpc(gdata.d4d(:,:,diffPointPos(2),diffPointPos(1))))));
title({'EWPC: Pick', 'fit windows'})
guidata(gcf,gdata)
MakeUiControls()
gdata = guidata(gcf);
guidata(gcf,gdata)
end
function [] = MakeUiControls()
gdata = guidata(gcf);
%contrast controls
bg = uibuttongroup('Title','EWPC Contrast',...
'Visible','off',...
'Position',[.35 0.01 .3 .1],...
'FontWeight','bold',...
'FontSize',12,...
'BackgroundColor','w',...
'SelectionChangedFcn',@bselection);
gdata.s1 = uicontrol(bg,'Style','slider',...
'Position',[40 0 60 20],...
'BackgroundColor','w',...
'Min',0.001,'Max',1,...
'Value',1,...
'Callback',@bselection,...
'HandleVisibility','off');
uicontrol(bg,'Style','text',...
'String','log',...
'FontSize',12,...
'HorizontalAlignment','right',...
'BackgroundColor','w',...
'Position',[0 0 30 25],...
'HandleVisibility','off');
uicontrol(bg,'Style','text',...
'String','linear',...
'FontSize',12,...
'HorizontalAlignment','left',...
'BackgroundColor','w',...
'Position',[110 0 40 25],...
'HandleVisibility','off');
bg.Visible = 'on';
guidata(gcf,gdata)
end
function bselection(source,event)
gdata = guidata(gcf);
Gamma = gdata.s1.Value;
gdata.Transf = @(x) x.^Gamma;
t = gdata.Transf;
diffPointPos = round(getPosition(gdata.DiffPoint));
gdata.EwpcIm.CData = bsat(t(ewpc(gdata.d4d(:,:,diffPointPos(2),diffPointPos(1)))));
guidata(gcf,gdata)
UpdateEWPCWins( )
gdata = guidata(gcf);
guidata(gcf,gdata)
end