forked from mporter-gre/mtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROISegmentDistanceAndMask.m
254 lines (221 loc) · 9.57 KB
/
ROISegmentDistanceAndMask.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
function [roishapeIdx, fullMaskImg, data, dataAround] = ROISegmentDistanceAndMask(roishapeIdx, patches, pixels, channelsToMeasure)
%Author Michael Porter
%Copyright 2009 University of Dundee. All rights reserved
% Copyright (C) 2013-2014 University of Dundee & Open Microscopy Environment.
% 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 objectViewer;
global objectClicked;
maxX = pixels.getSizeX.getValue;
maxY = pixels.getSizeY.getValue;
fullZ = pixels.getSizeZ.getValue;
fullMaskImg = uint8(zeros(maxY,maxX,fullZ));
numChannels = length(patches);
numROI = length(roishapeIdx);
for thisROI = 1:numROI
set(ROIText, 'String', ['ROI ', num2str(thisROI), ' of ' num2str(numROI)]);
drawnow;
baseZ = roishapeIdx{thisROI}.Z(1);
numZ(thisROI) = length(unique(roishapeIdx{thisROI}.Z));
numT(thisROI) = length(unique(roishapeIdx{thisROI}.T));
maxX = pixels.getSizeX.getValue;
maxY = pixels.getSizeY.getValue;
width = roishapeIdx{thisROI}.Width(1);
height = roishapeIdx{thisROI}.Height(1);
for thisChannel = 1:2
patchMasks{thisChannel}{thisROI} = seg3D(patches{channelsToMeasure(thisChannel)}{thisROI}, 0); %The 3D segmentation
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 then just use
%it.
combinedPatchMasks = patchMasks{1}{thisROI} + patchMasks{2}{thisROI};
if numZ > 1
if numZ > 25
showmeRawLargeScreenZSections(combinedPatchMasks);
else
showmeRaw(combinedPatchMasks);
end
sectionsChosen = 0;
while sectionsChosen == 0
set(ROIText, 'String', 'Click on the LOWEST correct Z-Section.');
figure(fig1);
drawnow;
uiwait(fig1);
startZ = sectionClicked;
set(ROIText, 'String', 'Click on the Highest correct Z-Section.');
figure(fig1);
drawnow;
uiwait(fig1);
stopZ = sectionClicked;
questionStr = ['Lowest Z = ', num2str(startZ), ', highest Z = ', num2str(stopZ), '?'];
response = questdlg(questionStr, 'Correct Z-Sections?', 'Yes', 'No', 'Yes');
if strcmp(response, 'Yes')
sectionsChosen = 1;
end
end
close(sectionFigure);
else
startZ = 1;
stopZ = 1;
end
set(ROIText, 'String', 'Creating mask and calculating intensities...');
drawnow;
roishapeIdx{thisROI}.startZ = startZ;
roishapeIdx{thisROI}.stopZ = stopZ;
roishapeIdx{thisROI}.numZ = stopZ-startZ;
numZ(thisROI) = stopZ-startZ+1;
for thisChannel = 1:2
patchMasks{thisChannel}{thisROI} = patchMasks{thisChannel}{thisROI}(:,:,startZ:stopZ);
end
X = roishapeIdx{thisROI}.X(1)+1; %svg entry in xml file indexes from (0, 0) instead of (1, 1), so +1
Y = roishapeIdx{thisROI}.Y(1)+1;
partMaskImg{thisChannel}{thisROI} = uint8(zeros(maxY,maxX,numZ(thisROI)));
%Find the number of objects segmented in the ROI in each channel. If
%there is more than one then going to have to ask the user which one to
%use.
for thisChannel = 1:2
[patchMasksbwln{thisChannel}{thisROI} numObjects{thisChannel}{thisROI}] = bwlabeln(patchMasks{thisChannel}{thisROI});
end
objectsChosen = 0;
while objectsChosen == 0
set(ROIText, 'String', 'Click on the object to measure the distance FROM.');
objectViewAndSelect(patchMasksbwln{1}{thisROI});
figure(objectViewer);
drawnow;
uiwait(objectViewer);
object1 = objectClicked
set(ROIText, 'String', 'Click on the object to measure the distance TO.');
objectViewAndSelect(patchMasksbwln{2}{thisROI})
figure(objectViewer);
drawnow;
uiwait(objectViewer);
object2 = objectClicked
questionStr = 'Are you sure?';
response = questdlg(questionStr, 'Correct Z-Sections?', 'Yes', 'No', 'Yes');
if strcmp(response, 'Yes')
objectsChosen = 1;
end
end
close(objectViewer);
2+2;
%Measure intensities under the segmentation mask for the intended
%channels.
counter = 1;
for thisMeasureChannel = measureChannels
thisROIIntensityShape{counter}{thisROI} = zeros(height,width,numZ(thisROI));
for thisZ = 1:numZ(thisROI)
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
end
end
partMaskImg{thisROI}(Y:Y+height-1,X:X+width-1, thisZ) = patchMasks{thisROI}(:,:, thisZ);
thisROIIntensityShape{counter}{thisROI}(patchMasks{thisROI}(:,:,thisZ)) = patches{thisMeasureChannel}{thisROI}(patchMasks{thisROI}(:,:,thisZ));
end
counter = counter + 1;
end
%Measure intensities around the segmentation mask for the intended
%channels.
counter = 1;
for thisMeasureAroundChannel = measureAroundChannels
thisAroundROIIntensityShape{counter}{thisROI} = zeros(height,width,numZ(thisROI));
for thisZ = 1:numZ(thisROI)
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
end
end
thisAroundROIIntensityShape{counter}{thisROI}(~patchMasks{thisROI}(:,:,thisZ)) = patches{thisMeasureAroundChannel}{thisROI}(~patchMasks{thisROI}(:,:,thisZ));
end
counter = counter + 1;
end
roiZ = 1;
for thisZ = baseZ+startZ:baseZ+stopZ
fullMaskImg(:,:,thisZ) = fullMaskImg(:,:,thisZ) + partMaskImg{thisROI}(:,:,roiZ);
roiZ = roiZ + 1;
end
counter = 1;
if ~isempty(measureChannels)
for thisMeasureChannel = measureChannels
intensityVector = thisROIIntensityShape{counter}{thisROI}(find(thisROIIntensityShape{counter}{thisROI}));
data{thisROI}{counter}.sumPix = sum(intensityVector);
data{thisROI}{counter}.numPix = length(intensityVector);
data{thisROI}{counter}.meanPix = data{thisROI}{counter}.sumPix / data{thisROI}{counter}.numPix;
data{thisROI}{counter}.stdPix = std(intensityVector);
data{thisROI}{counter}.channel = thisMeasureChannel;
counter = counter + 1;
end
else
data{thisROI}{counter}.sumPix = [];
data{thisROI}{counter}.numPix = [];
data{thisROI}{counter}.meanPix = [];
data{thisROI}{counter}.stdPix = [];
data{thisROI}{counter}.channel = [];
end
counter = 1;
if ~isempty(measureAroundChannels)
for thisMeasureAroundChannel = measureAroundChannels
intensityAroundVector = thisAroundROIIntensityShape{counter}{thisROI}(find(thisAroundROIIntensityShape{counter}{thisROI}));
dataAround{thisROI}{counter}.sumPix = sum(intensityAroundVector);
dataAround{thisROI}{counter}.numPix = length(intensityAroundVector);
dataAround{thisROI}{counter}.meanPix = dataAround{thisROI}{counter}.sumPix / dataAround{thisROI}{counter}.numPix;
dataAround{thisROI}{counter}.stdPix = std(intensityAroundVector);
dataAround{thisROI}{counter}.channel = thisMeasureAroundChannel;
counter = counter + 1;
end
else
dataAround{thisROI}{counter}.sumPix = [];
dataAround{thisROI}{counter}.numPix = [];
dataAround{thisROI}{counter}.meanPix = [];
dataAround{thisROI}{counter}.stdPix = [];
dataAround{thisROI}{counter}.channel = [];
end
% else
% counter = 1;
% for thisMeasureChannel = measureChannels(2:end)
% intensityVector = reshape(thisROIIntensityShape{thisMeasureChannel}{thisROI},1,[]);
% data{thisROI}{counter}.sumPix = sum(intensityVector);
% data{thisROI}{counter}.numPix = length(find(intensityVector));
% data{thisROI}{counter}.meanPix = data{thisROI}{thisMeasureChannel}.sumPix / data{thisROI}{thisMeasureChannel}.numPix;
% data{thisROI}{counter}.stdPix = std(intensityVector);
% counter = counter + 1;
% end
% end
end
fullMaskImg = fullMaskImg.*255;
clear('patchMasks');
clear('intensityVector');
clear('partMaskImg');
clear('thisROIIntensityShape');
end