-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCellMask.m
147 lines (121 loc) · 5.34 KB
/
getCellMask.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
%[mask] = getCellMask(data, varargin)
%
% Notes: for the 'bgproj' method, the detection must be run before
% Francois Aguet, 02/17/2012
function [mask, proj] = getCellMask(data, varargin)
ip = inputParser;
ip.CaseSensitive = false;
ip.addRequired('data', @isstruct);
ip.addParameter('Overwrite', false, @islogical);
ip.addParameter('Channel', 1, @isposint);
ip.addParameter('Connect', true, @islogical);
ip.addParameter('Display', false, @islogical);
ip.addParameter('ShowHistogram', false, @islogical);
ip.addParameter('ModeRatio', 0.8, @isscalar);
ip.addParameter('Mode', 'maxproj', @(x) any(strcmpi(x, {'maxproj', 'bgproj', 'density'})));
ip.addParameter('Validate', false, @islogical);
ip.parse(data, varargin{:});
nd = numel(data);
mask = cell(1,nd);
proj = cell(1,nd);
ch = ip.Results.Channel;
se = strel('disk', 5);
hasStoredMask = true(1,nd);
for i = 1:nd
maskPath = [data(i).source 'Detection' filesep 'cellmask.tif'];
projPath = [data(i).source 'Detection' filesep 'maxproj.tif'];
if ~(exist(maskPath, 'file')==2) || ~(exist(projPath, 'file')==2) || ip.Results.Overwrite
hasStoredMask(i) = false;
switch ip.Results.Mode
case 'maxproj'
if ~iscell(data(i).framePaths{ch})
stack = readtiff(data(i).framePaths{ch});
else
stack = zeros([data(i).imagesize data(i).movieLength], 'uint16');
for f = 1:data(i).movieLength
stack(:,:,f) = imread(data(i).framePaths{ch}{f});
end
end
proj{i} = max(stack, [], 3);
mask{i} = maskFromFirstMode(double(proj{i}), 'Connect', ip.Results.Connect,...
'Display', ip.Results.ShowHistogram, 'ModeRatio', ip.Results.ModeRatio);
case 'bgproj'
% load max. 100 frames
frameRange = unique(round(linspace(1, data(i).movieLength, 100)));
aip = zeros(data(i).imagesize);
mproj = zeros(data(i).imagesize);
if iscell(data(i).framePaths{ch})
parfor f = 1:numel(frameRange)
frame = double(imread(data(i).framePaths{ch}{frameRange(f)})); %#ok<PFBNS>
dmask = 0~=double(imread(data(i).maskPaths{frameRange(f)}));
dmask = imdilate(dmask, se);
frame(dmask) = 0;
aip = aip + frame;
mproj = mproj + dmask;
end
else
info = imfinfo(data(i).framePaths{ch});
minfo = imfinfo(data(i).maskPaths);
parfor f = 1:numel(frameRange)
frame = double(readtiff(data(i).framePaths{ch}, frameRange(f), info)); %#ok<PFBNS>
dmask = 0~=double(readtiff(data(i).maskPaths, frameRange(f), minfo));
dmask = imdilate(dmask, se);
frame(dmask) = 0;
aip = aip + frame;
mproj = mproj + dmask;
end
end
aip = aip./(numel(frameRange)-mproj);
% fill to enable filtering
aip(isnan(aip)) = prctile(aip(:), 95);
mask{i} = maskFromFirstMode(aip, 'Connect', ip.Results.Connect,...
'Display', ip.Results.ShowHistogram, 'ModeRatio', ip.Results.ModeRatio);
proj{i} = aip;
case 'density'
error('Density-based mode not implemented.');
end
mask{i} = imfill(mask{i}, 'holes');
else
mask{i} = double(imread(maskPath));
proj{i} = double(imread(projPath));
end
end
% Ask user to inspect and potentially manually adjust masks
if ip.Results.Validate && any(~hasStoredMask)
vmask = manualSegmentationTweakGUI(cellfun(@double, proj(~hasStoredMask), 'unif', 0), mask(~hasStoredMask));
mask(~hasStoredMask) = vmask;
end
% save masks
for i = 1:numel(data)
if ~hasStoredMask(i)
[~,~] = mkdir([data(i).source 'Detection']);
maskPath = [data(i).source 'Detection' filesep 'cellmask.tif'];
imwrite(uint8(mask{i}), maskPath, 'tif', 'compression' , 'lzw');
projPath = [data(i).source 'Detection' filesep 'maxproj.tif'];
imwrite(proj{i}, projPath, 'tif', 'compression' , 'lzw');
end
end
if ip.Results.Display
for i = 1:nd
if ~isempty(mask{i})
[ny,nx] = size(mask{i});
B = bwboundaries(mask{i});
B = cellfun(@(i) sub2ind([ny nx], i(:,1), i(:,2)), B, 'unif', 0);
B = cell2mat(B);
bmask = zeros([ny nx]);
bmask(B) = 1;
bmask = bwmorph(bmask, 'dilate');
iproj = imread([data(i).source 'Detection' filesep 'maxproj.tif']);
iproj = scaleContrast(log(double(iproj)));
iproj(bmask==1) = 0;
overlay = iproj;
overlay(bmask==1) = 255;
overlay = uint8(cat(3, overlay, iproj, iproj));
figure; imagesc(overlay); axis image;
end
end
end
if nd==1
mask = mask{1};
proj = proj{1};
end