-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinnercontour_seg.m
342 lines (271 loc) · 12.7 KB
/
innercontour_seg.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
%% function for the segmentation of the inner chest contour (vertebral body included)
% Method: the algorithm goes through the outer curvature in clockwise direction
%until the start point is found again. While going through the outer curvature,
%the number of steps is counted. Every 12 steps the actual point and the point
%12 steps before are connected and a perpendicular line in the mid-point
%is generated. Then the algorithm has to find the intersection point between
%the perpendicular line and the first point crossed by it on the two lungs.
%If the perpendicular does not meet the lungs the point is located at the
%same distance as the previous point.
%inputs:
%- I_imadjust: grey-scale image
%- BWlung: binary image representing segmented lungs
%- contour: coordinates of outer chest contour
%- xhalf: x position of the point located in the half of the image
%- lung1: coordinates of pixels releted to the right lung
%- lung2: coordinates of pixels releted to the left lung
%outputs:
%-inters: points representing the inner chest contour
function [inters] = innercontour_seg(I_imadjust,BWlung,contour,xhalf,lung1,lung2)
% figure
% imshow(BWlung)
nsteps=12;
%number of intersection points to find (calculated from the number of outer
%chest contour points and step number)
nrow=round((max(size(contour))-nsteps)/nsteps);
%initialization
%middle point
midX=zeros(nrow,1);
midY=zeros(nrow,1);
%intersection points
inters=zeros(nrow,2);
%distance between mean point and intersection point
distancemidint=zeros(nrow,1);
%mean distances
distancem=zeros(nrow,1);
%varianza delle distanze
stdm=zeros(nrow,1);
%counter
a=0;
%partition of the image in 4 quarters: image partitioned in a right and
%left half (indices: ic2 and ic4)
icx=find(contour(:,1)==xhalf);
ic2=icx(1,1);
ic4=icx(2,1);
% image partitioned in a upper and lower half (indices: ic1 and ic3)
%index on outer contour corresponding to the first point:
ic1=find(contour(1,1));
%indices on outer contour at the same y as the first point
ic3x=find(contour(:,2)==contour(1,2));
%elimination of consecutive points at the same y
ic3delete=find(diff(ic3x)==1);
ic3x(ic3delete+1)=[];
% index corresponding to the point with greater x:
ic3=ic3x(2,1);
%algorithm goes through the entire outer chest contour
for i=1:nsteps:max(size(contour))-nsteps
a=a+1;
%starter and end point of each segment
p1=contour(i,:);
p2=contour(i+nsteps,:);
% %segment between the 2 points
% hold on
% plot([p1(1) p2(1)],[p1(2) p2(2)],'y-')
% hold on
%middle point of segment
midX(a,1) = round(mean([p1(1), p2(1)]));
midY(a,1) = round(mean([p1(2), p2(2)]));
%if the 2 points are at the same y, perpendicular line is: x=midX
if p2(2)==p1(2)
% vector with the same pixel number of the image
y = linspace(1,size(I_imadjust,1),size(I_imadjust,2));
%perpendicular line
x=midX(a,1).*ones(1,size(I_imadjust,2));
%slope assigned to 'a'
slope='a';
%if the 2 points are at the same x, perpendicular line is: y=midY
elseif p2(1)==p1(1)
x = linspace(1,size(I_imadjust,1),size(I_imadjust,2));
y=midY(a,1).*ones(1,size(I_imadjust,2));
%slope assigned to '0'
slope=0;
%otherwise segment slope is computed
else
slope = (p2(2)-p1(2)) / (p2(1)-p1(1));
% perpendicular line slope
slope = -1/slope;
% x vector with the same pixel number of the image
x = round(linspace(1,size(I_imadjust,1),size(I_imadjust,2)));
% y corresponding to x coordinates calculated from equation
% (perpendicular line passing through middle point of the
% segment)
y = round(slope * (x - midX(a,1)) + midY(a,1));
%elimination of y values outside the image (and corresponding x values)
idelete=find(y>size(I_imadjust,1) | y<=0);
y(idelete)=[];
x(idelete)=[];
end
% %perpendicular line
% plot(x,y, 'b-');
% hold on
%matrix containing x and y coordinates of perpendicular line points
mat=[x' y'];
%% application of algorithm with different conditions based on the quadrant analyzed
%first quadrant: right lung considered (outer contour upper border):
%if the slope of the perpendicular increases (> 0) the intersection point
% is the first one(that with the smallest y), if the slope is negative the point is
% the last one(that with the lowest y), if the slope is 0 (parallel to the y axis)
% and if the slope does not exist (parallel to the x axis) the point of
% intersection is the first one.
if i>=ic1 && i<=ic2
%common points between perpendicular line and right lung region
[v,~,ib] = intersect(lung1,mat,'rows');
%if there are common points
if isempty(ib)==0
%if the slope is negative, the y of the intersection points
% decrease (y (end) <y (2)) so the order of the indices is
%reversed because I want the point with lower y coordinate
%that is the last one
if (v(1,2)>v(end,2))
ib=sort(ib,'descend');
end
%otherwise the intersection point is the first one
inters(a,:)=mat(ib(1),:);
end
%second quadrant: left lung considered (outer contour upper border):
%same method as for the first quadrant except in the case of
%slope=0 (y = cost) where I want the last point
elseif i>=ic2 && i<=ic3
[v,~,ib] = intersect(lung2,mat,'rows');
if isempty(ib)==0
%if slope<0 or y=cost the order of the indices is
%reversed because I want the point with lower y coordinate
%that is the last one
if (v(1,2)>=v(end,2))
ib=sort(ib,'descend');
end
%otherwise the intersection point is the first one
inters(a,:)=mat(ib(1),:);
end
%third quarter: left lung considered (outer countour lower border):
%if the slope of the perpendicular increases (> 0) the intersection point
% is the last one (that with the greatest y), if the slope is negative (<0)
%the point is the first one (that with the greatest y), if the slope is 0
%(parallel to the y axis) and if the slope does not exist (parallel to the x axis)
%the intersection point is the last one.
elseif i>=ic3 && i<=ic4
[v,~,ib] = intersect(lung2,mat,'rows');
if isempty(ib)==0
%if slope<0 the order of the indices is reversed because
%I want the first point
if (v(1,2)>v(end,2))
ib=sort(ib,'descend');
end
%intersection point is the last one
inters(a,:)=mat(ib(end),:);
end
%fourth quadrant: rightlung considered (outer contour lower border):
%same method as for the third quadrant except in the case of
%slope=0 (y = cost) where I want the first point
else
[v,~,ib] = intersect(lung1,mat,'rows');
if isempty(ib)==0
%if slope<0 or y=cost the order of the indices is reversed
%because I want the first point
if (v(1,2)>=v(end,2))
ib=sort(ib,'descend');
end
%intersection point is the last one
inters(a,:)=mat(ib(end),:);
end
end
%% cases where the perpendicular line doesn't cross the lungs
mid=[midX, midY];
%distance between middle point and intersection point
distancemidint(a)=sqrt((mid(a,1)-inters(a,1)).^2+(mid(a,2)-inters(a,2)).^2);
%mean value
distancem(a)=mean(distancemidint(1:a));
%standard deviation
stdm(a)=std(distancemidint(1:a));
%analysis beging to the second intersection point
if a>1
%if the distance between the midpoint and the intersection point is
%greater than 2*mean distance-standard deviation (threshold)
if distancemidint(a)>=(2*floor(distancem(a)))-floor(stdm(a))
%distance replaced by the previous one
distancemidint(a)=distancemidint(a-1);
%mean value recomputed
distancem(a)=mean(distancemidint(1:a));
%distance between the midpoint and perpendicular line points
distmatmid=round(sqrt((mid(a,1)-mat(:,1)).^2+(mid(a,2)-mat(:,2)).^2));
%midpoint index
imid=find(distmatmid==0);
slope=round(slope);
%analysis diversified based on the quadrant
%first quadrant
if i>=ic1 && i<=ic2
if slope<0
%elimination of the points after the mid point
%(the last ones: y decreases)
distmatmid(imid+1:end)=[];
mat(imid+1:end,:)=[];
%if slope >0, x=cost(slope=a) and y=cost(slope=0)
else
%elimination of the points before the mid point
%(the first ones: y increases)
distmatmid(1:imid-1)=[];
mat(1:imid-1,:)=[];
end
%second quadrant
elseif i>ic2 && i<=ic3
if slope<=0
%elimination of the points after the mid point
%(the last ones: y decreases)
distmatmid(imid+1:end)=[];
mat(imid+1:end,:)=[];
%if slope >0, x=cost(slope=a)
else
%elimination of the points before the mid point
%(the first ones: y increases)
distmatmid(1:imid-1)=[];
mat(1:imid-1,:)=[];
end
%third quadrant
elseif i>ic3 && i<=ic4
%elimination of the points before the mid point
%(the first ones: y decreases)
if slope<0
distmatmid(1:imid-1)=[];
mat(1:imid-1,:)=[];
%slope >0, x=cost(slope=a) and y=cost(slope=0)
else
%elimination of the points after the mid point
%(the last ones: y increases)
distmatmid(imid+1:end)=[];
mat(imid+1:end,:)=[];
end
%fourth quadrant
else
if slope<=0
%elimination of the points before the mid point
%(the first ones: y decreases)
distmatmid(1:imid-1)=[];
mat(1:imid-1,:)=[];
%slope >0, x=cost(slope=a)
else
%elimination of the points after the mid point
%(the last ones: y increases)
distmatmid(imid+1:end)=[];
mat(imid+1:end,:)=[];
end
end
% index of the point closer to the wanted distance
[~,iinters]=min(abs(distmatmid-distancemidint(a)));
if isempty(iinters)==0
inters(a,:)=mat(iinters,:);
end
end
%if intersection point is zero: error in the computation
if ~any(inters(a,:))
%point replaced by a point at the same distance as the
%previous one
distancemidint(a)=distancemidint(a-1);
distancem(a)=distancem(a-1);
end
end
% hold on
% plot(inters(a,1),inters(a,2),'r.');
%
% hold off
end
end