-
Notifications
You must be signed in to change notification settings - Fork 0
/
show2.m
218 lines (197 loc) · 6.45 KB
/
show2.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
function show2(varargin)
% Newish way of writing GUI programs - nice handling of callbacks with
% function handles and better parsing of command line - parse_pv_pairs.m
%
% Specify the structure field name as an argument to the function, and it
% will nicely be modified:
%
% show2('currentimage',3,'prefix','myspecialprefix_')
%
%
% parameters can be used in any order
% useful parameters are: currentimage, prefix, extension, numlength (not
% sure it's needed anymore), cmap
sfInitialiseFigure;
if nargin>0
% parse_pv_pairs will not remove any part of the structure...
app=parse_pv_pairs(app,varargin);
end
sfFindNumberOfImages;
sfUpdateFigure;
%% sfInitialiseFigure
function sfInitialiseFigure
warning('off','Images:initSize:adjustingMag');
[tmp,dname,tmp,tmp]=fileparts(pwd);
app.prefix=dname;
app.extension='edf';
app.currentimage=1;
app.numlength=4;
app.clims=[];
app.flatfield='no';
app.h_fig=figure;
app.h_histogram=figure;
set(app.h_histogram,'visible','off')
app.firsttime=1;
set(app.h_fig,'keypressfcn',@sfKeyPress)
end
%% sfFindNumberOfImages
function sfFindNumberOfImages
d=dir([app.prefix '*.' app.extension]);
app.numberofimages=length(d);
[app.filenames{1:app.numberofimages}]=deal(d.name);
app.filenames=sort(app.filenames);
end
%% sfUpdateFigure
function sfUpdateFigure(varargin)
if nargin==0 % file has changed or flatfield correction toggled
if isfield(app,'im') && length(app.im)>1024
set(gcf,'name','Loading...','numbertitle','off');
drawnow
end
if strcmpi(app.extension,'edf')
app.im=edfread(app.filenames{app.currentimage})';
else
app.im=imread(app.filenames{app.currentimage});
end
if strcmpi(app.flatfield,'yes')
fname_nextref=sprintf('refHST%04d.edf',...
(floor(app.currentimage/app.acq.RefSpacing)+1)*app.acq.RefSpacing);
fname_prevref=sprintf('refHST%04d.edf',...
(floor(app.currentimage/app.acq.RefSpacing))*app.acq.RefSpacing);
fprintf('\nReading reference image (%s, %s)\n',fname_prevref,fname_nextref)
im_refNext=edfread(fname_nextref)-app.im_dark;
im_refPrev=edfread(fname_prevref)-app.im_dark;
% what ratio between the two references?
fraction=mod(app.currentimage,app.acq.RefSpacing)/app.acq.RefSpacing;
im_ref=(im_refNext.*(fraction))+(im_refPrev.*(1-fraction));
app.im=app.im./im_ref';
end
if app.firsttime
app.clims=[min(app.im(:)) max(app.im(:))];
disp('Resetting colourmap')
app.firsttime=0;
end
elseif strcmp(varargin{1},'clims') % COLORLIMITS HAVE CHANGED
end
figure(app.h_fig)
imshow(app.im,app.clims);
set(gcf,...
'name',[num2str(app.currentimage) ': ' app.filenames{app.currentimage}],...
'numbertitle','off',...
'menubar','none')
drawnow;
end
%% sfKeyPress
function sfKeyPress(varargin)
% varargin{2} contains the event details
c=varargin{2};
switch c.Key
% CURSOR KEYS
case 'uparrow'
sfUpdateFigure;
case 'downarrow'
sfUpdateFigure;
case 'rightarrow'
if strcmp(c.Modifier,'shift')
app.previousimage=app.currentimage;
app.currentimage=sfWrap(app.currentimage+10,app.numberofimages);
elseif strcmp(c.Modifier,'control')
disp('Movie')
while 1
app.previousimage=app.currentimage;
app.currentimage=sfWrap(app.currentimage+10,app.numberofimages);
sfUpdateFigure
end
else
app.previousimage=app.currentimage;
app.currentimage=sfWrap(app.currentimage+1,app.numberofimages);
end
sfUpdateFigure;
case 'leftarrow'
if strcmp(c.Modifier,'shift')
app.previousimage=app.currentimage;
app.currentimage=sfWrap(app.currentimage-10,app.numberofimages);
elseif strcmp(c.Modifier,'control')
disp('moving')
while 1
app.previousimage=app.currentimage;
app.currentimage=sfWrap(app.currentimage-10,app.numberofimages);
sfUpdateFigure
end
else
app.previousimage=app.currentimage;
app.currentimage=sfWrap(app.currentimage-1,app.numberofimages);
end
sfUpdateFigure;
% OTHER KEYS
case 'return'
if isempty(c.Modifier) % just a plain keystroke
% write image to workspace 'im' variable
disp('Variable ''im'' now contains image');
assignin('base','im',app.im);
end
case 'h'
figure(app.h_histogram)
set(app.h_histogram,'visible','on')
hist(app.im(:),100)
figure(app.h_fig)
% COLOURMAP CHANGES
case 'c'
disp('Resetting colormap')
app.clims=[min(app.im(:)) max(app.im(:))];
sfUpdateFigure
case '1'
disp('Increasing contrast')
app.clims=app.clims*0.95;
sfUpdateFigure('clims');
case '2'
disp('Decreasing contrast')
app.clims=app.clims*1.05;
sfUpdateFigure('clims');
case '3'
disp('Colormap brighter')
app.clims=app.clims-max(app.clims)*0.05;
sfUpdateFigure('clims');
case '4'
disp('Colormap darker')
app.clims=app.clims+max(app.clims)*0.05;
sfUpdateFigure('clims');
% TOGGLE FLAT CORRECTION
case 'd'
if strcmpi(app.flatfield,'yes')
disp('Disabling flatfield correction')
app.flatfield='no';
app.firsttime='yes';
sfUpdateFigure
else
disp('Enabling flatfield correction')
app.flatfield='yes';
app.firsttime='yes';
fname=[app.prefix '.xml'];
if exist(fname,'file')
disp('Using XML file')
app.acq=query_xml(fname,'acquisition');
disp('Dark file is hardcoded - fix this');
app.im_dark=edfread('dark.edf');
sfUpdateFigure
else
disp('Cannot find XML file - will need to set reference file manually');
app.flatfield='no';
sfUpdateFigure
end
end
case 'q' % QUIT
set(app.h_fig,'KeyPressFcn',[]);
close(app.h_histogram);
end
end
function n=sfWrap(m,limit)
if m>limit
n=1;
elseif m<1
n=limit;
else
n=m;
end
end
end