-
Notifications
You must be signed in to change notification settings - Fork 1
/
Widefield_alignToAllen.m
357 lines (250 loc) · 8.74 KB
/
Widefield_alignToAllen.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
354
355
356
357
function Widefield_alignToAllen(pic)
% alignBrainToAllen(WFIm)
% alignBrainToAllen(animal, theDate)
%
% GUI to align brains to the Allen atlas.
%
% Inputs:
% Option 1: WFIm should be some useful image from the WF, such as blueAvg.
% Once alignment is done, you can send the resulting parameters
% to the Matlab workspace.
dat.WFIm = pic;
%% Parameters
% Points to click, in Allen coordinates relative to our trimmed brain image
dat.refPts = [
148 79; % Base of L OB
228.5 76 % Center base of OBs
308 79 % Base of R OB
228.5 335 % Base of RS
];
dat.bregmaRef = [228.5 190];
dat.allenCrossSize = 15;
dat.allenCrossLineWidth = 3;
dat.crossColors = {'g', 'r', 'c', 'w'};
dat.bregmaColor = 'y';
dat.selectedCrossSize = 12;
dat.edgeColor = [1 1 1];
%% Load Allen data maps
loadvar = load('allenDorsalMap', 'dorsalMaps');
dat.aMaps = loadvar.dorsalMaps;
dat.nAreas = max(dat.aMaps.dorsalMap(:));
%% Set up figure
hf = figure;
hf.Position = [100 250 1200 500];
hf.NumberTitle = 'off';
hf.Name = 'Brain Aligner';
drawnow;
hf.Pointer = 'crosshair';
%% Set up reference axes, plot Allen brain
% Axes
dat.axAllen = axes(hf, 'Position', [0.05 0.05 0.4 0.9]);
% Plot brain
imagesc(dat.aMaps.dorsalMap);
colormap(colorcube(dat.nAreas));
axis equal off;
hold on;
%% Set up widefield image axes
dat.axWF = axes(hf, 'Position', [0.5 0.05 0.4 0.9]);
maxBright = prctile(dat.WFIm(:), 99);
dat.WFIm(dat.WFIm > maxBright) = maxBright;
hIm = imagesc(dat.WFIm);
colormap(dat.axWF, gray(256));
axis equal off;
hold on;
% Make brain clickable
hIm.ButtonDownFcn = @advanceSelection;
%% Set up data structures to hold selections and reference crosses
dat.selectedPts = NaN(size(dat.refPts));
dat.WFCrossHs = [];
dat.refI = 1;
hf.UserData = dat;
% Draw first target cross
drawCrossOnAllen(hf);
title(dat.axWF, 'Click to place a ref point');
function drawCrossOnAllen(hf)
%% Retrieve all the data from the figure
dat = hf.UserData;
if dat.refI <= size(dat.refPts, 1)
% Normal reference point
plot(dat.axAllen, dat.refPts(dat.refI, 1), dat.refPts(dat.refI, 2), '+', ...
'MarkerSize', dat.allenCrossSize, 'LineWidth', dat.allenCrossLineWidth, ...
'color', dat.crossColors{dat.refI});
else
% Bregma
plot(dat.axAllen, dat.bregmaRef(1), dat.bregmaRef(2), '+', ...
'MarkerSize', dat.allenCrossSize, 'LineWidth', dat.allenCrossLineWidth, ...
'color', dat.bregmaColor);
plot(dat.axAllen, dat.bregmaRef(1), dat.bregmaRef(2), 'o', ...
'MarkerSize', dat.allenCrossSize - 4, 'LineWidth', dat.allenCrossLineWidth, ...
'color', dat.bregmaColor);
end
hf.UserData = dat;
function advanceSelection(src, ev)
%% Retrieve all the data from the figure
hf = gcbf;
dat = hf.UserData;
if strcmp(hf.SelectionType, 'normal')
%% Left click, advance
% If we've made as many selections as we can (including Bregma), ignore click
if dat.refI > size(dat.refPts, 1) + 1
return;
end
%% Find click
% Current mouse coordinates
pt = dat.axWF.CurrentPoint(1, 1:2);
if dat.refI <= size(dat.refPts, 1)
% Normal reference point
dat.selectedPts(dat.refI, :) = pt;
dat.WFCrossHs(dat.refI) = plot(dat.axWF, pt(1), pt(2), '+', ...
'MarkerSize', dat.selectedCrossSize, 'color', dat.crossColors{dat.refI});
% Make cross draggable
draggableActPlusUp(dat.WFCrossHs(dat.refI), [], @moveCross);
else
% Bregma
dat.bregma = pt;
hc = plot(dat.axWF, pt(1), pt(2), 'x', ...
'MarkerSize', dat.selectedCrossSize, 'color', dat.bregmaColor);
% Make cross draggable
draggableActPlusUp(hc, [], @moveBregma);
end
%% Advance
dat.refI = dat.refI + 1;
hf.UserData = dat;
if dat.refI > size(dat.refPts, 1) + 1
hf.Pointer = 'arrow';
dat.btnDone.Enable = 'on';
title(dat.axWF, 'Drag markers to revise');
elseif dat.refI == size(dat.refPts, 1) + 1
drawCrossOnAllen(hf);
title(dat.axWF, 'Click to estimate Bregma (ignore the ''V'' in the suture)');
else
drawCrossOnAllen(hf);
title(dat.axWF, 'Click to place a ref point or drag a cross to revise');
end
end
function performAlignment(src, ev)
hf = gcbf;
dat = hf.UserData;
%% Rescale reference points to our resolution
scaledRefs = (dat.refPts - 1) * dat.aMaps.allenPixelSize / dat.aMaps.desiredPixelSize + 1;
%% Perform the alignment itself, in 2 coordinate frames
% Image coordinates:
% Use the Center of the image, because that's how imrotate works. The +0.5
% is because Matlab indexing starts at 1.
c = fliplr(size(dat.WFIm)) / 2 + 0.5;
scaledRefsC = scaledRefs - c;
selectedPtsC = dat.selectedPts - c;
% Kabsch algorithm to align the points with a rotation and translation
[RC, tC] = rigidAlignPts(selectedPtsC', scaledRefsC', dat.chkScale.Value); % For image
[R, t] = rigidAlignPts(dat.selectedPts', scaledRefs', dat.chkScale.Value); % For alignment points
% Retrieve the angle from the rotation matrix, rotate the image
transParams.scaleConst = sqrt(R(1, 1) ^ 2 + R(1, 2) ^ 2);
ang = acosd(RC(1) / transParams.scaleConst);
ang = ang * sign(RC(1, 2));
% Pack the outputs
transParams.angleD = ang;
transParams.R = R;
transParams.t = t;
transParams.RC = RC;
transParams.tC = tC;
WF = alignAllenTransIm(dat.WFIm, transParams);
%% Set up figure
hf = figure;
hf.Position = [100 150 1200 500];
%% First panel: Aligned image with selected points and reference points
% Annoyingly, we need to know the colormap resolution used inside
% showAllenEdgesOnWF, because it will override the colormap
cRes = 255;
subplot(1, 3, 1);
image(cRes * (WF - min(WF(:))) / range(WF(:)));
% colormap(gray);
axis equal off;
% Crosses for reference points, X's for selected points. This lets us see
% how well they aligned
hold on;
plot(scaledRefs(2, 1) * [1 1], [1 size(WF, 1)], 'w-');
selPts = (R * dat.selectedPts' + t)';
for p = 1:size(dat.refPts, 1)
plot(scaledRefs(p, 1), scaledRefs(p, 2), '+', ...
'MarkerSize', 12, 'LineWidth', 1, 'color', dat.crossColors{p});
plot(selPts(p, 1), selPts(p, 2), 'x', ...
'MarkerSize', 12, 'LineWidth', 1, 'color', dat.crossColors{p});
end
bregma = (R * dat.bregma' + t)';
plot(bregma(1), bregma(2), 'x', ...
'MarkerSize', 12, 'LineWidth', 1, 'color', dat.bregmaColor);
plot(bregma(1), bregma(2), 'o', ...
'MarkerSize', 8, 'LineWidth', 1, 'color', dat.bregmaColor);
transParams.refPts = dat.refPts;
transParams.scaledRefs = scaledRefs;
transParams.selectedPts = dat.selectedPts';
transParams.transSelected = selPts;
transParams.selectedBregma = dat.bregma;
transParams.transBregma = bregma;
transParams.meanRefErr = mean(sqrt(sum((scaledRefs - selPts) .^ 2, 2)));
title(sprintf('Mean ref error: %0.2f', transParams.meanRefErr));
%% Second panel: Aligned image with borders
subplot(1, 3, 2);
showAllenEdgesOnWF(dat.WFIm, transParams, dat.aMaps, 0, 0);
if dat.chkScale.Value
title(sprintf('Brain scaled to %0.1f%%', 100 * transParams.scaleConst));
else
title('No brain scaling allowed');
end
%% Third panel: Aligned image, masked
subplot(1, 3, 3);
showAllenEdgesOnWF(dat.WFIm, transParams, dat.aMaps, 1, 0);
if transParams.meanRefErr < 4
title('Very nice, well done old chap');
end
%% Button to save transform to workspace
hbw = uicontrol('Style', 'pushbutton', ...
'String', 'Save params to workspace', ...
'Units', 'Normalized', 'Position', [0.25 0.05 0.2 0.08], ...
'ForegroundColor', 'w', ...
'BackgroundColor', [0 0 1]);
hbw.Callback = @(~, ~) transParamsToWS(transParams, hbw);
%% Button to save transform to opts2.mat, if available
if dat.havePath
hbs = uicontrol('Style', 'pushbutton', ...
'String', 'Save params to opts2.mat', ...
'Units', 'Normalized', 'Position', [0.55 0.05 0.2 0.08], ...
'ForegroundColor', 'w', ...
'BackgroundColor', [1 0 0]);
hbs.Callback = @(~, ~) saveOpts(dat.path, transParams, hbs);
end
function moveCross(src, ev)
% Update data structure after dragging a reference point
hf = gcbf;
dat = hf.UserData;
refI = find(dat.WFCrossHs == src, 1);
% Paranoia, should never happen
if isempty(refI)
error('Lost track of handle for dragged reference point');
end
dat.selectedPts(refI, :) = [src.XData src.YData];
hf.UserData = dat;
function moveBregma(src, ev)
% Update data structure after dragging a reference point
hf = gcbf;
dat = hf.UserData;
dat.bregma = [src.XData src.YData];
hf.UserData = dat;
function transParamsToWS(transParams, src)
assignin('base', 'transParams', transParams);
src.Enable = 'off';
src.BackgroundColor = [0.5 0.5 1];
function saveOpts(thePath, transParams, src)
loadVar = load(fullfile(thePath, 'opts.mat'));
fnames = fieldnames(loadVar);
if ~isfield(loadVar, 'opts')
error('opts.mat got deleted!');
end
if length(fnames) > 1
error('opts.mat contains variables besides opts!');
end
opts = loadVar.opts;
opts.transParams = transParams;
save(fullfile(thePath, 'opts2.mat'), 'opts');
src.Enable = 'off';
src.BackgroundColor = [1 0.5 0.5];