Repository navigation
Expand file tree
/
Copy pathshowCellMatching.m
More file actions
60 lines (54 loc) · 2.67 KB
/
Copy pathshowCellMatching.m
File metadata and controls
60 lines (54 loc) · 2.67 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
function fig = showCellMatching(mc, tag1, tag2)
% showCellMatching Draw the cell-matching alignment figure from a saved
% matching (no re-picking): the two aligned frames overlaid (imshowpair) with
% the matched somas and links between them.
%
% USAGE
% showCellMatching('...\BaselineChange\MatchedCells_0minus_24minus.mat','0minus','24minus')
% showCellMatching(MatchedCells) % or pass the struct directly
%
% INPUTS
% mc - a MatchedCells struct, or a path to a .mat file containing one
% tag1, tag2 - timepoint names for the labels (optional)
if nargin < 2 || isempty(tag1), tag1 = 'timepoint 1'; end
if nargin < 3 || isempty(tag2), tag2 = 'timepoint 2'; end
tag1 = char(tag1); tag2 = char(tag2);
if ischar(mc) || isstring(mc)
S = load(mc);
if isfield(S,'MatchedCells')
mc = S.MatchedCells;
else
fn = fieldnames(S);
hit = fn(contains(fn,'MatchedCells','IgnoreCase',true));
if isempty(hit), error('No MatchedCells struct found in %s', mc); end
mc = S.(hit{1});
end
end
if ~isfield(mc,'GCaMP_Image1') || ~isfield(mc,'GCaMP_Image2') || ~isfield(mc,'transformMatrix')
error('MatchedCells is missing GCaMP images or the transform needed to draw the overlay.');
end
im1 = im2double(mc.GCaMP_Image1);
im2 = im2double(mc.GCaMP_Image2);
T = mc.transformMatrix;
im2a = imwarp(im2, T, 'OutputView', imref2d(size(im1)));
fig = figure('Color','w','Name','Cell matching','NumberTitle','off');
imshowpair(im1, im2a, 'falsecolor'); hold on; % im1 -> green, im2 -> magenta
nMatched = 0;
if isfield(mc,'matchedCentroidCoordinates_0minus') && ~isempty(mc.matchedCentroidCoordinates_0minus) ...
&& isfield(mc,'matchedCentroidCoordinates_timepoint2')
c1 = mc.matchedCentroidCoordinates_0minus; % timepoint-1 frame
c2a = transformPointsForward(T, mc.matchedCentroidCoordinates_timepoint2); % -> timepoint-1 frame
nMatched = size(c1,1);
h1 = plot(c1(:,1), c1(:,2), 'ro', 'MarkerSize',10, 'LineWidth',1.5);
h2 = plot(c2a(:,1), c2a(:,2), 'g+', 'MarkerSize',10, 'LineWidth',1.5);
h3 = gobjects(1);
for i = 1:nMatched
h3 = plot([c1(i,1) c2a(i,1)], [c1(i,2) c2a(i,2)], 'b-', 'LineWidth',1);
end
legend([h1 h2 h3], {[tag1 ' somas'], [tag2 ' somas (aligned)'], 'matches'}, ...
'Location','northeast'); % default (black) legend text so it is visible
end
hold off;
title({sprintf('Aligned images and matched somas (%d cells matched)', nMatched), ...
sprintf('overlay: green = %s, magenta = %s', tag1, tag2)});
end