-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCell_area_convex.m
More file actions
194 lines (163 loc) · 4.9 KB
/
Cell_area_convex.m
File metadata and controls
194 lines (163 loc) · 4.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
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
function [major, minor, area, segout, u, I0, seg, boundary]=Cell_area_convex(fname, K, convex, t1, t2)
cmin=0;
cmax=255;
epsilon=0.01;
%% Read in the image and standardize into [0, 255] interval
addpath InsidePolyFolder
info = imfinfo(fname);
num_images = numel(info);
for k = 1:num_images
A(:,:,k) = imread(fname, k);
end
target=A(:,:,K);
interval=linspace(double(quantile(target(:),0)), double(quantile(target(:),1-epsilon)), 256);
[bincounts,ind]= histc(double(target), interval);
ind=ind+255*(double(target)>quantile(target(:),1-epsilon));
I=uint8(ind-1);
I0=I;
% % May Enhance contrast using histogram equalization;
% I=histeq(I0,100);
u=1./info(1,1).XResolution; % length of a single pixel
figure;
imshow(I0), title('original image');
% text(size(I0,2),size(I0,1)+15, ...
% 'Image of egg chamber', ...
% 'FontSize',7,'HorizontalAlignment','right');
% text(size(I0,2),size(I0,1)+45, ....
% 'Florida State University, Jia', ...
% 'FontSize',7,'HorizontalAlignment','right');
caxis([cmin, cmax])
% apply the first threshold
if(~exist('t1','var'))
level1=graythresh(I);
original=im2bw(I, 0.2*level1);
else
original = (I>t1);
end
% apply the average filter to get rid of large area of noise
wsize1=floor(size(I,1)./20);
wsize2=floor(size(I,2)./20);
h = fspecial('average', [wsize1,wsize2]);
original1=filter2(h, original);
% apply the second threshold, expect find the large component and several far away component
if(~exist('t2','var'))
level2=graythresh(original1);
original2=im2bw(original1, 1*level2);
else
original2 = (original1>t2);
end
se = strel('disk',max(2, floor(size(I,1)./400)));
afterOpening = imopen(original2,se);
figure;
subplot(2,2,1)
imshow(original)
title('Apply first filter')
%figure;
subplot(2,2,2)
imshow(original1)
title('Apply average filter')
%figure;
subplot(2,2,3)
imshow(original2)
title('Apply second filter')
%figure
subplot(2,2,4)
imshow(afterOpening)
title('Break out components')
%% find the largest components
stats = regionprops(afterOpening, 'Area','ConvexHull','FilledArea','MajorAxisLength',...
'MinorAxisLength', 'Orientation', 'Eccentricity','PixelIdxList');
AA=struct2cell(stats);
area=zeros(1,size(AA,2));
for i=1:numel(area)
area(i)=AA{1,i};
end
[trash, index]=max(area);
index_large=AA{8, index};
if(~exist('convex','var'))
convex=0;
end
if convex==1
convex_index=AA{6,index};
[inner(:,2),inner(:,1)]=find(I0>=0);
xv = [convex_index(:,1);convex_index(1,1)]; yv = [convex_index(:,2);convex_index(2,2)];
IN = insidepoly(inner(:,1),inner(:,2), xv,yv);
I(IN)=50;
I(~IN)=0;
outline = bwperim(I);
segout = I;
segout(outline) = 255;
I0(segout==0)=0;
I11=I0;
I11(outline)=255;
% figure(100);
% imshow(I11)
[coeff,score,latent] = princomp(inner(IN,:));
area=numel(find(IN==1))*(u)^2;
minor=sqrt(4*area./pi./sqrt(latent(1)./latent(2)));
major=sqrt(4*area./pi./sqrt(latent(2)./latent(1)));
%fprintf('This cell has CONVEX size %4.1f by %4.1f with area %4.1f\n',major,minor,area)
else
I(index_large)=50;
I(setdiff(1:numel(I),index_large))=0;
final=imfill(I,'holes');
outline = bwperim(final);
segout = final;
segout(outline) = 255;
I0(segout==0)=0;
%figure, imshow(I0), hold on; h=imshow(segout); set(h, 'AlphaData', 0.5); title('outlined original image');
I11=I0;
I11(outline)=255;
% figure;
% imshow(I11)
major=AA{2,index}*u;
minor=AA{3,index}*u;
area=AA{7,index}*(u)^2;
%fprintf('This cell has size %4.1f by %4.1f with area %4.1f\n',major,minor,area)
end
%% second round
addpath('chanvese')
% chenvese segmentation on the cleaned image
t3=3e-10;
%seg = chenvese(imadjust(histeq(I0,256)),'whole+small',400,t3,'chan');
seg = chenvese(imadjust(I0),'whole+small',1000,t3,'chan');
if numel(find(seg==1))>numel(find(seg==0))
seg=~seg;
end
figure;
imshow(seg)
title('segmentation mask')
seg = bwareaopen(seg, floor(0.0001*area./(u^2)));
figure;
imshow(seg)
title('segmentation mask minus small parts')
clear inner
[inner1(:,2),inner1(:,1)]=find(seg>=0);
[inner(:,2),inner(:,1)]=find(seg>0);
KK = convhull(inner(:,1),inner(:,2));
figure
imshow(seg)
hold on
plot(inner(KK,1),inner(KK,2),'r-','linewidth',3)
title('segmentation mask with boundary (red)')
boundary=inner(KK,:);
xv = [inner(KK,1) ; inner(KK(1),1)]; yv = [inner(KK,2) ; inner(KK(1),2)];
IN = insidepoly(inner1(:,1),inner1(:,2), xv,yv);
I1=I0;
I1(IN)=50;
I1(~IN)=0;
outline = bwperim(I1);
segout = I1;
segout(outline) = 255;
I0(segout==0)=0;
I11=I0;
I11(outline)=255;
figure (200);
imshow(I11)
title('segmentation in original intensity')
[coeff,score,latent] = princomp(inner1(IN,:));
area=numel(find(IN==1))*(u)^2;
minor=sqrt(4*area./pi./sqrt(latent(1)./latent(2)));
major=sqrt(4*area./pi./sqrt(latent(2)./latent(1)));
fprintf('This cell has CHAN CONVEX size %4.1f by %4.1f with area %4.1f um^2\n',major,minor,area)
fprintf('This cell has ratio %4.2f \n', major./minor)