Repository navigation
Expand file tree
/
Copy pathrematchAtDistance.m
More file actions
44 lines (40 loc) · 2.21 KB
/
Copy pathrematchAtDistance.m
File metadata and controls
44 lines (40 loc) · 2.21 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
function MC2 = rematchAtDistance(rootFolder, tag1, tag2, MC, maxDist)
% rematchAtDistance Rebuild a cell matching at a new distance cutoff, reusing
% the transform already in MC (same matching math as matchCells, but without
% re-loading images or re-picking points).
%
% Used by the Extended Activity Tracking add-on to recover cells when the
% default 5 px cutoff is too strict (e.g. higher-drift comparisons such as 24 h).
% Note: a larger cutoff recovers more pairs but raises the risk of pairing the
% wrong cells, and it can never match more cells than were detected at the
% sparser timepoint.
%
% INPUTS
% rootFolder - sample root folder (with <tag>/center.mat and <tag>/radii.mat)
% tag1, tag2 - timepoint tags (tag1 = column 1 of matchedCellNumbers)
% MC - existing MatchedCells struct (must contain transformMatrix)
% maxDist - new distance cutoff in pixels (timepoint-1 frame)
%
% OUTPUT
% MC2 - MatchedCells struct with the re-matched pairs at the new cutoff
% (keeps the original images/transform; updates the matched fields and
% sets MC2.matchDist = maxDist).
if ~isstruct(MC) || ~isfield(MC,'transformMatrix') || isempty(MC.transformMatrix)
error('rematchAtDistance:noTransform', 'The existing matching has no transform to reuse.');
end
centroids1 = importdata(fullfile(rootFolder, tag1, 'center.mat'));
radii1 = importdata(fullfile(rootFolder, tag1, 'radii.mat'));
centroids2 = importdata(fullfile(rootFolder, tag2, 'center.mat'));
radii2 = importdata(fullfile(rootFolder, tag2, 'radii.mat'));
centroids2_aligned = transformPointsForward(MC.transformMatrix, centroids2);
[idx, dist] = knnsearch(centroids2_aligned, centroids1, 'K', 1);
valid = dist < maxDist;
MC2 = MC;
MC2.matchedCellNumbers = [find(valid), idx(valid)]; % [id_tag1, id_tag2]
MC2.matchedCentroidCoordinates_0minus = centroids1(valid,:);
MC2.matchedRadii_0minus = radii1(valid);
MC2.matchedCentroidCoordinates_timepoint2 = centroids2(idx(valid),:);
MC2.matchedRadii_timepoint2 = radii2(idx(valid));
MC2.validMatches_image1 = valid;
MC2.matchDist = maxDist;
end