forked from mporter-gre/mtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROISegmentMeasureAndMask.m
313 lines (287 loc) · 15.1 KB
/
ROISegmentMeasureAndMask.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
function [roiShapes, fullMaskImg, data, dataAround, objectCounter, objectData, objectDataAround, numSegPixels, measureSegChannel] = ROISegmentMeasureAndMask(roiShapes, pixels, measureChannels, measureAroundChannels, segChannel, verifyZ, featherSize, groupObjects, minSize, selectedSegType, threshold, channelsToFetch, numROI, annulusSize, gapSize)
%Author Michael Porter
% Copyright (C) 2009-2014 University of Dundee.
% All rights reserved.
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
global fig1;
global ROIText;
global sectionClicked;
global sectionFigure;
global session;
maxX = pixels.getSizeX.getValue;
maxY = pixels.getSizeY.getValue;
fullZ = pixels.getSizeZ.getValue;
fullT = pixels.getSizeT.getValue;
imageId = pixels.getImage.getId.getValue;
sigmaMultiplier = threshold;
for thisFullT = 1:fullT
fullMaskImg{thisFullT} = uint8(zeros(maxY,maxX,fullZ));
end
if ~ismember(segChannel, channelsToFetch)
channelsToFetch = [segChannel channelsToFetch];
measureSegChannel = 0;
end
for thisZ = 1:fullZ
for thisC = channelsToFetch
for thisT = 1:fullT
zctStack(:,:,thisZ,thisC,thisT) = getPlane(session, imageId, thisZ-1, thisC-1, thisT-1);
end
end
end
for thisROI = 1:numROI
timePoints = getROITimePoints(roiShapes{thisROI});
numShapes = roiShapes{thisROI}.numShapes;
numT(thisROI) = length(timePoints);
for thisT = 1:numT(thisROI)
numZthisT = 1;
shapeCounter = 1;
for thisShape = 1:numShapes
if thisT == roiShapes{thisROI}.(['shape' num2str(thisShape)]).getTheT.getValue + 1
roiShapesThisT{1}.(['shape' num2str(shapeCounter)]) = roiShapes{thisROI}.(['shape' num2str(thisShape)]);
numZthisT = numZthisT + 1;
shapeCounter = shapeCounter + 1;
end
end
roiShapesThisT{1}.numShapes = numZthisT - 1;
roiShapesThisT{1}.shapeType = roiShapes{thisROI}.shapeType;
[patches measureSegChannel] = cutPatchesFromROI(roiShapesThisT, segChannel, channelsToFetch, pixels, 1, zctStack, thisT); %The 1 at the end == thisROI in cutPatches...
%set(ROIText, 'String', ['ROI ', num2str(thisROI), ' of ' num2str(numROI)]);
drawnow;
baseZ = roiShapes{thisROI}.shape1.getTheZ.getValue;
numZ(thisROI) = length(roiShapesThisT{1}.numShapes);
maxX = pixels.getSizeX.getValue;
maxY = pixels.getSizeY.getValue;
width = roiShapes{thisROI}.shape1.getWidth.getValue;
height = roiShapes{thisROI}.shape1.getHeight.getValue;
patchMax = max(reshape(patches{segChannel}, 1, []));
if strcmpi(selectedSegType, 'Otsu')
[patchMasks lowestValue] = seg3D(patches{segChannel}, featherSize, groupObjects, minSize); %The 3D segmentation
elseif strcmpi(selectedSegType, 'Spots')
patchMasks = spotSeg3D(patches{segChannel});
patchMasks = filterSpotSize(patchMasks, minSize);
if groupObjects == 1
patchMasks = bwlabeln(patchMasks);
end
if featherSize > 0
se = strel('diamond', featherSize);
patchMasks = imdilate(patchMasks, se);
end
elseif strcmpi(selectedSegType, 'Absolute')
[patchMasks lowestValue] = seg3DThresh(patches{segChannel}, featherSize, groupObjects, threshold, minSize, patchMax);
elseif strcmpi(selectedSegType, 'Sigma')
patchesLinear = reshape(patches{segChannel}, [], 1);
[patchMean, patchStd] = normfit(patchesLinear);
threshold = patchMean + sigmaMultiplier * patchStd;
%sigmaMultiplier = threshold;
%threshold = getSigmaThreshold(patches{segChannel}, sigmaMultiplier);
[patchMasks lowestValue] = seg3DThresh(patches{segChannel}, featherSize, groupObjects, threshold, minSize, patchMax);
end
%Show the user the results of the segmentation, and ask them to specify
%the correct range of Z-sections. If there is only 1 Z or there is nothing segmented
%then just use it.
maxPatch = max(max(max(patchMasks)));
if numZthisT > 1 && maxPatch > 0
if verifyZ == 1
[startZ, stopZ] = zChooser(patchMasks);
else
startZ = 1;
stopZ = numZthisT-1;
end
else
startZ = 1;
stopZ = 1;
end
%set(ROIText, 'String', 'Creating mask and calculating intensities...');
drawnow;
roiShapes{thisROI}.startZ = startZ;
roiShapes{thisROI}.stopZ = stopZ;
roiShapes{thisROI}.numZ = stopZ-startZ;
numZ(thisROI) = stopZ-startZ+1;
patchMasks = patchMasks(:,:,startZ:stopZ);
numSegPixels{thisROI}(thisT) = length(find(reshape((patchMasks>0),1,[])));
X = floor(roiShapes{thisROI}.shape1.getX.getValue)+1; %svg entry in xml file indexes from (0, 0) instead of (1, 1), so +1
Y = floor(roiShapes{thisROI}.shape1.getY.getValue)+1;
partMaskImg = uint8(zeros(maxY,maxX,numZ(thisROI)));
%Measure intensities under the segmentation mask for the intended
%channels.
counter = 1;
for thisMeasureChannel = measureChannels
thisROIIntensityShape{counter} = zeros(size(patchMasks));
ROIEndX = X+width;
ROIEndY = Y+height;
if ROIEndX > maxX || ROIEndY > maxY || X < 0 || Y < 0
cropPatch = int8(1);
if X < 1
X = 1;
end
if Y < 1
Y = 1;
end
else
cropPatch = 0;
end
for thisZ = 1:numZ(thisROI)
if floor(cropPatch) == 1
for col = 1:width
posX = col+X-1;
if posX > maxX %If the ROI was drawn to extend off the image, set the crop to the edge of the image only.
posX = maxX;
end
for row = 1:height
posY = row+Y-1;
if posY > maxY
posY = maxY;
end
partMaskImg(posY, posX, thisZ) = patchMasks(row, col, thisZ);
end
end
else
posX = X+width-1;
posY = Y+height-1;
partMaskImg(Y:posY,X:posX, thisZ) = patchMasks(:,:, thisZ);
end
[patchYLocs, patchXLocs] = find(patchMasks(:,:,thisZ));
lengthPatchLocs = length(patchYLocs);
for thisPatchLoc = 1:lengthPatchLocs
thisROIIntensityShape{counter}(patchYLocs(thisPatchLoc),patchXLocs(thisPatchLoc),thisZ) = patches{thisMeasureChannel}(patchYLocs(thisPatchLoc), patchXLocs(thisPatchLoc), thisZ);
end
end
%thisROIIntensityShape{counter}(patchMasks) = patches{thisMeasureChannel}(patchMasks);
counter = counter + 1;
end
%Measure intensities around the segmentation mask for the intended
%channels. Detect if the 'around' signal come from an annulus.
counter = 1;
for thisMeasureAroundChannel = measureAroundChannels
thisAroundROIIntensityShape{counter} = zeros(size(patchMasks));
if annulusSize > 0
diskSize = annulusSize + gapSize;
se = strel('disk', diskSize);
seGap = strel('disk', gapSize);
patchMasksBkg = patchMasks;
patchMasksBkgDil = imdilate(patchMasksBkg, se);
patchMasksSigPlusGap = imdilate(patchMasksBkg, seGap);
patchMasksBkgDil(patchMasksSigPlusGap>0) = 0;
thisAroundROIIntensityShape{counter}(logical(patchMasksBkgDil)) = patches{thisMeasureAroundChannel}(logical(patchMasksBkgDil));
else
thisAroundROIIntensityShape{counter}(~patchMasks) = patches{thisMeasureAroundChannel}(~patchMasks);
end
counter = counter + 1;
end
roiZ = 1;
for thisZ = baseZ+startZ:baseZ+stopZ
fullMaskImg{thisT}(:,:,thisZ) = fullMaskImg{thisT}(:,:,thisZ) + partMaskImg(:,:,roiZ);
roiZ = roiZ + 1;
end
fullMaskImg{thisT} = fullMaskImg{thisT}.*255;
counter = 1;
if ~isempty(measureChannels)
for thisMeasureChannel = measureChannels
intensityVector = thisROIIntensityShape{counter}(patchMasks>0);
data{thisROI}{thisT}{counter}.sumPix = sum(intensityVector);
data{thisROI}{thisT}{counter}.numPix = length(intensityVector);
data{thisROI}{thisT}{counter}.meanPix = data{thisROI}{thisT}{counter}.sumPix / data{thisROI}{thisT}{counter}.numPix;
data{thisROI}{thisT}{counter}.stdPix = std(intensityVector);
data{thisROI}{thisT}{counter}.channel = thisMeasureChannel;
if groupObjects == 0
[row, col, objectValues] = find(unique(patchMasks));
numObjects = length(objectValues);
objectCounter{thisROI}{thisT}.numObjects = numObjects;
%If there are no segmented pixels per object enter zero.
if isempty(objectValues)
objectData{thisROI}{thisT}{1}{counter}.sumPix = 0;
objectData{thisROI}{thisT}{1}{counter}.numPix = 0;
objectData{thisROI}{thisT}{1}{counter}.meanPix = 0;
objectData{thisROI}{thisT}{1}{counter}.stdPix = 0;
objectData{thisROI}{thisT}{1}{counter}.channel = thisMeasureChannel;
else
objectIntensityVector = {};
for thisObject = 1:numObjects
objectIntensityVector{thisObject} = thisROIIntensityShape{counter}(find(patchMasks==objectValues(thisObject)));
objectData{thisROI}{thisT}{thisObject}{counter}.sumPix = sum(objectIntensityVector{thisObject});
objectData{thisROI}{thisT}{thisObject}{counter}.numPix = length(objectIntensityVector{thisObject});
objectData{thisROI}{thisT}{thisObject}{counter}.meanPix = objectData{thisROI}{thisT}{thisObject}{counter}.sumPix / objectData{thisROI}{thisT}{thisObject}{counter}.numPix;
objectData{thisROI}{thisT}{thisObject}{counter}.stdPix = std(objectIntensityVector{thisObject});
objectData{thisROI}{thisT}{thisObject}{counter}.channel = thisMeasureChannel;
end
end
else
objectData = [];
objectCounter{thisROI}{thisT}.numObjects = 1;
end
counter = counter + 1;
end
else
data{thisROI}{thisT}{counter}.sumPix = [];
data{thisROI}{thisT}{counter}.numPix = [];
data{thisROI}{thisT}{counter}.meanPix = [];
data{thisROI}{thisT}{counter}.stdPix = [];
data{thisROI}{thisT}{counter}.channel = [];
if groupObjects == 1
objectData = [];
end
end
counter = 1;
if ~isempty(measureAroundChannels)
for thisMeasureAroundChannel = measureAroundChannels
intensityAroundVector = thisAroundROIIntensityShape{counter}(find(thisAroundROIIntensityShape{counter}));
dataAround{thisROI}{thisT}{counter}.sumPix = sum(intensityAroundVector);
dataAround{thisROI}{thisT}{counter}.numPix = length(intensityAroundVector);
dataAround{thisROI}{thisT}{counter}.meanPix = dataAround{thisROI}{thisT}{counter}.sumPix / dataAround{thisROI}{thisT}{counter}.numPix;
dataAround{thisROI}{thisT}{counter}.stdPix = std(intensityAroundVector);
dataAround{thisROI}{thisT}{counter}.channel = thisMeasureAroundChannel;
%Think about objectDataAround a bit more before including this
%kind of code.
if groupObjects == 0 && annulusSize > 0
[row, col, objectValues] = find(unique(patchMasksBkg));
numObjects = length(objectValues);
%If there are no segmented pixels per object enter zero.
if isempty(objectValues)
objectDataAround{thisROI}{thisT}{1}{counter}.sumPix = 0;
objectDataAround{thisROI}{thisT}{1}{counter}.numPix = 0;
objectDataAround{thisROI}{thisT}{1}{counter}.meanPix = 0;
objectDataAround{thisROI}{thisT}{1}{counter}.stdPix = 0;
objectDataAround{thisROI}{thisT}{1}{counter}.channel = thisMeasureChannel;
else
for thisObject = 1:numObjects
objectIntensityVector = thisAroundROIIntensityShape{counter}(patchMasksBkgDil==objectValues(thisObject));
objectDataAround{thisROI}{thisT}{counter}{thisObject}.sumPix = sum(objectIntensityVector);
objectDataAround{thisROI}{thisT}{counter}{thisObject}.numPix = length(objectIntensityVector);
objectDataAround{thisROI}{thisT}{counter}{thisObject}.meanPix = objectDataAround{thisROI}{thisT}{counter}{thisObject}.sumPix / objectDataAround{thisROI}{thisT}{counter}{thisObject}.numPix;
objectDataAround{thisROI}{thisT}{counter}{thisObject}.stdPix = std(objectIntensityVector);
objectDataAround{thisROI}{thisT}{counter}{thisObject}.channel = thisMeasureChannel;
end
end
else
objectDataAround = [];
end
counter = counter + 1;
end
else
dataAround{thisROI}{thisT}{counter}.sumPix = [];
dataAround{thisROI}{thisT}{counter}.numPix = [];
dataAround{thisROI}{thisT}{counter}.meanPix = [];
dataAround{thisROI}{thisT}{counter}.stdPix = [];
dataAround{thisROI}{thisT}{counter}.channel = [];
objectDataAround = [];
end
end
end
clear('patchMasks');
clear('intensityVector');
clear('partMaskImg');
clear('thisROIIntensityShape');
end