[1422d3]: / functions / functions_preProc / segmentStain.m

Download this file

172 lines (123 with data), 4.4 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
 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
function [maskACClopeOpen] = segmentStain(im, imColorNorm, filename, dirResults, params, plotta, savefile)
%------------------------------------
%otsu (intensity) on E
if size(imColorNorm, 3) == 3
imColorNorm_gray = rgb2gray(imColorNorm);
end %if size
th = graythresh(imColorNorm_gray) + params.offsetOtsuIntens;
%thresh = imColorNorm_gray < th*255;
thresh = imColorNorm_gray < th;
threshOne = connComp2(thresh, 1, 1);
maskThresh = threshOne;
% figure
% imshow(maskThresh)
% pause
%------------------------------------
%fuzzy c-means on E
%imAlt = im2uint8(rgb2lab(im));
imAlt = im2uint8(rgb2hsv(im));
[C, U, LUT, H] = FastFCMeans(imAlt, params.nClusters);
%analyze centers, discard greatest one (white background)
% [C_sort, indC] = sort(C, 'ascend');
[C_sort, indC] = sort(C, 'descend'); %for hsv
C_select = indC(1 : params.classesToConsider);
% %memberships to maps
L = LUT2label(imAlt, LUT);
Umap = FM2map(imAlt, U, H);
%memberships
if plotta
fh1 = figure;
subplot(1,3,1)
imshow(rgb2gray(Umap(:,:,:,1)),[]);
subplot(1,3,2)
imshow(rgb2gray(Umap(:,:,:,2)),[]);
subplot(1,3,3)
imshow(rgb2gray(Umap(:,:,:,3)),[]);
str = [filename '; class memberships'];
mtit(fh1, str, 'Interpreter', 'none', 'fontsize', 14, 'color', [1 0 0], 'xoff', .0, 'yoff', .0);
if savefile
export_fig(gcf, [dirResults str '.jpg']);
end %if save
end %if plotta
%[C, U, LUT, H] = FastFCMeans(im, params.nClusters);
%analyze centers, discard greatest one (white background)
% [C_sort, indC] = sort(C, 'ascend');
% C_select = indC(1 : params.classesToConsider);
% %memberships to maps
% L = LUT2label(im, LUT);
% Umap = FM2map(im, U, H);
%select
maskFuzzy = false([size(L, 1) size(L,2)]);
classThresholded = cell(numel(C_select), 1);
for c = 1 : numel(C_select)
class = rgb2gray(Umap(:,:,:,C_select(c)));
class = imadjust(class);
%for one of the classes, modify the threshold
%if c == 2
threshClass = graythresh(class) + params.offsetOtsuFCM;
%else %if c
%threshClass = graythresh(class);
%end %if c
classThresholded{c} = class > threshClass;
%threshClass = multithresh(class, params.numberMultiThresh) - params.offsetOtsuFCM;
%classThresholded{c} = class > threshClass(2);
maskFuzzy = logical(maskFuzzy + classThresholded{c});
end %for c
maskFuzzy = connComp2(maskFuzzy, 1, 1);
%display
if plotta
fh2 = figure;
for c = 1 : numel(C_select)
subplot(1, numel(C_select), c)
imshow(classThresholded{c})
end %for c
str = [filename '; class memberships, segmentation'];
mtit(fh2, str, 'Interpreter', 'none', 'fontsize', 14, 'color', [1 0 0], 'xoff', .0, 'yoff', .0);
if savefile
export_fig(gcf, [dirResults str '.jpg']);
end %if save
end %if plotta
%------------------------------------
%combine
% mask = logical(maskThresh .* maskFuzzy);
mask = logical(maskThresh + maskFuzzy);
%morph
se = strel(params.typeSeSegm, params.sizeSeSegm);
maskFill = imfill(mask, 'holes');
maskClose = imclose(maskFill, se);
maskCloseOpen = imopen(maskClose, se);
%------------------------------------
%refine with active contour
maskAC = activecontour(im, maskCloseOpen, params.numIterAC, 'Chan-Vese', 'ContractionBias', -0.3);
%morph
maskAC_oneCC = connComp2(maskAC, 1, 1);
maskACFill = imfill(maskAC_oneCC, 'holes');
maskACClose = imclose(maskACFill, se);
maskACClopeOpen = imopen(maskACClose, se);
%------------------------------------
if plotta
fh = fsfigure;
subplot(2,3,1)
imshow(im)
title('Original')
subplot(2,3,2)
imshow(im2double(im) + edge(maskThresh), []);
title('Mask Otsu');
subplot(2,3,3)
imshow(im2double(im) + edge(maskFuzzy), []);
title('Mask Fuzzy C-Means');
subplot(2,3,4)
imshow(im2double(im) + edge(maskCloseOpen), []);
title('Mask combined + morph');
subplot(2,3,5)
imshow(im2double(im) + edge(maskAC), []);
title('Mask combined + morph + AC');
subplot(2,3,6)
imshow(im2double(im) + edge(maskACClopeOpen), []);
title('Mask combined + morph + AC + morph');
str = [filename '; segmentation'];
mtit(fh, str, 'Interpreter', 'none', 'fontsize', 14, 'color', [1 0 0], 'xoff', .0, 'yoff', .04);
if savefile
export_fig(gcf, [dirResults str '.jpg']);
end %if save
end %if plotta