-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectCTCs.m
150 lines (129 loc) · 4.37 KB
/
detectCTCs.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
function detectCTCs(I1,I2,I3,x,y,ind,pos)
s = 1;
strg=sprintf('%%.%dd',s);
indxStr=sprintf(strg,ind);
[aux, I2w] = spotDetector(double(I2));
%
% figure,imshow(I2w,[])
% hold on
% plot(x,y,'g*')
% title('Nucleus WAVELET (w manual selection in green asterik)')
Ir = I1./I3;
% figure,imshow(Ir,[])
% hold on
% plot(x,y,'g*')
% title('Ratio CK/CD45 (w manual selection in green asterik)')
Inew = Ir.*I2w; % WHY MULTIPLIED
% figure, imshow(Inew,[])
% hold on
% plot(x,y,'g*')
% title('Ratio times Wavelet DAPI (w manual selection in green asterik)')
COEF1 = 7;
COEF2 = 20;
COEF3 = 0.3;% 0.6 is perfect for IMAGE 4 / 0.3 for images 3,4,5
% % retain only pixel intensities which are similar
[cutoffInd, cutoffV] = cutFirstHistMode(Inew,0);
Icut = Inew>cutoffV*COEF1; % REMOVE THE NOISE FEATURES %no 3
% figure,imshow(Icut,[])
% hold on
% plot(x,y,'g*')
% title('Unimodal hist (w manual selection in green asterik)')
X = bwlabel(Icut);
stats1 = regionprops(X,'all');
% cut areas by size
Ar = [stats1.Area];
% figure,hist(Ar);
[In_Ar,Cut_Ar]=cutFirstHistMode(Ar,0);% switch to 1 to see HIST
goodAr = find(Ar>(Cut_Ar*COEF3)); % SPOTS WHICH are big enough
stats = stats1(goodAr);
h = figure,imshow(I1,[])
hold on
for i = 1:length(stats)
plot(stats(i).Centroid(1),stats(i).Centroid(2),'rs')
text(stats(i).Centroid(1)+2,stats(i).Centroid(2)+2,[num2str(i)],'Color','r');
end
plot(x,y,'g*')
title(['POS ',pos,', CK channel: automated selection in red squares, manual selection in green asteriks'])
% saveas(h,(['CTCs_',indxStr,'.fig']),'fig');
% close
% Initialize 'feats' structure
% feats=struct(...
% 'pos',[0 0],...
% 'ecc',0,...
% 'ori',0);
%
% for j = 1:length(stats)
% feats.pos(j,1) = stats(j).Centroid(1);
% feats.pos(j,2) = stats(j).Centroid(2);
% feats.ecc(j,1) = stats(j).Eccentricity;
% feats.ori(j,1) = stats(j).Orientation;
% feats.len(j,1) = stats(j).MajorAxisLength;
%
% e1 = [-cos(stats(j).Orientation*pi/180) sin(stats(j).Orientation*pi/180) 0];
% e2 = [sin(stats(j).Orientation*pi/180) cos(stats(j).Orientation*pi/180) 0];
% e3 = [0 0 1];
% Ori = [stats(j).Centroid 0];
% v1 = [-10 10];
% v2 = [-5 5];
% v3 = [0 0];
% [xGrid,yGrid]=arbitraryGrid(e1,e2,e3,Ori,v1,v2,v3);
% Crop(:,:,j) = interp2(I1,xGrid,yGrid); % READ FROM RAW DATA CYTOKERATIN
% e1 = [];e2 = [];e3 = []; Ori = []; v1 = []; v2 = []; xGrid = []; yGrid = [];
% end
%
% Cm = nanmean(Crop,3); % MEAN/REPRESENTATIVE CTC CROP
% Crop(isnan(Crop))=0;% border effect - some NaN
% Cm1 = bwlabel(Cm);
% statsC = regionprops(Cm1,'all');
%
% B = Cm(:); % MEAN CTC
% A = ones(length(B),2);
%
% for m = 1:size(Crop,3)
% CR = Crop(:,:,m);
% A(:,2) = CR(:); % INDIVIDUAL CTC
% goodRows = find(A(:,2) ~= 0 & isfinite(B));
% XX = lscov(A(goodRows,:),B(goodRows));
% RES = B(goodRows) - A(goodRows,:)*XX;
% OUT(m,:) = [mean(RES(:).^2),XX'];
% end
%
% [Ind,V]=cutFirstHistMode(OUT(:,1),0);% switch to 1 to see HIST
%
% goodFeats = find(OUT(:,1)<(V*COEF2)); % SPOTS WHICH FIT WELL WITH THE MEAN CTC SPOT
%
% featNames = fieldnames(feats);
%
% for field = 1:length(featNames)
% feats.(featNames{field}) = feats.(featNames{field})(goodFeats,:);
% end
aaux = 0;%5
h=figure,
If=I3;
imshow(If(1+aaux:end-aaux,1+aaux:end-aaux),[]);
title(['POS ',pos,', CD45 channel: automated selection in red squares, manual selection in green asteriks'])
hold on
for i = 1:length(stats)
plot(stats(i).Centroid(1),stats(i).Centroid(2),'rs')
text(stats(i).Centroid(1)+2,stats(i).Centroid(2)+2,[num2str(i)],'Color','r');
end
hold on
plot(x-aaux,y-aaux,'g*')
% saveas(h,(['Leukocytes_',indxStr,'.fig']),'fig');
% close
%-----------------------
h = figure,imshow(I2,[])
hold on
for i = 1:length(stats)
plot(stats(i).Centroid(1),stats(i).Centroid(2),'rs')
text(stats(i).Centroid(1)+2,stats(i).Centroid(2)+2,[num2str(i)],'Color','r');
end
plot(x,y,'g*')
title(['POS ',pos,', Nucleus (w manual selection in green asterik)'])
% saveas(h,(['Nucleus_',indxStr,'.fig']),'fig');
% close
%------------------------------------------------
% get centroids and plot together w handclicked
% get centroids and go back to original image to obtain connected component
% labeling properties
%