-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEIT_EIDORS_Model_GREIT.m
336 lines (282 loc) · 14.1 KB
/
EIT_EIDORS_Model_GREIT.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
function EIT_EIDORS_Model_GREIT()
%% Make folder and set filePath
if ~exist('.\EIT_EIDORS_Model_GREIT','dir')
mkdir('.\EIT_EIDORS_Model_GREIT');
end
EIT_GREIT_Filepath = '.\EIT_EIDORS_Model_GREIT';
if ~exist('.\EIT_EIDORS_Model_GREIT_Voltage','dir')
mkdir('.\EIT_EIDORS_Model_GREIT_Voltage');
end
EIT_V_Filepath = '.\EIT_EIDORS_Model_GREIT_Voltage';
if ~exist('.\EIT_EIDORS_Model_GREIT_CP','dir')
mkdir('.\EIT_EIDORS_Model_GREIT_CP');
end
EIT_CP_Filepath = '.\EIT_EIDORS_Model_GREIT_CP';
%% Get Contours
thorax = shape_library('get','adult_male','boundary');
rlung = shape_library('get','adult_male','right_lung');
llung = shape_library('get','adult_male','left_lung');
% one could also run:
% shape_library('get','adult_male');
% to get all the info at once in a struct
% show the library image
% figure; shape_library('show','adult_male');
% print_convert thoraxmdl01a.jpg '-density 100'
shape = { 0, % height
{thorax, rlung, llung}, % contours
[4,50], % perform smoothing with 50 points
0.04}; % small maxh (fine mesh)
elec_pos = [ 16, % number of elecs per plane
1, % equidistant spacing
0]'; % a single z-plane
elec_shape = [0.05, % radius
0, % circular electrode
0.01 ]'; % maxh (electrode refinement)
fmdl = ng_mk_extruded_model(shape, elec_pos, elec_shape);
% this similar model is also available as:
% fmdl = mk_library_model('adult_male_16el_lungs');
% show_fem(fmdl,[0 1]);
fmdl.nodes(:,1) = fmdl.nodes(:,1) - 0.5*(max(fmdl.nodes(:,1)) + min(fmdl.nodes(:,1)));
fmdl.nodes(:,2) = fmdl.nodes(:,2) - 0.5*(max(fmdl.nodes(:,2)) + min(fmdl.nodes(:,2)));
% figure; show_fem(fmdl); axis tight off
[fmdl.stimulation,fmdl.meas_sel] = mk_stim_patterns(16,1,[0,1],[0,1],{'no_meas_current'}, 5);
img = mk_image(fmdl,1);
vh = fwd_solve(img);
vRef = vh.meas;
numElement = length(img.fwd_model.elems);
element = img.fwd_model.elems;
node = img.fwd_model.nodes;
% -- Save img at compareImg to calculate S
% In GREIT, use it for tempImg
SImg = img;
% -- Set initial lung conductivity
img.elem_data(fmdl.mat_idx{1}) = 1;
lungElem = [fmdl.mat_idx{2}; fmdl.mat_idx{3}]; % 2 : Right, 3 : Left
sigmaLen = 1000;
sigmaX = linspace(0,4*pi,sigmaLen);
amp = linspace(1,1.3,sigmaLen);
sigma = 0.1*amp.*sin(sigmaX)+0.25;
%% Making Inverse Model & Solver in Advance
opt.imgsz = [128 128];
opt.square_pixels = 1;
opt.Nsim = 500; % 500 hundred targets to train on, seems enough
opt.distr = 3; % non-random, uniform
opt.target_size = 0.02; %small targets
opt.target_offset = 0;
opt.noise_figure = 0.5; % this is key!
imdl = mk_GREIT_model(img, 0.2, [], opt);
imdl.fwd_model.meas_select = fmdl.meas_sel;
reconSolver = imdl.solve_use_matrix.RM;
%% Find Boundary
boundaryNode = unique(img.fwd_model.boundary(:));
element = img.fwd_model.elems;
boundaryElement = [];
for iter = 1:length(element)
for innerIter = 1:length(boundaryNode)
if element(iter,1) == boundaryNode(innerIter) || element(iter,2) ...
== boundaryNode(innerIter) || element(iter,3) == boundaryNode(innerIter)
boundaryElement = [boundaryElement; iter];
end
end
end
boundaryElement = sort(unique(boundaryElement));
%% Change triangle mesh to grid mesh
for i = 1:size(element,1)
xy(i,:) = mean(node(element(i,1:3),:));
end
nPixel = 128;
margin_FOV = 1.05;
meshsize = max(max(abs(node)))*margin_FOV;
ti = -meshsize:(2*meshsize)/(nPixel-1):meshsize;
[qx,qy] = meshgrid(ti,ti);
% -- No use area
% This is for training data
% img.elem_data(lungElem) = sigma(1);
% This is for showing data
% img.elem_data(lungElem) = sigma(350);
% vkRef = fwd_solve(img);
% vCRef = vkRef.meas;
% bodyShpae128 : makes outer body pixels zero (Need for post processing)
bodyShape = zeros(numElement,1);
bodyShape(boundaryElement) = 0.7;
bodyShape128 = scatteredInterpolant(xy(:,1),xy(:,2),bodyShape);
bodyShape128 = flipud(bodyShape128(qx,qy));
bodyShape128(find(bodyShape128>0)) = 1;
bodyShape128 = imcomplement(bodyShape128);
%% Change the collapse area
for collapseCase = 1:19%19:19
tempImg = SImg;
switch collapseCase
% Do nothing, which is normal lung
case 1
targetLungElem = lungElem;
collapseP = 0;
% Right lung collapse 5%
case 2
collapseArea = inline('((x+0.34)/0.32).^2 + ((y+0.49)/0.16).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Right lung collapse 10%
case 3
collapseArea = inline('((x+0.34)/0.32).^2 + ((y+0.39)/0.16).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Right lung collapse 15%
case 4
collapseArea = inline('((x+0.34)/0.32).^2 + ((y+0.3)/0.16).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Right lung collapse 20%
case 5
collapseArea = inline('((x+0.34)/0.32).^2 + ((y+0.25)/0.2).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Right lung collapse 25%
case 6
collapseArea = inline('((x+0.34)/0.35).^2 + ((y+0.21)/0.24).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Left lung collapse 5%
case 7
collapseArea = inline('((x-0.34)/0.32).^2 + ((y+0.52)/0.16).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Left lung collapse 10%
case 8
collapseArea = inline('((x-0.34)/0.32).^2 + ((y+0.42)/0.16).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Left lung collapse 15%
case 9
collapseArea = inline('((x-0.4)/0.32).^2 + ((y+0.34)/0.16).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Left lung collapse 20%
case 10
collapseArea = inline('((x-0.4)/0.32).^2 + ((y+0.29)/0.2).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Left lung collapse 25%
case 11
collapseArea = inline('((x-0.44)/0.34).^2 + ((y+0.25)/0.23).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Both lung collapse 5%
case 12
collapseArea = inline('(x/0.8).^2 + ((y+0.59)/0.2).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Both lung collapse 10%
case 13
collapseArea = inline('(x/0.8).^2 + ((y+0.53)/0.2).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Both lung collapse 15%
case 14
collapseArea = inline('(x/0.8).^2 + ((y+0.485)/0.2).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Both lung collapse 20%
case 15
collapseArea = inline('(x/0.8).^2 + ((y+0.435)/0.2).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Both lung collapse 25%
case 16
collapseArea = inline('(x/0.8).^2 + ((y+0.391)/0.2).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Both lung collapse 30%
case 17
collapseArea = inline('(x/0.8).^2 + ((y+0.35)/0.2).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Both lung collapse 35%
case 18
collapseArea = inline('(x/0.8).^2 + ((y+0.31)/0.2).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
% Both lung collapse 40%
case 19
collapseArea = inline('(x/0.8).^2 + ((y+0.288)/0.22).^2 < 1','x','y','z');
[row, ~] = find(elem_select(tempImg.fwd_model, collapseArea));
targetLungElem = setdiff(lungElem,row);
collapseP = 1 - length(targetLungElem) / length(lungElem);
disp(collapseP);
otherwise
error('Wrong collapseCase!')
end
RLungCollapseElem = intersect(fmdl.mat_idx{2},targetLungElem);
LLungCollapseElem = intersect(fmdl.mat_idx{3},targetLungElem);
RCollapseP = 1 - length(RLungCollapseElem) / length(fmdl.mat_idx{2});
LCollapseP = 1 - length(LLungCollapseElem) / length(fmdl.mat_idx{3});
disp([RCollapseP, LCollapseP])
%% Run GREIT and save EIT data during sine TV wave
for iter = 1:sigmaLen
tempImg.elem_data(targetLungElem) = sigma(iter);
if iter == 1
saveFig = figure; show_fem(tempImg); axis tight off % → Ok
saveas(saveFig, ['EIDORS Model Collapse shape (Collapse case ' num2str(collapseCase) ').bmp'])
close(saveFig);
% break; % For save fem model image
end
vk = fwd_solve(tempImg);
vCase = vk.meas;
% V = vCRef - vCase;
V = vRef - vCase;
% vErr = innerTerm*V;
% V = V - vErr; % -- CAUTION !!!
reconResult = reconSolver*V;
% -- Change image into 128*128
F = scatteredInterpolant(xy(:,1),xy(:,2),reconResult);
% Need to change the constant for each cases
gridReconResult = flipud(F(qx,qy)) / 400;
gridReconResult(gridReconResult<0) = 0;
% This makes outer body pixel zero (post processing)
gridReconResult = gridReconResult .* bodyShape128;
figure; imagesc(gridReconResult); axis square tight off % Check
% Save 128*128 image & voltage data for training
imgPath = [EIT_GREIT_Filepath '\EIT_EIDORS_Model_GREIT_collapse_case_' num2str(collapseCase) '_' num2str(iter) '.png'];
imwrite(gridReconResult,imgPath,'PNG'); close;
VPath = [EIT_V_Filepath '\EIT_EIDORS_Model_GREIT_Voltage_collapse_case_' num2str(collapseCase) '_' num2str(iter) '.csv'];
writematrix(V',VPath);
CPPath = [EIT_CP_Filepath '\EIT_EIDORS_Model_GREIT_CP_collapse_case_' num2str(collapseCase) '_' num2str(iter) '.csv'];
writematrix([RCollapseP, LCollapseP],CPPath);
disp(['EIDORS Model GREIT Case ' num2str(collapseCase) ' → ' num2str(iter) ' / ' num2str(sigmaLen) ' Finished ...']);
end
end
end