-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest4D.m
281 lines (228 loc) · 9.92 KB
/
test4D.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
function test4D(im4D)
% thisObj = varargin{1};
%
% if nargin > 1
% searchDim = varargin{2};
% else
% searchDim = 'scan';
% end
im_init_r = squeeze( mean( im4D, [1, 2]) );
im_init_k = squeeze( im4D(:,:,1,1) );%mean( im4D, [1, 2]) );
[nx, ny, nsx, nsy] = size( im4D );
% Find default min, max
cmin_r = min(im_init_r(:));
cmax_r = max(im_init_r(:));
crange_r = cmax_r - cmin_r;
cmin_k = min(im_init_k(:));
cmax_k = max(im_init_k(:));
crange_k= cmax_k - cmin_k;
nedge = 256;
edges_r = linspace( cmin_r, cmax_r, nedge);
edges_k = linspace( cmin_k, cmax_k, nedge);
bins_r = histcounts( im_init_r, edges_r );
bins_k = histcounts( im_init_k, edges_k );
f = figure("Name", "4D Viewer by SSH", "Position", [100 100 950 650]);
% Image Axes
ax(1) = axes( f, "Units", "normalized", "Position", [50/950 50/650 400/950 400/650], "XLimitMethod", "Tight");%, "XLim", [1,nkx], "YLim", [1,nky] );
ax(2) = axes( f, "Units", "normalized", "Position", [500/950 50/650 400/950 400/650], "XLimitMethod", "Tight");%"XLim", [1,nx], "YLim", [1,ny] );
im(1) = imagesc(ax(1), im_init_r);
im(2) = imagesc(ax(2), im_init_k);
axis( ax(1:2), 'equal','off')
colormap( ax(1), gray(65535) )
colormap( ax(2), gray(65535) )
% B&C
ax(3) = axes( f, "Units", "normalized", "Position", [ 50/950 500/650 400/950 100/650], "XLimitMethod", "Tight");
ax(4) = axes( f, "Units", "normalized", "Position", [ 500/950 500/650 400/950 100/650], "XLimitMethod", "Tight");
im(3) = area( ax(3), edges_r(1:(nedge-1)), bins_r, 'edgecolor','none');% histogram( ax(3), im_init_r(:) );
im(4) = area( ax(4), edges_k(1:(nedge-1)), bins_k, 'edgecolor','none');% histogram( ax(3), im_init_r(:) );
ax(3).XLim = [ cmin_r-crange_r/40, cmax_r+crange_r/40 ];
ax(4).XLim = [ cmin_k-crange_k/40, cmax_k+crange_k/40 ];
ax(3).YLim = [ 0, max(bins_r) ];
ax(4).YLim = [ 0, max(bins_k) ];
ax(3).Toolbar.Visible = 'off';
ax(4).Toolbar.Visible = 'off';
disableDefaultInteractivity(ax(3))
disableDefaultInteractivity(ax(4))
bt_r = uicontrol(f, "Style","togglebutton","String", "Auto B & C", "Units","Normalized","Position",[370/950 573/650 80/950 25/650]);
bt_k = uicontrol(f, "Style","togglebutton","String", "Auto B & C", "Units","Normalized","Position",[820/950 573/650 80/950 25/650]);
% Real Space Selector
selector_r = uicontrol(f, "Style", "popupmenu", ...
"String", ["Point", "Rectangle", "Full"],...
"Units","normalized", "Position",[50/950 0 100/950 40/650] );
% UI setup
roi_r(1) = drawpoint(ax(1),'Deletable',false,'Position',[1 1],...
'DrawingArea',[1, 1, nsy-1, nsx-1],'Visible','on');
roi_r(2) = drawrectangle(ax(1),'Deletable',false,'Position',[1, 1, round(nsx/5), round(nsx/5)],...
'DrawingArea',[1, 1, nsy-1, nsx-1],'Visible','off');
roi_r(3) = drawrectangle(ax(1),'Deletable',false,'Position',[1, 1, nsy-1, nsx-1],...
'DrawingArea',[1, 1, nsy-1, nsx-1],'Visible','off','InteractionsAllowed','none',...
'FaceAlpha',0);
% K Space Selector
selector_k = uicontrol(f, "Style", "popupmenu", ...
"String", ["Point", "Rectangle", "Full"],...
"Units","normalized", "Position",[500/950 0 100/950 40/650] );
selector_k.Value = 3;
% UI setup
roi_k(1) = drawpoint(ax(2),'Deletable',false,'Position',[1 1],...
'DrawingArea',[1, 1, ny-1, nx-1],'Visible','off');
roi_k(2) = drawrectangle(ax(2),'Deletable',false,'Position',[1, 1, round(nx/5), round(nx/5)],...
'DrawingArea',[1, 1, ny-1, nx-1],'Visible','off');
roi_k(3) = drawrectangle(ax(2),'Deletable',false,'Position',[1, 1, ny-1, nx-1],...
'DrawingArea',[1, 1, ny-1, nx-1],'Visible','on','InteractionsAllowed','none',...
'FaceAlpha',0);
sl_r(1) = drawline( ax(3), 'Position', [cmin_r -1; cmin_r, nx*ny], ...
'InteractionsAllowed','translate','color','r');
sl_r(2) = drawline( ax(3), 'Position', [cmax_r -1; cmax_r, nx*ny], ...
'InteractionsAllowed','translate','color','r');
sl_k(1) = drawline( ax(4), 'Position', [cmin_k -1; cmin_k, nsx*nsy], ...
'InteractionsAllowed','translate','color','r');
sl_k(2) = drawline( ax(4), 'Position', [cmax_k -1; cmax_k, nsx*nsy], ...
'InteractionsAllowed','translate','color','r');
addlistener( roi_r(1),'MovingROI',@(src,evnt) roi_r_1_moved( roi_r,im4D,im, ax, sl_k, bt_k) );
addlistener( roi_r(2),'MovingROI',@(src,evnt) roi_r_2_moved( roi_r,im4D,im, ax, sl_k, bt_k) );
addlistener( roi_r(3),'MovingROI',@(src,evnt) roi_r_3_moved( roi_r,im4D,im, ax, sl_k, bt_k) );
selector_r.Callback = @(src,evt) select_roi_r(src, roi_r, im4D, im, ax, sl_k, bt_k);
addlistener( roi_k(1),'MovingROI',@(src,evnt) roi_k_1_moved( roi_k,im4D,im, ax, sl_r, bt_r) );
addlistener( roi_k(2),'MovingROI',@(src,evnt) roi_k_2_moved( roi_k,im4D,im, ax, sl_r, bt_r) );
addlistener( roi_k(3),'MovingROI',@(src,evnt) roi_k_3_moved( roi_k,im4D,im, ax, sl_r, bt_r) );
selector_k.Callback = @(src,evt) select_roi_k(src, roi_k, im4D, im, ax, sl_r, bt_r);
addlistener( bt_r, 'Value','PostSet', @(src, evt) bt_pressed( bt_r, im(1), sl_r, ax(1) ) );
addlistener( bt_k, 'Value','PostSet', @(src, evt) bt_pressed( bt_k, im(2), sl_k, ax(2)) );
addlistener( sl_r,'MovingROI',@(src,evnt) set_contrast( ax(1), sl_r ) );
addlistener( sl_k,'MovingROI',@(src,evnt) set_contrast( ax(2), sl_k ) );
end
function bt_pressed( bt, im, sl, ax)
if bt.Value
cmin = min( im.CData(:));
cmax = max( im.CData(:));
sl(1).Position([1 2]) = cmin;
sl(2).Position([1 2]) = cmax;
set_contrast( ax, sl )
end
end
function set_contrast( ax, sl )
crange = [sl(1).Position(1); sl(2).Position(2)];
if crange(1) > crange(2)
crange = crange(2:-1:1);
end
ax.CLim = crange;
end
function select_roi_r( selector_r, roi_r, im4D, im, ax, sl_k, bt_k )
for i = 1:3
roi_r(i).Visible = 0;
end
roi_r(selector_r.Value).Visible = 1;
if roi_r(1).Visible
roi_r_1_moved( roi_r, im4D, im, ax, sl_k, bt_k );
elseif roi_r(2).Visible
roi_r_2_moved( roi_r, im4D, im, ax, sl_k, bt_k );
elseif roi_r(3).Visible
roi_r_3_moved( roi_r, im4D, im, ax, sl_k, bt_k );
end
end
function select_roi_k( selector_k, roi_k, im4D, im, ax, sl_r, bt_r )
for i = 1:3
roi_k(i).Visible = 0;
end
roi_k(selector_k.Value).Visible = 1;
if roi_k(1).Visible
roi_k_1_moved( roi_k, im4D, im, ax, sl_r, bt_r );
elseif roi_k(2).Visible
roi_k_2_moved( roi_k, im4D, im, ax, sl_r, bt_r );
elseif roi_k(3).Visible
roi_k_3_moved( roi_k, im4D, im, ax, sl_r, bt_r);
end
end
function roi_r_1_moved( roi_r, im4D, im, ax, sl_k, bt_k )
%xy = round(eventData.CurrentPosition);
xy = round( roi_r(1).Position);
x = xy(1); y = xy(2);
imData = squeeze(im4D(:,:,y,x));
cmin = min( imData(:)); cmax = max( imData(:) );
edges = linspace( cmin, cmax, 256);
im(2).CData = imData;
im(4).XData = edges(1:end-1);
im(4).YData = histcounts( imData, edges );
crange = cmax-cmin;
ax(4).XLim = [cmin-crange/40 cmax+crange/40];
if bt_k.Value
sl_k(1).Position([1 2]) = cmin;
sl_k(2).Position([1 2]) = cmax;
end
end
function roi_r_2_moved( roi_r, im4D, im, ax, sl_k, bt_k )
xywh = round(roi_r(2).Position);
x = xywh(1); y = xywh(2); w = xywh(3); h = xywh(4);
imData = squeeze( mean(im4D(:,:,y:(y+h),x:(x+w)),[3 4]) );
cmin = min( imData(:)); cmax = max( imData(:) );
edges = linspace( cmin, cmax, 256);
im(2).CData = imData;
im(4).XData = edges(1:end-1);
im(4).YData = histcounts( imData, edges );
crange = cmax-cmin;
ax(4).XLim = [cmin-crange/40 cmax+crange/40];
if bt_k.Value
sl_k(1).Position([1 2]) = cmin;
sl_k(2).Position([1 2]) = cmax;
end
end
function roi_r_3_moved( roi_r, im4D, im, ax, sl_k, bt_k )
imData = squeeze( mean(im4D,[3 4]) );
cmin = min( imData(:)); cmax = max( imData(:) );
edges = linspace( cmin, cmax, 256);
im(2).CData = imData;
im(4).XData = edges(1:end-1);
im(4).YData = histcounts( imData, edges );
crange = cmax-cmin;
ax(4).XLim = [cmin-crange/40 cmax+crange/40];
if bt_k.Value
sl_k(1).Position([1 2]) = cmin;
sl_k(2).Position([1 2]) = cmax;
end
end
function roi_k_1_moved( roi_k, im4D, im, ax, sl_r, bt_r )
xy = round( roi_k(1).Position);
x = xy(1); y = xy(2);
imData = squeeze(im4D(y,x,:,:));
cmin = min( imData(:)); cmax = max( imData(:) );
edges = linspace( cmin, cmax, 256);
im(1).CData = imData;
im(3).XData = edges(1:end-1);
im(3).YData = histcounts( imData, edges );
crange = cmax-cmin;
ax(3).XLim = [cmin-crange/40 cmax+crange/40];
if bt_r.Value
sl_r(1).Position([1 2]) = cmin;
sl_r(2).Position([1 2]) = cmax;
end
end
function roi_k_2_moved( roi_k, im4D, im, ax, sl_r, bt_r )
xywh = round(roi_k(2).Position);
x = xywh(1); y = xywh(2); w = xywh(3); h = xywh(4);
imData = squeeze( mean(im4D(y:(y+h),x:(x+w),:,:),[1 2]) );
cmin = min( imData(:)); cmax = max( imData(:) );
edges = linspace( cmin, cmax, 256);
im(1).CData = imData;
im(3).XData = edges(1:end-1);
im(3).YData = histcounts( imData, edges );
crange = cmax-cmin;
ax(3).XLim = [cmin-crange/40 cmax+crange/40];
if bt_r.Value
sl_r(1).Position([1 2]) = cmin;
sl_r(2).Position([1 2]) = cmax;
end
end
function roi_k_3_moved( roi_k, im4D, im, ax, sl_r, bt_r)
imData = squeeze( mean(im4D,[1 2]) );
cmin = min( imData(:)); cmax = max( imData(:) );
edges = linspace( cmin, cmax, 256);
im(1).CData = imData;
im(3).XData = edges(1:end-1);
im(3).YData = histcounts( imData, edges );
crange = cmax-cmin;
ax(3).XLim = [cmin-crange/40 cmax+crange/40];
if bt_r.Value
sl_r(1).Position([1 2]) = cmin;
sl_r(2).Position([1 2]) = cmax;
end
end