Diff of /gimmeSegs_prostate2.m [000000] .. [be1edc]

Switch to unified view

a b/gimmeSegs_prostate2.m
1
function [lumen,epithelium_cells,epithelium,stroma] = gimmeSegs_prostate2(image,showMe,saveSegs,noDecon)
2
%% Prostate Adaptation of gimmeSegs
3
4
%% Purpose:  
5
% gimmeSegs modification for use with prostate slides
6
%
7
%% Inputs:
8
% image:  image being segmented
9
% showMe: enables figure of outputs
10
% saveSegs: saves outputs to .mat variables
11
% noDecon: use if not using Color Deconvolution
12
%
13
%% Outputs:
14
% Segmented lumen, epithelial cells, epithelium, stroma, and columnar cells
15
16
%% ------------------------------------------------------------------------
17
if ~exist('showMe','var')
18
    showMe = 0;
19
end
20
21
if ~exist('saveSegs','var')
22
    saveSegs = 0;
23
end
24
25
if ~exist('noDecon','var')
26
    noDecon = 0;
27
end
28
29
rrat = 0.293; % do not know what these are / ask Sam
30
brat = 0.5078;
31
32
disp('1. Segmenting Lumen')
33
tic
34
35
% lumen = imbinarize(image(:,:,2));
36
lumen = image(:,:,2)>190;
37
lumen = uint8(lumen);
38
39
toc
40
% -------------------------------------------------------------------------
41
disp('2. Segmenting Epithelium')
42
43
if noDecon == 1
44
    
45
    whatsLeft = image .* repmat(uint8(~lumen),[1,1,3]);
46
    whatsLeft = whatsLeft .* repmat(uint8(~vessel),[1,1,3]);
47
    
48
    red = whatsLeft(:,:,1);
49
    blue = whatsLeft(:,:,3);
50
    stroma = (red + blue) ./ 2;
51
    purp = ((red.*rrat) + (blue.*brat))./2;
52
    p2p = stroma - purp;
53
    
54
    nuc = uint8(zeros(size(p2p)));
55
    nuc(p2p>60) = 1;
56
    
57
    se = strel('disk',2);%nuc2 = imclose(nuc,se);
58
    nuc2 = imfill(nuc);
59
    cc = bwconncomp(nuc2);
60
    epithelium = uint8(zeros(size(nuc2)));
61
    x = regionprops('table',cc,'MajoraxisLength','MinoraxisLength','Circularity','Area');
62
    
63
    for i = 1:length(cc.PixelIdxList)
64
        if x.Circularity(i) > 0.1 && x.Area(i) > 1
65
            epithelium(cc.PixelIdxList{i}) = 1;
66
        end
67
    end
68
else
69
    % set of standard values for stain vectors (from python scikit)
70
    He = [0.6443186; 0.7166757; 0.26688856];
71
    Eo = [0.09283128; 0.9545457; 0.28324];
72
    Res = [ 0.63595444;   0.001; 0.7717266 ]; %residual
73
    
74
    HDABtoRGB = [He/norm(He) Eo/norm(Eo) Res/norm(Res)]';
75
    RGBtoHDAB = inv(HDABtoRGB);
76
    
77
    tic
78
    imageHDAB = SeparateStains(image, RGBtoHDAB);
79
    toc
80
    
81
    epithelium_cells = imcomplement(imageHDAB(:,:,1))>0.3; % >0.5
82
   
83
    thresh = quantile(unique(imcomplement(imageHDAB(:,:,1))),0.3);
84
    thresh2 = quantile(unique(imageHDAB(:,:,2)),0.6);
85
    epithelium = (imcomplement(imageHDAB(:,:,1))>(thresh+0.1)) + (imageHDAB(:,:,2)>thresh2) & ~lumen; %0.5 %0.8
86
    
87
%     epithelium2 = imbinarize(imcomplement(image(:,:,1)));
88
%     epithelium = (epithelium + epithelium2) & ~lumen;
89
    epithelium = uint8(epithelium);
90
    
91
%     figure('Position',[100 2000, 1500, 900]);
92
% subplot(221); imagesc(image); axis image
93
% subplot(222); imagesc(imoverlay(image,epithelium)); axis image
94
95
end
96
97
% -------------------------------------------------------------------------
98
disp('3. Segmenting Stroma')
99
100
tic
101
102
% stroma = imbinarize(imageHDAB(:,:,3));
103
thresh = quantile(unique(imcomplement(imageHDAB(:,:,2))),0.3); % find the 0.3 quantile to segment between epithelium and stroma
104
stroma = imcomplement(imageHDAB(:,:,2))>(thresh+0.1) & ~lumen; %0.4
105
% stroma2 = imageHDAB(:,:,1)>0.9; %0.8
106
% stroma = (stroma + stroma2) & ~lumen;
107
stroma = uint8(stroma);
108
109
110
%     figure('Position',[100 2000, 1500, 900]);
111
% subplot(221); imagesc(image); axis image
112
% subplot(222); imagesc(imoverlay(image,stroma)); axis image
113
toc
114
115
disp('Segmentation Complete');
116
if showMe == 1
117
    
118
    figure;
119
    subplot(141); imagesc(image); title('orig'); axis image
120
    subplot(142); imagesc(lumen); title('lumen'); axis image
121
    subplot(143); imagesc(epithelium); title('epithelium'); axis image
122
    subplot(144); imagesc(stroma); title('stroma'); axis image
123
    set(gcf,'Position',[100,100,1600,600]); axis image
124
    
125
end
126
127
if saveSegs == 1
128
    
129
    disp('Writing segmentations')
130
    
131
    save('lumen','lumen');
132
    save('epithelium','epithelium');
133
    save('stroma','stroma');
134
    
135
end