[be1edc]: / gimmeSegs_prostate2.m

Download this file

135 lines (102 with data), 3.9 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
function [lumen,epithelium_cells,epithelium,stroma] = gimmeSegs_prostate2(image,showMe,saveSegs,noDecon)
%% Prostate Adaptation of gimmeSegs
%% Purpose:
% gimmeSegs modification for use with prostate slides
%
%% Inputs:
% image: image being segmented
% showMe: enables figure of outputs
% saveSegs: saves outputs to .mat variables
% noDecon: use if not using Color Deconvolution
%
%% Outputs:
% Segmented lumen, epithelial cells, epithelium, stroma, and columnar cells
%% ------------------------------------------------------------------------
if ~exist('showMe','var')
showMe = 0;
end
if ~exist('saveSegs','var')
saveSegs = 0;
end
if ~exist('noDecon','var')
noDecon = 0;
end
rrat = 0.293; % do not know what these are / ask Sam
brat = 0.5078;
disp('1. Segmenting Lumen')
tic
% lumen = imbinarize(image(:,:,2));
lumen = image(:,:,2)>190;
lumen = uint8(lumen);
toc
% -------------------------------------------------------------------------
disp('2. Segmenting Epithelium')
if noDecon == 1
whatsLeft = image .* repmat(uint8(~lumen),[1,1,3]);
whatsLeft = whatsLeft .* repmat(uint8(~vessel),[1,1,3]);
red = whatsLeft(:,:,1);
blue = whatsLeft(:,:,3);
stroma = (red + blue) ./ 2;
purp = ((red.*rrat) + (blue.*brat))./2;
p2p = stroma - purp;
nuc = uint8(zeros(size(p2p)));
nuc(p2p>60) = 1;
se = strel('disk',2);%nuc2 = imclose(nuc,se);
nuc2 = imfill(nuc);
cc = bwconncomp(nuc2);
epithelium = uint8(zeros(size(nuc2)));
x = regionprops('table',cc,'MajoraxisLength','MinoraxisLength','Circularity','Area');
for i = 1:length(cc.PixelIdxList)
if x.Circularity(i) > 0.1 && x.Area(i) > 1
epithelium(cc.PixelIdxList{i}) = 1;
end
end
else
% set of standard values for stain vectors (from python scikit)
He = [0.6443186; 0.7166757; 0.26688856];
Eo = [0.09283128; 0.9545457; 0.28324];
Res = [ 0.63595444; 0.001; 0.7717266 ]; %residual
HDABtoRGB = [He/norm(He) Eo/norm(Eo) Res/norm(Res)]';
RGBtoHDAB = inv(HDABtoRGB);
tic
imageHDAB = SeparateStains(image, RGBtoHDAB);
toc
epithelium_cells = imcomplement(imageHDAB(:,:,1))>0.3; % >0.5
thresh = quantile(unique(imcomplement(imageHDAB(:,:,1))),0.3);
thresh2 = quantile(unique(imageHDAB(:,:,2)),0.6);
epithelium = (imcomplement(imageHDAB(:,:,1))>(thresh+0.1)) + (imageHDAB(:,:,2)>thresh2) & ~lumen; %0.5 %0.8
% epithelium2 = imbinarize(imcomplement(image(:,:,1)));
% epithelium = (epithelium + epithelium2) & ~lumen;
epithelium = uint8(epithelium);
% figure('Position',[100 2000, 1500, 900]);
% subplot(221); imagesc(image); axis image
% subplot(222); imagesc(imoverlay(image,epithelium)); axis image
end
% -------------------------------------------------------------------------
disp('3. Segmenting Stroma')
tic
% stroma = imbinarize(imageHDAB(:,:,3));
thresh = quantile(unique(imcomplement(imageHDAB(:,:,2))),0.3); % find the 0.3 quantile to segment between epithelium and stroma
stroma = imcomplement(imageHDAB(:,:,2))>(thresh+0.1) & ~lumen; %0.4
% stroma2 = imageHDAB(:,:,1)>0.9; %0.8
% stroma = (stroma + stroma2) & ~lumen;
stroma = uint8(stroma);
% figure('Position',[100 2000, 1500, 900]);
% subplot(221); imagesc(image); axis image
% subplot(222); imagesc(imoverlay(image,stroma)); axis image
toc
disp('Segmentation Complete');
if showMe == 1
figure;
subplot(141); imagesc(image); title('orig'); axis image
subplot(142); imagesc(lumen); title('lumen'); axis image
subplot(143); imagesc(epithelium); title('epithelium'); axis image
subplot(144); imagesc(stroma); title('stroma'); axis image
set(gcf,'Position',[100,100,1600,600]); axis image
end
if saveSegs == 1
disp('Writing segmentations')
save('lumen','lumen');
save('epithelium','epithelium');
save('stroma','stroma');
end